Skip to content

Commit

Permalink
Improve receive with argument and options formatting. (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Feb 8, 2024
1 parent 0c1f8ed commit f1f0f2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
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

0 comments on commit f1f0f2f

Please sign in to comment.