Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 9, 2012
Jump to file or symbol
Failed to load files and symbols.
+40 −5
Split
@@ -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?
@@ -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/)