Skip to content

Commit

Permalink
Attempt to set last argument on the object itself
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabigeek committed Aug 25, 2022
1 parent cf19d36 commit a89cf15
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/mocha/parameter_matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions lib/mocha/parameter_matchers/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/mocha/parameters_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/unit/parameter_matchers/stub_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def mocha_inspect
"matcher(#{@matches})"
end

def to_matcher(*)
def to_matcher
self
end
end
Expand Down

0 comments on commit a89cf15

Please sign in to comment.