Skip to content

Commit

Permalink
Merge 4e4ef45 into b43952f
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Phippen committed Jul 21, 2013
2 parents b43952f + 4e4ef45 commit 596b988
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 110 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -6,6 +6,7 @@ Breaking Changes for 3.0.0:
* Remove explicit support for 1.8.6 (Jon Rowe)
* Remove the deprecated `be_close` matcher, preferring `be_within` instead.
(Sam Phippen)
* Rename `be_true` and `be_false` to `be_truthy` and `be_falsey`. (Sam Phippen)

### 2.14.0 / 2013-07-06
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.0.rc1...v2.14.0)
Expand Down
60 changes: 30 additions & 30 deletions features/built_in_matchers/be.feature
Expand Up @@ -3,31 +3,31 @@ Feature: "be" matchers
There are several related "be" matchers:

```ruby
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_truthy # passes if obj is truthy (not nil or false)
obj.should be_falsey # passes if obj is falsy (nil or false)
obj.should be_nil # passes if obj is nil
obj.should be # passes if obj is truthy (not nil or false)
```

Scenario: be_true matcher
Given a file named "be_true_spec.rb" with:
Scenario: be_truthy matcher
Given a file named "be_truthy_spec.rb" with:
"""ruby
describe "be_true matcher" do
specify { true.should be_true }
specify { 7.should be_true }
specify { "foo".should be_true }
specify { nil.should_not be_true }
specify { false.should_not be_true }
describe "be_truthy matcher" do
specify { true.should be_truthy }
specify { 7.should be_truthy }
specify { "foo".should be_truthy }
specify { nil.should_not be_truthy }
specify { false.should_not be_truthy }
# deliberate failures
specify { true.should_not be_true }
specify { 7.should_not be_true }
specify { "foo".should_not be_true }
specify { nil.should be_true }
specify { false.should be_true }
specify { true.should_not be_truthy }
specify { 7.should_not be_truthy }
specify { "foo".should_not be_truthy }
specify { nil.should be_truthy }
specify { false.should be_truthy }
end
"""
When I run `rspec be_true_spec.rb`
When I run `rspec be_truthy_spec.rb`
Then the output should contain "10 examples, 5 failures"
And the output should contain:
"""
Expand Down Expand Up @@ -55,25 +55,25 @@ Feature: "be" matchers
got: false
"""

