Skip to content

Commit

Permalink
Update the yield cukes with a few more/better examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Apr 11, 2012
1 parent f8c3c01 commit d020cfe
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions features/built_in_matchers/yield.feature
Expand Up @@ -22,16 +22,35 @@ Feature: yield matchers
to detect whether or not your method yields, and, if so, how many times and what to detect whether or not your method yields, and, if so, how many times and what
the yielded arguments are. the yielded arguments are.


Background:
Given a file named "my_class.rb" with:
"""
class MyClass
def self.yield_once_with(*args)
yield *args
end
def self.raw_yield
yield
end
def self.dont_yield
end
end
"""

Scenario: yield_control matcher Scenario: yield_control matcher
Given a file named "yield_control_spec.rb" with: Given a file named "yield_control_spec.rb" with:
""" """
require './my_class'
describe "yield_control matcher" do describe "yield_control matcher" do
specify { expect { |b| File.open("temp", "w", &b) }.to yield_control } specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control }
specify { expect { |b| :foo.to_s(&b) }.not_to yield_control } specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control }
# deliberate failures # deliberate failures
specify { expect { |b| :foo.to_s(&b) }.to yield_control } specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control }
specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_control } specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control }
end end
""" """
When I run `rspec yield_control_spec.rb` When I run `rspec yield_control_spec.rb`
Expand All @@ -43,51 +62,58 @@ Feature: yield matchers
Scenario: yield_with_args matcher Scenario: yield_with_args matcher
Given a file named "yield_with_args_spec.rb" with: Given a file named "yield_with_args_spec.rb" with:
""" """
require './my_class'
describe "yield_with_args matcher" do describe "yield_with_args matcher" do
specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_args } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args }
specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_args(File) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args("foo") }
specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args("17") } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(String) }
specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args(/1\d/) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(/oo/) }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args("foo", "bar") }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(String, String) }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(/fo/, /ar/) }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args(17, "baz") }
# deliberate failures # deliberate failures
specify { expect { |b| :foo.to_s(&b) }.to yield_with_args(3, 4) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args }
specify { expect { |b| "Foo 17".sub(/\d+/, &b) }.to yield_with_args("18") } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args("foo") }
specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_args(File) } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(String) }
specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_args } specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(/oo/) }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args("foo", "bar") }
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(17, "baz") }
end end
""" """
When I run `rspec yield_with_args_spec.rb` When I run `rspec yield_with_args_spec.rb`
Then the output should contain all of these: Then the output should contain all of these:
| 8 examples, 4 failures | | 14 examples, 6 failures |
| expected given block to yield with arguments, but did not yield |
| expected given block to yield with arguments, but yielded with unexpected arguments |
| expected given block not to yield with arguments, but yielded with expected arguments |
| expected given block not to yield with arguments, but did | | expected given block not to yield with arguments, but did |
| expected given block not to yield with arguments, but yielded with expected arguments |
| expected given block to yield with arguments, but yielded with unexpected arguments |


Scenario: yield_with_no_args matcher Scenario: yield_with_no_args matcher
Given a file named "yield_with_no_args_spec.rb" with: Given a file named "yield_with_no_args_spec.rb" with:
""" """
def raw_yield require './my_class'
yield
end
describe "yield_with_no_args matcher" do describe "yield_with_no_args matcher" do
specify { expect { |b| raw_yield(&b) }.to yield_with_no_args } specify { expect { |b| MyClass.raw_yield(&b) }.to yield_with_no_args }
specify { expect { |b| :foo.to_s(&b) }.not_to yield_with_no_args } specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_with_no_args }
specify { expect { |b| File.open("temp", "w", &b) }.not_to yield_with_no_args } specify { expect { |b| MyClass.yield_once_with("a", &b) }.not_to yield_with_no_args }
# deliberate failures # deliberate failures
specify { expect { |b| raw_yield(&b) }.not_to yield_with_no_args } specify { expect { |b| MyClass.raw_yield(&b) }.not_to yield_with_no_args }
specify { expect { |b| :foo.to_s(&b) }.to yield_with_no_args } specify { expect { |b| MyClass.dont_yield(&b) }.to yield_with_no_args }
specify { expect { |b| File.open("temp", "w", &b) }.to yield_with_no_args } specify { expect { |b| MyClass.yield_once_with("a", &b) }.to yield_with_no_args }
end end
""" """
When I run `rspec yield_with_no_args_spec.rb` When I run `rspec yield_with_no_args_spec.rb`
Then the output should contain all of these: Then the output should contain all of these:
| 6 examples, 3 failures | | 6 examples, 3 failures |
| expected given block not to yield with no arguments, but did | | expected given block not to yield with no arguments, but did |
| expected given block to yield with no arguments, but did not yield | | expected given block to yield with no arguments, but did not yield |
| expected given block to yield with no arguments, but yielded with arguments: [#<File:temp (closed)>] | | expected given block to yield with no arguments, but yielded with arguments: ["a"] |


Scenario: yield_successive_args matcher Scenario: yield_successive_args matcher
Given a file named "yield_successive_args_spec.rb" with: Given a file named "yield_successive_args_spec.rb" with:
Expand Down

0 comments on commit d020cfe

Please sign in to comment.