From c0eda854aa5ef81dbce3158ee470d2f6bbd28b5e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 8 Feb 2024 20:38:18 +1300 Subject: [PATCH] Improve receive with argument and options formatting. --- lib/sus/receive.rb | 28 ++++++++++++++++------------ test/sus/receive.rb | 10 ++++++++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/sus/receive.rb b/lib/sus/receive.rb index 02923eb..e83d403 100644 --- a/lib/sus/receive.rb +++ b/lib/sus/receive.rb @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/test/sus/receive.rb b/test/sus/receive.rb index da17462..7796165 100644 --- a/test/sus/receive.rb +++ b/test/sus/receive.rb @@ -16,7 +16,7 @@ def call end class Interface - def implementation(*arguments) + def implementation(*arguments, **options) RealImplementation.new end end @@ -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