Skip to content

Commit

Permalink
add eq matcher (for ==)
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Feb 1, 2010
1 parent d372603 commit a23000e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'rspec/matchers/be_instance_of'
require 'rspec/matchers/be_kind_of'
require 'rspec/matchers/change'
require 'rspec/matchers/eq'
require 'rspec/matchers/eql'
require 'rspec/matchers/equal'
require 'rspec/matchers/errors'
Expand Down
47 changes: 47 additions & 0 deletions lib/rspec/matchers/eq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Rspec
module Matchers
# :call-seq:
# should eq(expected)
# should_not eq(expected)
#
# Passes if actual == expected.
#
# 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_|
match do |actual|
actual == _expected_
end

failure_message_for_should do |actual|
<<-MESSAGE
expected #{_expected_.inspect}
got #{actual.inspect}
(compared using ==)
MESSAGE
end

failure_message_for_should_not do |actual|
<<-MESSAGE
expected #{actual.inspect} not to equal #{_expected_.inspect}
(compared using ==)
MESSAGE
end

description do
"== #{_expected_}"
end
end
end
end
end

5 changes: 2 additions & 3 deletions rspec-expectations.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
s.authors = ["David Chelimsky", "Chad Humphries"]
s.date = %q{2010-01-29}
s.date = %q{2010-01-31}
s.email = %q{dchelimsky@gmail.com;chad.humphries@gmail.com}
s.extra_rdoc_files = [
"README.markdown"
Expand Down Expand Up @@ -79,7 +79,6 @@ Gem::Specification.new do |s|
"spec/rspec/matchers/include_spec.rb",
"spec/rspec/matchers/match_array_spec.rb",
"spec/rspec/matchers/match_spec.rb",
"spec/rspec/matchers/matcher_methods_spec.rb",
"spec/rspec/matchers/matcher_spec.rb",
"spec/rspec/matchers/matchers_spec.rb",
"spec/rspec/matchers/operator_matcher_spec.rb",
Expand Down Expand Up @@ -110,6 +109,7 @@ Gem::Specification.new do |s|
"spec/rspec/matchers/compatibility_spec.rb",
"spec/rspec/matchers/description_generation_spec.rb",
"spec/rspec/matchers/dsl_spec.rb",
"spec/rspec/matchers/eq_spec.rb",
"spec/rspec/matchers/eql_spec.rb",
"spec/rspec/matchers/equal_spec.rb",
"spec/rspec/matchers/exist_spec.rb",
Expand All @@ -118,7 +118,6 @@ Gem::Specification.new do |s|
"spec/rspec/matchers/include_spec.rb",
"spec/rspec/matchers/match_array_spec.rb",
"spec/rspec/matchers/match_spec.rb",
"spec/rspec/matchers/matcher_methods_spec.rb",
"spec/rspec/matchers/matcher_spec.rb",
"spec/rspec/matchers/matchers_spec.rb",
"spec/rspec/matchers/operator_matcher_spec.rb",
Expand Down
34 changes: 34 additions & 0 deletions spec/rspec/matchers/eq_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

module Rspec
module Matchers
describe "eq" do
it "matches when actual == expected" do
1.should eq(1)
end

it "does not match when actual != expected" do
1.should_not eq(2)
end

it "describes itself" do
matcher = eq(1)
matcher.matches?(1)
matcher.description.should == "== 1"
end

it "provides message, expected and actual on #failure_message" do
matcher = eq("1")
matcher.matches?(1)
matcher.failure_message_for_should.should == "\nexpected \"1\"\n got 1\n\n(compared using ==)\n"
end

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"
end
end
end
end

0 comments on commit a23000e

Please sign in to comment.