Scenario: be_false matcher
Given a file named "be_false_spec.rb" with:
Scenario: be_falsey matcher
Given a file named "be_falsey_spec.rb" with:
"""ruby
describe "be_false matcher" do
specify { nil.should be_false }
specify { false.should be_false }
specify { true.should_not be_false }
specify { 7.should_not be_false }
specify { "foo".should_not be_false }
describe "be_falsey matcher" do
specify { nil.should be_falsey }
specify { false.should be_falsey }
specify { true.should_not be_falsey }
specify { 7.should_not be_falsey }
specify { "foo".should_not be_falsey }
# deliberate failures
specify { nil.should_not be_false }
specify { false.should_not be_false }
specify { true.should be_false }
specify { 7.should be_false }
specify { "foo".should be_false }
specify { nil.should_not be_falsey }
specify { false.should_not be_falsey }
specify { true.should be_falsey }
specify { 7.should be_falsey }
specify { "foo".should be_falsey }
end
"""
When I run `rspec be_false_spec.rb`
When I run `rspec be_falsey_spec.rb`
Then the output should contain "10 examples, 5 failures"
And the output should contain:
"""
Expand Down
14 changes: 8 additions & 6 deletions lib/rspec/matchers.rb
Expand Up @@ -182,23 +182,25 @@ def self.is_a_matcher?(obj)
end

# Passes if actual is truthy (anything but false or nil)
def be_true
BuiltIn::BeTrue.new
def be_truthy
BuiltIn::BeTruthy.new
end

# Passes if actual is falsy (false or nil)
def be_false
BuiltIn::BeFalse.new
def be_falsey
BuiltIn::BeFalsey.new
end

alias_method :be_falsy, :be_falsey

# Passes if actual is nil
def be_nil
BuiltIn::BeNil.new
end

# @example
# expect(actual).to be_true
# expect(actual).to be_false
# expect(actual).to be_truthy
# expect(actual).to be_falsey
# expect(actual).to be_nil
# expect(actual).to be_[arbitrary_predicate](*args)
# expect(actual).not_to be_nil
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/matchers/built_in.rb
Expand Up @@ -4,8 +4,8 @@ module BuiltIn
require 'rspec/matchers/built_in/base_matcher'
autoload :BeAnInstanceOf, 'rspec/matchers/built_in/be_instance_of'
autoload :Be, 'rspec/matchers/built_in/be'
autoload :BeTrue, 'rspec/matchers/built_in/be'
autoload :BeFalse, 'rspec/matchers/built_in/be'
autoload :BeTruthy, 'rspec/matchers/built_in/be'
autoload :BeFalsey, 'rspec/matchers/built_in/be'
autoload :BeNil, 'rspec/matchers/built_in/be'
autoload :BeComparedTo, 'rspec/matchers/built_in/be'
autoload :BePredicate, 'rspec/matchers/built_in/be'
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/matchers/built_in/be.rb
Expand Up @@ -3,7 +3,7 @@
module RSpec
module Matchers
module BuiltIn
class BeTrue < BaseMatcher
class BeTruthy < BaseMatcher
def match(_, actual)
!!actual
end
Expand All @@ -17,7 +17,7 @@ def failure_message_for_should_not
end
end

class BeFalse < BaseMatcher
class BeFalsey < BaseMatcher
def match(_, actual)
!actual
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/expectations/handler_spec.rb
Expand Up @@ -140,7 +140,7 @@ module Expectations
actual = Object.new
matcher.should_receive(:matches?).with(actual).and_return(false)
matcher.stub(:negative_failure_message).and_return("ignore")
expect(RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)).to be_false
expect(RSpec::Expectations::NegativeExpectationHandler.handle_matcher(actual, matcher)).to be_falsey
end


Expand Down
12 changes: 6 additions & 6 deletions spec/rspec/expectations/syntax_spec.rb
Expand Up @@ -91,8 +91,8 @@ def negative_expression

context "when only :expect is enabled" do
before do
expect(Syntax.should_enabled?).to be_false
expect(Syntax.expect_enabled?).to be_true
expect(Syntax.should_enabled?).to be_falsey
expect(Syntax.expect_enabled?).to be_truthy
end

it 'generates a positive expression using the expect syntax' do
Expand All @@ -106,8 +106,8 @@ def negative_expression

context "when both :should and :expect are enabled", :uses_should do
before do
expect(Syntax.should_enabled?).to be_true
expect(Syntax.expect_enabled?).to be_true
expect(Syntax.should_enabled?).to be_truthy
expect(Syntax.expect_enabled?).to be_truthy
end

it 'generates a positive expression using the expect syntax' do
Expand All @@ -121,8 +121,8 @@ def negative_expression

context "when only :should is enabled", :uses_only_should do
before do
Syntax.should_enabled?.should be_true
Syntax.expect_enabled?.should be_false
Syntax.should_enabled?.should be_truthy
Syntax.expect_enabled?.should be_falsey
end

it 'generates a positive expression using the expect syntax' do
Expand Down
16 changes: 8 additions & 8 deletions spec/rspec/matchers/base_matcher_spec.rb
Expand Up @@ -8,21 +8,21 @@ module RSpec::Matchers::BuiltIn
end

it "returns true if there are no errors" do
expect(matcher.match_unless_raises {}).to be_true
expect(matcher.match_unless_raises {}).to be_truthy
end

it "returns false if there is an error" do
expect(matcher.match_unless_raises { raise }).to be_false
expect(matcher.match_unless_raises { raise }).to be_falsey
end

it "returns false if the only submitted error is raised" do
expect(matcher.match_unless_raises(RuntimeError){ raise "foo" }).to be_false
expect(matcher.match_unless_raises(RuntimeError){ raise "foo" }).to be_falsey
end

it "returns false if any of several errors submitted is raised" do
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }).to be_false
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }).to be_false
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }).to be_false
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise "foo" }).to be_falsey
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise ArgumentError.new('') }).to be_falsey
expect(matcher.match_unless_raises(RuntimeError, ArgumentError, NameError) { raise NameError.new('') }).to be_falsey
end

it "re-raises any error other than one of those specified" do
Expand Down Expand Up @@ -51,10 +51,10 @@ def matches?(actual)
end
end

expect(matcher.new(3).matches?(3)).to be_true
expect(matcher.new(3).matches?(3)).to be_truthy
expect(matcher.new(3)).to eq(3)

expect(matcher.new(3).matches?(4)).to be_false
expect(matcher.new(3).matches?(4)).to be_falsey
expect(matcher.new(3)).not_to eq(4)
end
end
Expand Down
32 changes: 24 additions & 8 deletions spec/rspec/matchers/be_spec.rb
Expand Up @@ -235,34 +235,50 @@ def happy?
end
end

describe "expect(...).to be_true" do
describe "expect(...).to be_truthy" do
it "passes when actual equal?(true)" do
expect(true).to be_true
expect(true).to be_truthy
end

it "passes when actual is 1" do
expect(1).to be_true
expect(1).to be_truthy
end

it "fails when actual equal?(false)" do
expect {
expect(false).to be_true
expect(false).to be_truthy
}.to fail_with("expected: true value\n got: false")
end
end

describe "expect(...).to be_false" do
describe "expect(...).to be_falsey" do
it "passes when actual equal?(false)" do
expect(false).to be_false
expect(false).to be_falsey
end

it "passes when actual equal?(nil)" do
expect(nil).to be_false
expect(nil).to be_falsey
end

it "fails when actual equal?(true)" do
expect {
expect(true).to be_false
expect(true).to be_falsey
}.to fail_with("expected: false value\n got: true")
end
end

describe "expect(...).to be_falsy" do
it "passes when actual equal?(false)" do
expect(false).to be_falsy
end

it "passes when actual equal?(nil)" do
expect(nil).to be_falsy
end

it "fails when actual equal?(true)" do
expect {
expect(true).to be_falsy
}.to fail_with("expected: false value\n got: true")
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/rspec/matchers/description_generation_spec.rb
Expand Up @@ -25,14 +25,14 @@
expect(RSpec::Matchers.generated_description).to eq "should not be empty"
end

it "expect(...).to be true" do
expect(true).to be_true
expect(RSpec::Matchers.generated_description).to eq "should be true"
it "expect(...).to be truthy" do
expect(true).to be_truthy
expect(RSpec::Matchers.generated_description).to eq "should be truthy"
end

it "expect(...).to be false" do
expect(false).to be_false
expect(RSpec::Matchers.generated_description).to eq "should be false"
it "expect(...).to be falsey" do
expect(false).to be_falsey
expect(RSpec::Matchers.generated_description).to eq "should be falsey"
end

it "expect(...).to be nil" do
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/matchers/eq_spec.rb
Expand Up @@ -28,7 +28,7 @@ module Matchers
end.new

expect(actual).to eq :anything # to trigger the matches? method
expect(called).to be_true
expect(called).to be_truthy
end

it "describes itself" do
Expand Down

0 comments on commit 596b988

Please sign in to comment.