From b701d0fd0bcf24d1b1d6a01ba05b688a58061341 Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Tue, 29 Nov 2011 05:06:07 -0600 Subject: [PATCH] clean up ArgumentMatchers --- lib/rspec/mocks/argument_matchers.rb | 44 ++++++++++++---------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/rspec/mocks/argument_matchers.rb b/lib/rspec/mocks/argument_matchers.rb index 0b75254f4..6d3ad8b2d 100644 --- a/lib/rspec/mocks/argument_matchers.rb +++ b/lib/rspec/mocks/argument_matchers.rb @@ -1,12 +1,12 @@ module RSpec module Mocks - # ArgumentMatchers are messages that you can include in message + # ArgumentMatchers are placeholders that you can include in message # expectations to match arguments against a broader check than simple # equality. # - # With the exception of any_args() and no_args(), the matchers - # are all positional - they match against the arg in the given position. + # With the exception of `any_args` and `no_args`, they all match against + # the arg in same position in the argument list. module ArgumentMatchers class AnyArgsMatcher @@ -36,8 +36,7 @@ def initialize(regexp) end def ==(value) - return value =~ @regexp unless value.is_a?(Regexp) - value == @regexp + Regexp === value ? value == @regexp : value =~ @regexp end end @@ -46,7 +45,7 @@ def initialize(ignore) end def ==(value) - TrueClass === value || FalseClass === value + [true,false].include?(value) end end @@ -56,12 +55,9 @@ def initialize(expected) end def ==(actual) - @expected.each do | key, value | - return false unless actual.has_key?(key) && value == actual[key] - end - true + @expected.all? {|k,v| actual.has_key?(k) && v == actual[k]} rescue NoMethodError - return false + false end def description @@ -75,12 +71,9 @@ def initialize(expected) end def ==(actual) - @expected.each do | key, value | - return false if actual.has_key?(key) && value == actual[key] - end - true + @expected.none? {|k,v| actual.has_key?(k) && v == actual[k]} rescue NoMethodError - return false + false end def description @@ -94,7 +87,7 @@ def initialize(*methods_to_respond_to) end def ==(value) - @methods_to_respond_to.all? { |sym| value.respond_to?(sym) } + @methods_to_respond_to.all? {|sym| value.respond_to?(sym)} end end @@ -185,8 +178,8 @@ def boolean BooleanMatcher.new(nil) end - # Passes if the argument is a hash that includes the specified key(s) or key/value - # pairs. If the hash includes other keys, it will still pass. + # Passes if the argument is a hash that includes the specified key(s) or + # key/value pairs. If the hash includes other keys, it will still pass. # # @example # @@ -197,27 +190,28 @@ def hash_including(*args) HashIncludingMatcher.new(anythingize_lonely_keys(*args)) end - # Passes if the argument is a hash that doesn't include the specified key(s) or key/value + # Passes if the argument is a hash that doesn't include the specified + # key(s) or key/value # # @example # - # object.should_receive(:message).with(hash_not_including(:key => val)) - # object.should_receive(:message).with(hash_not_including(:key)) - # object.should_receive(:message).with(hash_not_including(:key, :key2 => :val2)) + # object.should_receive(:message).with(hash_excluding(:key => val)) + # object.should_receive(:message).with(hash_excluding(:key)) + # object.should_receive(:message).with(hash_excluding(:key, :key2 => :val2)) def hash_excluding(*args) HashExcludingMatcher.new(anythingize_lonely_keys(*args)) end alias_method :hash_not_including, :hash_excluding - # Passes if arg.instance_of?(klass) + # Passes if `arg.instance_of?(klass)` def instance_of(klass) InstanceOf.new(klass) end alias_method :an_instance_of, :instance_of - # Passes if arg.kind_of?(klass) + # Passes if `arg.kind_of?(klass)` def kind_of(klass) KindOf.new(klass) end