diff --git a/lib/rspec/matchers/built_in/base_matcher.rb b/lib/rspec/matchers/built_in/base_matcher.rb index d0b0ffc44..4572f1b3b 100644 --- a/lib/rspec/matchers/built_in/base_matcher.rb +++ b/lib/rspec/matchers/built_in/base_matcher.rb @@ -16,6 +16,15 @@ module BaseMatcher attr_reader :actual, :expected, :rescued_exception + def initialize(expected = nil) + @expected = expected + end + + def matches?(actual) + @actual = actual + match(expected, actual) + end + def match_unless_raises(*exceptions) exceptions.unshift Exception if exceptions.empty? begin diff --git a/lib/rspec/matchers/built_in/be.rb b/lib/rspec/matchers/built_in/be.rb index 8476e9dc7..514032104 100644 --- a/lib/rspec/matchers/built_in/be.rb +++ b/lib/rspec/matchers/built_in/be.rb @@ -6,44 +6,44 @@ module BuiltIn class BeTrue include BaseMatcher - def matches?(actual) - @actual = actual + def match(_, actual) + !!actual end def failure_message_for_should - "expected: true value\n got: #{@actual.inspect}" + "expected: true value\n got: #{actual.inspect}" end def failure_message_for_should_not - "expected: non-true value\n got: #{@actual.inspect}" + "expected: non-true value\n got: #{actual.inspect}" end end class BeFalse include BaseMatcher - def matches?(actual) - !(@actual = actual) + def match(_, actual) + !actual end def failure_message_for_should - "expected: false value\n got: #{@actual.inspect}" + "expected: false value\n got: #{actual.inspect}" end def failure_message_for_should_not - "expected: non-false value\n got: #{@actual.inspect}" + "expected: non-false value\n got: #{actual.inspect}" end end class BeNil include BaseMatcher - def matches?(actual) - (@actual = actual).nil? + def match(_, actual) + actual.nil? end def failure_message_for_should - "expected: nil\n got: #{@actual.inspect}" + "expected: nil\n got: #{actual.inspect}" end def failure_message_for_should_not @@ -52,14 +52,14 @@ def failure_message_for_should_not end class Be - include RSpec::Matchers::Pretty + include BaseMatcher def initialize(*args, &block) @args = args end - def matches?(actual) - !!(@actual = actual) + def match(_, actual) + !!actual end def failure_message_for_should @@ -70,10 +70,6 @@ def failure_message_for_should_not "expected #{@actual.inspect} to evaluate to false" end - def description - "be" - end - [:==, :<, :<=, :>=, :>, :===].each do |operator| define_method operator do |operand| BeComparedTo.new(operand, operator) diff --git a/lib/rspec/matchers/built_in/be_instance_of.rb b/lib/rspec/matchers/built_in/be_instance_of.rb index 4aa1fb57e..17ba4937b 100644 --- a/lib/rspec/matchers/built_in/be_instance_of.rb +++ b/lib/rspec/matchers/built_in/be_instance_of.rb @@ -4,13 +4,8 @@ module BuiltIn class BeAnInstanceOf include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - @actual.instance_of? @expected + def match(expected, actual) + actual.instance_of? expected end end end diff --git a/lib/rspec/matchers/built_in/be_kind_of.rb b/lib/rspec/matchers/built_in/be_kind_of.rb index 1e9969780..e47fc1d5c 100644 --- a/lib/rspec/matchers/built_in/be_kind_of.rb +++ b/lib/rspec/matchers/built_in/be_kind_of.rb @@ -4,12 +4,8 @@ module BuiltIn class BeAKindOf include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - (@actual = actual).kind_of?(@expected) + def match(expected, actual) + actual.kind_of? expected end end end diff --git a/lib/rspec/matchers/built_in/be_within.rb b/lib/rspec/matchers/built_in/be_within.rb index 6bebb23f6..a9617d86c 100644 --- a/lib/rspec/matchers/built_in/be_within.rb +++ b/lib/rspec/matchers/built_in/be_within.rb @@ -2,8 +2,6 @@ module RSpec module Matchers module BuiltIn class BeWithin - include BaseMatcher - def initialize(delta) @delta = delta end diff --git a/lib/rspec/matchers/built_in/cover.rb b/lib/rspec/matchers/built_in/cover.rb index ea5711ea4..4b6be8017 100644 --- a/lib/rspec/matchers/built_in/cover.rb +++ b/lib/rspec/matchers/built_in/cover.rb @@ -10,12 +10,12 @@ def initialize(*expected) def matches?(range) @actual = range - @expected.all? {|e| range.cover?(e)} + @expected.all? { |e| range.cover?(e) } end def does_not_match?(range) @actual = range - expected.none? {|e| range.cover?(e)} + expected.none? { |e| range.cover?(e) } end end end diff --git a/lib/rspec/matchers/built_in/eq.rb b/lib/rspec/matchers/built_in/eq.rb index f4f1d34c7..62b9e2786 100644 --- a/lib/rspec/matchers/built_in/eq.rb +++ b/lib/rspec/matchers/built_in/eq.rb @@ -4,20 +4,16 @@ module BuiltIn class Eq include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - (@actual = actual) == @expected + def match(expected, actual) + expected == actual end def failure_message_for_should - "\nexpected: #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using ==)\n" + "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n" end def failure_message_for_should_not - "\nexpected: value != #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using ==)\n" + "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n" end def diffable?; true; end diff --git a/lib/rspec/matchers/built_in/eql.rb b/lib/rspec/matchers/built_in/eql.rb index 436ac79ab..cd08aaf29 100644 --- a/lib/rspec/matchers/built_in/eql.rb +++ b/lib/rspec/matchers/built_in/eql.rb @@ -4,20 +4,16 @@ module BuiltIn class Eql include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - (@actual = actual).eql?(@expected) + def match(expected, actual) + actual.eql? expected end def failure_message_for_should - "\nexpected: #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using eql?)\n" + "\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n" end def failure_message_for_should_not - "\nexpected: value != #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using eql?)\n" + "\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n" end def diffable? diff --git a/lib/rspec/matchers/built_in/equal.rb b/lib/rspec/matchers/built_in/equal.rb index d88ca5744..c2d403e39 100644 --- a/lib/rspec/matchers/built_in/equal.rb +++ b/lib/rspec/matchers/built_in/equal.rb @@ -4,20 +4,15 @@ module BuiltIn class Equal include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - @actual.equal? @expected + def match(expected, actual) + actual.equal? expected end def failure_message_for_should return <<-MESSAGE -expected #{inspect_object(@expected)} - got #{inspect_object(@actual)} +expected #{inspect_object(expected)} + got #{inspect_object(actual)} Compared using equal?, which compares object identity, but expected and actual are not the same object. Use @@ -30,8 +25,8 @@ def failure_message_for_should def failure_message_for_should_not return <<-MESSAGE -expected not #{inspect_object(@actual)} - got #{inspect_object(@expected)} +expected not #{inspect_object(actual)} + got #{inspect_object(expected)} Compared using equal?, which compares object identity. diff --git a/lib/rspec/matchers/built_in/match.rb b/lib/rspec/matchers/built_in/match.rb index bd189b803..f870f5db3 100644 --- a/lib/rspec/matchers/built_in/match.rb +++ b/lib/rspec/matchers/built_in/match.rb @@ -4,13 +4,8 @@ module BuiltIn class Match include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - @actual.match @expected + def match(expected, actual) + actual.match expected end end end diff --git a/lib/rspec/matchers/built_in/match_array.rb b/lib/rspec/matchers/built_in/match_array.rb index bb1c4849f..319ca4a1c 100644 --- a/lib/rspec/matchers/built_in/match_array.rb +++ b/lib/rspec/matchers/built_in/match_array.rb @@ -2,22 +2,17 @@ module RSpec module Matchers module BuiltIn class MatchArray - include RSpec::Matchers::Pretty + include BaseMatcher - def initialize(expected) - @expected = expected - end - - def matches?(actual) - @actual = actual - @extra_items = difference_between_arrays(@actual, @expected) - @missing_items = difference_between_arrays(@expected, @actual) + def match(expected, actual) + @extra_items = difference_between_arrays(actual, expected) + @missing_items = difference_between_arrays(expected, actual) @extra_items.empty? & @missing_items.empty? end def failure_message_for_should - message = "expected collection contained: #{safe_sort(@expected).inspect}\n" - message += "actual collection contained: #{safe_sort(@actual).inspect}\n" + message = "expected collection contained: #{safe_sort(expected).inspect}\n" + message += "actual collection contained: #{safe_sort(actual).inspect}\n" message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty? message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty? message @@ -28,7 +23,7 @@ def failure_message_for_should_not end def description - "contain exactly #{_pretty_print(@expected)}" + "contain exactly #{_pretty_print(expected)}" end private