Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor matchers to use BaseMatcher's initialize and matches? methods

  • Loading branch information...
commit 40243447dd84aa5ec7cca13cf162f1f392d1bc29 1 parent 6a8c932
@dchelimsky dchelimsky authored
View
9 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
View
32 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)
View
9 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
View
8 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
View
2  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
View
4 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
View
12 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
View
12 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?
View
17 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.
View
9 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
View
19 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

2 comments on commit 4024344

@myronmarston

@dchelimsky -- This breaks lots of specs in rspec-mocks:

http://travis-ci.org/#!/rspec/rspec-mocks/builds/1692434

The rspec-mocks specs all passed for me locally before I pushed, so I was surprised to see the build fail. Then I git pulled the other rspec repos (including this one) and was able to reproduce the spec failures. Reverting this commit locally fixed the rspec-mocks failures.

I can look into fixing things in rspec-mocks, but it seems like this refactoring might not be as innocuous as originally thought, given that it broke another project. Could it break end users as well?

@dchelimsky
Owner

Thanks for pointing this out. Fixed by a3e2839. The problem was that the eq matcher needs to call actual == expected (not expected == actual) to support actuals that define their own equality (like the HashIncludingMatcher in rspec-mocks).

Please sign in to comment.
Something went wrong with that request. Please try again.