From a89cf154dcbfd226430438c46b49fb4462a67a72 Mon Sep 17 00:00:00 2001 From: Nicholas Koh Date: Fri, 26 Aug 2022 01:42:50 +0800 Subject: [PATCH] Attempt to set last argument on the object itself --- lib/mocha/parameter_matchers/base.rb | 2 +- lib/mocha/parameter_matchers/instance_methods.rb | 15 ++++++++++++--- lib/mocha/parameters_matcher.rb | 3 ++- test/unit/parameter_matchers/stub_matcher.rb | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/mocha/parameter_matchers/base.rb b/lib/mocha/parameter_matchers/base.rb index 430876b09..0afac05f6 100644 --- a/lib/mocha/parameter_matchers/base.rb +++ b/lib/mocha/parameter_matchers/base.rb @@ -3,7 +3,7 @@ module ParameterMatchers # @abstract Subclass and implement +#matches?+ and +#mocha_inspect+ to define a custom matcher. Also add a suitably named instance method to {ParameterMatchers} to build an instance of the new matcher c.f. {#equals}. class Base # @private - def to_matcher(*) + def to_matcher self end diff --git a/lib/mocha/parameter_matchers/instance_methods.rb b/lib/mocha/parameter_matchers/instance_methods.rb index b0870c846..3cf9e0e68 100644 --- a/lib/mocha/parameter_matchers/instance_methods.rb +++ b/lib/mocha/parameter_matchers/instance_methods.rb @@ -5,9 +5,17 @@ module ParameterMatchers # @private module InstanceMethods # @private - def to_matcher(*) + def to_matcher Mocha::ParameterMatchers::Equals.new(self) end + + def mark_last_argument + @is_last_argument = true + end + + def is_last_argument? + !!@is_last_argument + end end end end @@ -19,8 +27,9 @@ class Object # @private class Hash - def to_matcher(last_argument: false) - return Mocha::ParameterMatchers::LastPositionalHash.new(self) if last_argument + # @private + def to_matcher + return Mocha::ParameterMatchers::LastPositionalHash.new(self) if is_last_argument? super end diff --git a/lib/mocha/parameters_matcher.rb b/lib/mocha/parameters_matcher.rb index 49cb598c1..90e1c0288 100644 --- a/lib/mocha/parameters_matcher.rb +++ b/lib/mocha/parameters_matcher.rb @@ -29,7 +29,8 @@ def mocha_inspect end def matchers - @expected_parameters[0...-1].map(&:to_matcher) + [@expected_parameters.last.to_matcher(last_argument: true)] + @expected_parameters.last.mark_last_argument + @expected_parameters.map(&:to_matcher) end end end diff --git a/test/unit/parameter_matchers/stub_matcher.rb b/test/unit/parameter_matchers/stub_matcher.rb index 21acc85d3..79eb0f434 100644 --- a/test/unit/parameter_matchers/stub_matcher.rb +++ b/test/unit/parameter_matchers/stub_matcher.rb @@ -16,7 +16,7 @@ def mocha_inspect "matcher(#{@matches})" end - def to_matcher(*) + def to_matcher self end end