Skip to content
Browse files

Merge pull request #187 from nanocity/yield_arguments

Use 'empty?' instead of 'any?' and 'none?' to match 'nil' and 'false'
  • Loading branch information...
2 parents 83d9cd2 + 3d9ae80 commit f8a806578b8ebd497fa9dce07adec25a64b00749 @alindeman alindeman committed
Showing with 40 additions and 5 deletions.
  1. +5 −5 lib/rspec/matchers/built_in/yield.rb
  2. +35 −0 spec/rspec/matchers/yield_spec.rb
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/)

0 comments on commit f8a8065

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