Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use 'empty?' instead of 'any?' and 'none?' to match 'nil' and 'false' #187

Merged
merged 1 commit into from

3 participants

@nanocity

No description provided.

@alindeman
Collaborator

This looks good to me. @myronmarston, @dchelimsky, see any weaknesses?

@myronmarston
Owner

Looks great. Nice catch, @nanocity!

@alindeman alindeman merged commit f8a8065 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 9, 2012
  1. @nanocity

    Use 'empty?' instead of 'any?' and 'none?' in order to match 'nil' an…

    nanocity authored
    …d 'false' as argument values
This page is out of date. Refresh to see the latest.
View
10 lib/rspec/matchers/built_in/yield.rb
@@ -83,7 +83,7 @@ class YieldWithNoArgs < BaseMatcher
def matches?(block)
@probe = YieldProbe.probe(block)
- @probe.yielded_once?(:yield_with_no_args) && @probe.single_yield_args.none?
+ @probe.yielded_once?(:yield_with_no_args) && @probe.single_yield_args.empty?
end
def failure_message_for_should
@@ -127,7 +127,7 @@ def failure_message_for_should_not
def description
desc = "yield with args"
- desc << "(" + @expected.map { |e| e.inspect }.join(", ") + ")" if @expected.any?
+ desc << "(" + @expected.map { |e| e.inspect }.join(", ") + ")" unless @expected.empty?
desc
end
@@ -152,9 +152,9 @@ def negative_failure_reason
end
def args_match?
- if @expected.none? # expect {...}.to yield_with_args
- @positive_args_failure = "yielded with no arguments" if @actual.none?
- return @actual.any?
+ if @expected.empty? # expect {...}.to yield_with_args
+ @positive_args_failure = "yielded with no arguments" if @actual.empty?
+ return !@actual.empty?
end
unless match = all_args_match?
View
35 spec/rspec/matchers/yield_spec.rb
@@ -130,6 +130,12 @@ def each_arg(*args, &block)
}.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/)
end
+ it 'fails if the block yields with arg false' do
+ expect {
+ expect { |b| _yield_with_args(false, &b) }.to yield_with_no_args
+ }.to fail_with(/expected given block to yield with no arguments, but yielded with arguments/)
+ end
+
it 'raises an error if it yields multiple times' do
expect {
expect { |b| [1, 2].each(&b) }.to yield_with_no_args
@@ -179,6 +185,7 @@ def each_arg(*args, &block)
it 'has a description' do
yield_with_args.description.should eq("yield with args")
yield_with_args(1, 3).description.should eq("yield with args(1, 3)")
+ yield_with_args(false).description.should eq("yield with args(false)")
end
describe "expect {...}.to yield_with_args" do
@@ -273,6 +280,34 @@ def each_arg(*args, &block)
end
end
+ describe "expect {...}.to yield_with_args( false )" do
+ it 'passes if the block yields with the given arguments' do
+ expect { |b| _yield_with_args(false, &b) }.to yield_with_args(false)
+ end
+
+ it 'passes if the block yields with the given arguments using instance_eval' do
+ expect { |b| InstanceEvaler.new.yield_with_args(false, &b) }.to yield_with_args(false)
+ end
+
+ it 'fails if the block does not yield' do
+ expect {
+ expect { |b| _dont_yield(&b) }.to yield_with_args(false)
+ }.to fail_with(/expected given block to yield with arguments, but did not yield/)
+ end
+
+ it 'fails if the block yields with no arguments' do
+ expect {
+ expect { |b| _yield_with_no_args(&b) }.to yield_with_args(false)
+ }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
+ end
+
+ it 'fails if the block yields with different arguments' do
+ expect {
+ expect { |b| _yield_with_args(false, &b) }.to yield_with_args(true)
+ }.to fail_with(/expected given block to yield with arguments, but yielded with unexpected arguments/)
+ end
+ end
+
describe "expect {...}.to yield_with_args(/reg/, /ex/)" do
it "passes if the block yields strings matching the regexes" do
expect { |b| _yield_with_args("regular", "expression", &b) }.to yield_with_args(/reg/, /ex/)
Something went wrong with that request. Please try again.