Permalink
Browse files

move eq matcher to a class

  • Loading branch information...
1 parent 73606f7 commit e8e4e5f8e129db0cb3d58aabde64be321043b4fd @dchelimsky dchelimsky committed Oct 17, 2011
Showing with 38 additions and 36 deletions.
  1. +12 −0 lib/rspec/matchers.rb
  2. +23 −33 lib/rspec/matchers/eq.rb
  3. +1 −1 spec/rspec/matchers/eq_spec.rb
  4. +2 −2 spec/rspec/matchers/raise_error_spec.rb
View
12 lib/rspec/matchers.rb
@@ -171,6 +171,18 @@ module Matchers
if defined?(Test::Unit::TestCase)
Test::Unit::TestCase.send(:include, self)
end
+
+ # Passes if <tt>actual == expected</tt>.
+ #
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
+ #
+ # == Examples
+ #
+ # 5.should eq(5)
+ # 5.should_not eq(3)
+ def eq(expected)
+ Eq.new(expected)
+ end
end
end
View
56 lib/rspec/matchers/eq.rb
@@ -1,44 +1,34 @@
module RSpec
module Matchers
- # Passes if <tt>actual == expected</tt>.
- #
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
- #
- # == Examples
- #
- # 5.should eq(5)
- # 5.should_not eq(3)
- def eq(expected)
- Matcher.new :eq, expected do |_expected_|
-
- diffable
-
- match do |actual|
- actual == _expected_
- end
-
- failure_message_for_should do |actual|
- <<-MESSAGE
+ class Eq
+ attr_reader :actual
+ def initialize(expected)
+ @expected = expected
+ end
-expected: #{_expected_.inspect}
- got: #{actual.inspect}
+ def expected
+ [@expected]
+ end
-(compared using ==)
-MESSAGE
- end
+ def matches?(actual)
+ @actual = actual
+ @actual == @expected
+ end
- failure_message_for_should_not do |actual|
- <<-MESSAGE
+ def failure_message_for_should
+ "\nexpected: #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using ==)\n"
+ end
-expected #{actual.inspect} not to equal #{_expected_.inspect}
+ def failure_message_for_should_not
+ "\nexpected: value != #{@expected.inspect}\n got: #{@actual.inspect}\n\n(compared using ==)\n"
+ end
-(compared using ==)
-MESSAGE
- end
+ def diffable?
+ true
+ end
- description do
- "eq #{_expected_}"
- end
+ def description
+ "eq #{@expected}"
end
end
end
View
2 spec/rspec/matchers/eq_spec.rb
@@ -30,7 +30,7 @@ module Matchers
it "provides message, expected and actual on #negative_failure_message" do
matcher = eq(1)
matcher.matches?(1)
- matcher.failure_message_for_should_not.should == "\nexpected 1 not to equal 1\n\n(compared using ==)\n"
+ matcher.failure_message_for_should_not.should == "\nexpected: value != 1\n got: 1\n\n(compared using ==)\n"
end
end
end
View
4 spec/rspec/matchers/raise_error_spec.rb
@@ -181,8 +181,8 @@
}
}.should fail_with(/expected: 4/m)
- ran.should be(true)
- passed.should be(false)
+ ran.should be_true
+ passed.should be_false
end
it "does NOT yield exception if no error was thrown" do

0 comments on commit e8e4e5f

Please sign in to comment.