Skip to content

Commit

Permalink
More consistency across failure messages for operator matchers.
Browse files Browse the repository at this point in the history
Also cleaned up the related cukes. There was some duplication between
the operator and "be" matcher cukes, and the latter had some examples
that I prefer to discourage.
  • Loading branch information
dchelimsky committed Dec 20, 2010
1 parent 3b59fc1 commit fe8f85e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 148 deletions.
76 changes: 31 additions & 45 deletions features/matchers/be.feature
@@ -1,15 +1,11 @@
Feature: be matchers
Feature: "be" matchers

There are several related "be" matchers:

* obj.should be_true # passes if obj is truthy (not nil or false)
* obj.should be_false # passes if obj is falsey (nil or false)
* obj.should be_nil # passes if obj is nil
* obj.should be # passes if obj is not nil
* obj.should be < expected # passes if obj < expected
* obj.should be > expected # passes if obj > expected
* obj.should be <= expected # passes if obj <= expected
* obj.should be >= expected # passes if obj >= expected
obj.should be_true # passes if obj is truthy (not nil or false)
obj.should be_false # passes if obj is falsy (nil or false)
obj.should be_nil # passes if obj is nil
obj.should be # passes if obj is not nil

Scenario: be_true matcher
Given a file named "be_true_spec.rb" with:
Expand Down Expand Up @@ -84,13 +80,32 @@ Feature: be matchers
end
"""
When I run "rspec be_nil_spec.rb"
Then the output should contain all of these:
| 10 examples, 5 failures |
| expected not nil, got nil |
| expected nil, got false |
| expected nil, got true |
| expected nil, got 7 |
| expected nil, got "foo" |
Then the output should contain "10 examples, 5 failures"
And the output should contain:
"""
expected: not nil
got: nil
"""
And the output should contain:
"""
expected: nil
got: false
"""
And the output should contain:
"""
expected: nil
got: true
"""
And the output should contain:
"""
expected: nil
got: 7
"""
And the output should contain:
"""
expected: nil
got: "foo"
"""

Scenario: be matcher
Given a file named "be_spec.rb" with:
Expand Down Expand Up @@ -118,32 +133,3 @@ Feature: be matchers
| expected "foo" to evaluate to false |
| expected nil to evaluate to true |
| expected false to evaluate to true |

Scenario: be operator matchers
Given a file named "be_operators_spec.rb" with:
"""
describe 17 do
it { should be < 20 }
it { should be > 15 }
it { should be <= 17 }
it { should be >= 17 }
it { should_not be < 15 }
it { should_not be > 20 }
it { should_not be <= 16 }
it { should_not be >= 18 }
# deliberate failures
it { should be < 15 }
it { should be > 20 }
it { should be <= 16 }
it { should be >= 18 }
end
"""
When I run "rspec be_operators_spec.rb"
Then the output should contain all of these:
| 12 examples, 4 failures |
| expected < 15, got 17 |
| expected > 20, got 17 |
| expected <= 16, got 17 |
| expected >= 18, got 17 |

129 changes: 51 additions & 78 deletions features/matchers/operators.feature
@@ -1,114 +1,87 @@
Feature: Operator matchers

RSpec provides a number of matchers that are based on Ruby's built-in
operators. These mostly work like you expect. For example, each of these pass:
operators. These pretty much work like you expect. For example, each of these
pass:

* 7.should == 7
* 25.2.should < 100
* 8.should > 7
* 17.should <= 17
* 3.should >= 2
* [1, 2, 3].should == [1, 2, 3]
* "this is a string".should =~ /^this/
* "this is a string".should_not =~ /^that/
* String.should === "this is a string"
7.should == 7
[1, 2, 3].should == [1, 2, 3]
"this is a string".should =~ /^this/
"this is a string".should_not =~ /^that/
String.should === "this is a string"

You can also use comparison operators combined with the "be" matcher like
this:

37.should be < 100
37.should be <= 38
37.should be >= 2
37.should be > 7

RSpec also provides a `=~` matcher for arrays that disregards differences in
the ording between the actual and expected array. For example:

* [1, 2, 3].should =~ [2, 3, 1] # pass
* [:a, :c, :b].should =~ [:a, :c] # fail
[1, 2, 3].should =~ [2, 3, 1] # pass
[:a, :c, :b].should =~ [:a, :c] # fail

Scenario: numeric operator matchers
Given a file named "numeric_operator_matchers_spec.rb" with:
"""
describe 18 do
it { should == 18 }
it { should < 20 }
it { should > 15 }
it { should <= 19 }
it { should >= 17 }
it { should be < 20 }
it { should be > 15 }
it { should be <= 19 }
it { should be >= 17 }
it { should_not == 28 }
it { should_not < 15 }
it { should_not > 20 }
it { should_not <= 17 }
it { should_not >= 19 }
# deliberate failures
it { should == 28 }
it { should < 15 }
it { should > 20 }
it { should <= 17 }
it { should >= 19 }
it { should be < 15 }
it { should be > 20 }
it { should be <= 17 }
it { should be >= 19 }
it { should_not == 18 }
it { should_not < 20 }
it { should_not > 15 }
it { should_not <= 19 }
it { should_not >= 17 }
end
"""
When I run "rspec numeric_operator_matchers_spec.rb"
Then the output should contain "20 examples, 10 failures"
Then the output should contain "12 examples, 6 failures"
And the output should contain:
"""
Failure/Error: it { should == 28 }
expected: 28,
expected: 28
got: 18 (using ==)
"""
And the output should contain:
"""
Failure/Error: it { should < 15 }
expected: < 15,
Failure/Error: it { should be < 15 }
expected: < 15
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should > 20 }
expected: > 20,
Failure/Error: it { should be > 20 }
expected: > 20
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should <= 17 }
expected: <= 17,
Failure/Error: it { should be <= 17 }
expected: <= 17
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should >= 19 }
expected: >= 19,
Failure/Error: it { should be >= 19 }
expected: >= 19
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not == 18 }
expected not: == 18,
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not < 20 }
expected not: < 20,
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not > 15 }
expected not: > 15,
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not <= 19 }
expected not: <= 19,
got: 18
"""
And the output should contain:
"""
Failure/Error: it { should_not >= 17 }
expected not: >= 17,
expected not: == 18
got: 18
"""

Expand Down Expand Up @@ -159,79 +132,79 @@ Feature: Operator matchers
And the output should contain:
"""
Failure/Error: it { should == "Peach" }
expected: "Peach",
expected: "Peach"
got: "Strawberry" (using ==)
"""
And the output should contain:
"""
Failure/Error: it { should < "Cranberry" }
expected: < "Cranberry",
expected: < "Cranberry"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should > "Zuchini" }
expected: > "Zuchini",
expected: > "Zuchini"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should <= "Potato" }
expected: <= "Potato",
expected: <= "Potato"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should >= "Tomato" }
expected: >= "Tomato",
expected: >= "Tomato"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should =~ /apple/ }
expected: /apple/,
expected: /apple/
got: "Strawberry" (using =~)
"""
And the output should contain:
"""
Failure/Error: it { should_not == "Strawberry" }
expected not: == "Strawberry",
expected not: == "Strawberry"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not < "Tomato" }
expected not: < "Tomato",
expected not: < "Tomato"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not > "Apple" }
expected not: > "Apple",
expected not: > "Apple"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not <= "Turnip" }
expected not: <= "Turnip",
expected not: <= "Turnip"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not >= "Banana" }
expected not: >= "Banana",
expected not: >= "Banana"
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: it { should_not =~ /berry/ }
expected not: =~ /berry/,
expected not: =~ /berry/
got: "Strawberry"
"""
And the output should contain:
"""
Failure/Error: Symbol.should === subject
expected: "Strawberry",
expected: "Strawberry"
got: Symbol (using ===)
"""

Expand Down Expand Up @@ -260,13 +233,13 @@ Feature: Operator matchers
And the output should contain:
"""
Failure/Error: it { should_not == [1, 2, 3] }
expected not: == [1, 2, 3],
expected not: == [1, 2, 3]
got: [1, 2, 3]
"""
And the output should contain:
"""
Failure/Error: it { should == [1, 3, 2] }
expected: [1, 3, 2],
expected: [1, 3, 2]
got: [1, 2, 3] (using ==)
"""
And the output should contain:
Expand Down
8 changes: 4 additions & 4 deletions lib/rspec/matchers/be.rb
Expand Up @@ -2,7 +2,7 @@

RSpec::Matchers.define :be_true do
match do |actual|
!!actual
actual
end
end

Expand All @@ -18,11 +18,11 @@
end

failure_message_for_should do |actual|
"expected nil, got #{actual.inspect}"
"expected: nil\n got: #{actual.inspect}"
end

failure_message_for_should_not do
"expected not nil, got nil"
"expected: not nil\n got: nil"
end
end

Expand Down Expand Up @@ -96,7 +96,7 @@ def matches?(actual)
end

def failure_message_for_should
"expected #{@operator} #{@expected}, got #{@actual.inspect}"
"expected: #{@operator} #{@expected}\n got: #{@operator.to_s.gsub(/./, ' ')} #{@actual.inspect}"
end

def failure_message_for_should_not
Expand Down

0 comments on commit fe8f85e

Please sign in to comment.