Skip to content

Commit

Permalink
Merge pull request #368 from rspec/simpler-failure-equal-singletons
Browse files Browse the repository at this point in the history
Use a simpler message for equal on true false and nil
  • Loading branch information
Sam Phippen committed Nov 24, 2013
2 parents 0673c19 + a048ee1 commit 8caf5a8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
@@ -1,6 +1,11 @@
### 3.0.0.beta2 Development
[full changelog](http://github.com/rspec/rspec-expectations/compare/v3.0.0.beta1...v3.0.0.beta2)

Ehancements:

* Simplify the failure message of the `be` matcher when matching against:
`true`, `false` and `nil`. (Sam Phippen)

Breaking Changes for 3.0.0:

* Remove deprecated support for accessing the `RSpec` constant using
Expand Down
45 changes: 33 additions & 12 deletions lib/rspec/matchers/built_in/equal.rb
Expand Up @@ -7,17 +7,11 @@ def match(expected, actual)
end

def failure_message_for_should
return <<-MESSAGE
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
`#{eq_expression}` if you don't care about
object identity in this example.
MESSAGE
if expected_is_a_literal_singleton?
simple_failure_message
else
detailed_failure_message
end
end

def failure_message_for_should_not
Expand All @@ -31,10 +25,37 @@ def failure_message_for_should_not
MESSAGE
end

def diffable?; true; end
def diffable?
!expected_is_a_literal_singleton?
end

private

LITERAL_SINGLETONS = [true, false, nil]

def expected_is_a_literal_singleton?
LITERAL_SINGLETONS.include?(expected)
end

def simple_failure_message
"\nexpected #{expected.inspect}\n got #{inspect_object(actual)}\n"
end

def detailed_failure_message
return <<-MESSAGE
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
`#{eq_expression}` if you don't care about
object identity in this example.
MESSAGE
end


def inspect_object(o)
"#<#{o.class}:#{o.object_id}> => #{o.inspect}"
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rspec/matchers/equal_spec.rb
Expand Up @@ -50,6 +50,27 @@ def string.equal?(other)
end
end

it "prints a special message for `false`" do
expected, actual = false, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected false\n got #{inspect_object(actual)}\n"
end

it "prints a special message for `true`" do
expected, actual = true, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected true\n got #{inspect_object(actual)}\n"
end

it "prints a special message for `nil`" do
expected, actual = nil, "1"
expect {
expect(actual).to equal(expected)
}.to fail_with "\nexpected nil\n got #{inspect_object(actual)}\n"
end

it "suggests the `eq` matcher on failure" do
expected, actual = "1", "1"
expect {
Expand Down

0 comments on commit 8caf5a8

Please sign in to comment.