Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

beef up equality matchers feature

  • Loading branch information...
commit 7cdb347ce0ac06aa8508639f9f674b32a20f0f0e 1 parent 7b52164
David Chelimsky dchelimsky authored
Showing with 75 additions and 28 deletions.
  1. +75 −28 features/matchers/equality.feature
103 features/matchers/equality.feature
View
@@ -1,13 +1,25 @@
Feature: equality matchers
- RSpec equality comparison matchers.
+ Ruby exposes several different methods for handling equality:
- Typically, if you are comparing values, "eq" and "eql" will
- usually do the job. However, one thing to note is that "eq"
- will perform type conversion, while "eql" will not.
+ a.equal?(b) # object identity - a and b refer to the same object
+ a.eql?(b) # object equivalence - a and b have the same value
+ a == b # object equivalence - a and b have the same value with type conversions
- If you need to compare equality of objects, "equal" is the
- one you want.
+ Note that these descriptions are guidelines but are not forced by the
+ language. Any object can implement any of these methods with its own
+ semantics.
+
+ rspec-expectations ships with matchers that align with each of these methods:
+
+ a.should equal(b) # passes if a.equal?(b)
+ a.should eql(b) # passes if a.eql?(b)
+ a.should == b # passes if a == b
+
+ It also ships with two matchers that have more of a DSL feel to them:
+
+ a.should be(b) # passes if a.equal?(b)
+ a.should eq(b) # passes if a == b
Scenario: compare using eq (==)
Given a file named "compare_using_eq.rb" with:
@@ -15,30 +27,46 @@ Feature: equality matchers
require 'spec_helper'
describe "a string" do
-
- let(:string) { "foo" }
-
it "is equal to another string of the same value" do
- string.should eq("foo")
+ "this string".should eq("this string")
end
it "is not equal to another string of a different value" do
- string.should_not eq("bar")
+ "this string".should_not eq("a different string")
end
-
end
describe "an integer" do
+ it "is equal to a float of the same value" do
+ 5.should eq(5.0)
+ end
+ end
+ """
+ When I run "rspec compare_using_eq.rb"
+ Then the output should contain "3 examples, 0 failures"
- let(:integer) { 5 }
+ Scenario: compare using ==
+ Given a file named "compare_using_==.rb" with:
+ """
+ require 'spec_helper'
- it "is equal to the same value in float type" do
- integer.should eq(5.0)
+ describe "a string" do
+ it "is equal to another string of the same value" do
+ "this string".should == "this string"
end
+ it "is not equal to another string of a different value" do
+ "this string".should_not == "a different string"
+ end
+ end
+
+ describe "an integer" do
+ it "is equal to a float of the same value" do
+ 5.should == 5.0
+ end
end
"""
- When I run "rspec compare_using_eq.rb"
+ When I run "rspec compare_using_==.rb"
Then the output should contain "3 examples, 0 failures"
Scenario: compare using eql (eql?)
@@ -47,19 +75,16 @@ Feature: equality matchers
require 'spec_helper'
describe "an integer" do
-
- let(:integer) { 5 }
-
it "is equal to another integer of the same value" do
- integer.should eql(5)
+ 5.should eql(5)
end
it "is not equal to another integer of a different value" do
- integer.should_not eql(6)
+ 5.should_not eql(6)
end
- it "is not equal to the same value in float type" do
- integer.should_not eql(5.0)
+ it "is not equal to a float of the same value" do
+ 5.should_not eql(5.0)
end
end
@@ -73,19 +98,17 @@ Feature: equality matchers
require 'spec_helper'
describe "a string" do
-
- let(:string) { "foo" }
-
it "is equal to itself" do
+ string = "this string"
string.should equal(string)
end
it "is not equal to another string of the same value" do
- string.should_not equal("foo")
+ "this string".should_not equal("this string")
end
it "is not equal to another string of a different value" do
- string.should_not equal("bar")
+ "this string".should_not equal("a different string")
end
end
@@ -93,3 +116,27 @@ Feature: equality matchers
When I run "rspec compare_using_equal.rb"
Then the output should contain "3 examples, 0 failures"
+ Scenario: compare using be (equal?)
+ Given a file named "compare_using_be.rb" with:
+ """
+ require 'spec_helper'
+
+ describe "a string" do
+ it "is equal to itself" do
+ string = "this string"
+ string.should be(string)
+ end
+
+ it "is not equal to another string of the same value" do
+ "this string".should_not be("this string")
+ end
+
+ it "is not equal to another string of a different value" do
+ "this string".should_not be("a different string")
+ end
+
+ end
+ """
+ When I run "rspec compare_using_be.rb"
+ Then the output should contain "3 examples, 0 failures"
+

1 comment on commit 7cdb347

Justin Ko

Documentation doesn't get any better than this!

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