Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve receive with argument and options formatting. #39

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/sus/receive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(base, method)
end

def print(output)
output.write("receive ", :variable, @method.to_s, :reset, " ")
output.write("receive ", :variable, @method.to_s, :reset)
end

def with_arguments(predicate)
Expand Down Expand Up @@ -70,33 +70,37 @@ def and_return(*returning)
end

def validate(mock, assertions, arguments, options, block)
@arguments.call(assertions, arguments) if @arguments
@options.call(assertions, options) if @options
@block.call(assertions, block) if @block
return unless @arguments or @options or @block

assertions.nested(self) do |assertions|
@arguments.call(assertions, arguments) if @arguments
@options.call(assertions, options) if @options
@block.call(assertions, block) if @block
end
end

def call(assertions, subject)
assertions.nested(self) do |assertions|
mock = @base.mock(subject)

called = 0

if call_original?
mock.before(@method) do |*arguments, **options, &block|
called += 1

validate(mock, assertions, arguments, options, block)
end
else
mock.replace(@method) do |*arguments, **options, &block|
called += 1

validate(mock, assertions, arguments, options, block)

next @returning
end
end

if @times
assertions.defer do
@times.call(assertions, called)
Expand All @@ -120,7 +124,7 @@ def print(output)

def call(assertions, subject)
assertions.nested(self) do |assertions|
@predicate.call(assertions, subject)
Expect.new(assertions, subject).to(@predicate)
end
end
end
Expand All @@ -136,7 +140,7 @@ def print(output)

def call(assertions, subject)
assertions.nested(self) do |assertions|
@predicate.call(assertions, subject)
Expect.new(assertions, subject).to(@predicate)
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions test/sus/receive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def call
end

class Interface
def implementation(*arguments)
def implementation(*arguments, **options)
RealImplementation.new
end
end
Expand All @@ -37,9 +37,15 @@ def implementation(*arguments)
interface.implementation(:foo, :bar)
end

it "can validate arguments" do
it "can validate (not) arguments" do
expect(interface).not.to receive(:implementation).with(:foo, :bar)

interface.implementation(:foo, :bar2)
end

it "can validate options" do
expect(interface).to receive(:implementation).with(x: 1, y: 2)

interface.implementation(x: 1, y: 2)
end
end
Loading