Created
January 29, 2013 17:56
-
-
Save jimweirich/4666180 to your computer and use it in GitHub Desktop.
Exploring several alternatives to iterating over a Then block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ rspec -fd thing_spec.rb | |
| thing | |
| matches thing a | |
| Then { subject.should include thing } | |
| matches thing b | |
| Then { subject.should include thing } | |
| matches thing c | |
| Then { subject.should include thing } | |
| matches thing d | |
| Then { subject.should include thing } | |
| matches thing e | |
| Then { subject.should include thing } | |
| Then { %w(a b c d e).all? { |s| subject.include?(s) } } | |
| Then { all_included_in?(subject, %w(a b c d e)) } | |
| Then { all_included_in?(subject, first_five_letters) } | |
| Then { included_letters.all_included_in?(subject) } | |
| Then { %w(a b c d e).should all_be_included_in subject } | |
| Then { first_five_letters.should all_be_included_in subject } | |
| Finished in 0.00445 seconds | |
| 16 examples, 0 failures |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rspec-given' | |
| RSpec::Given.use_natural_assertions | |
| # Exploring several alternatives to iterating over a Then block. | |
| describe "thing" do | |
| let(:subject){ "abcde" } | |
| %w(a b c d e).each do |thing| | |
| it "matches thing #{thing}" do | |
| subject.should include thing | |
| end | |
| Then { subject.should include thing } | |
| end | |
| Then { %w(a b c d e).all? { |s| subject.include?(s) } } | |
| Then { all_included_in?(subject, %w(a b c d e)) } | |
| def all_included_in?(sub, others) | |
| others.all? { |x| sub.include?(x) } | |
| end | |
| Given(:first_five_letters) { %w(a b c d e) } | |
| Then { all_included_in?(subject, first_five_letters) } | |
| Given(:included_letters) { Inclusion.new(%w(a b c d e)) } | |
| Then { included_letters.all_included_in?(subject) } | |
| Inclusion = Struct.new(:members) do | |
| def all_included_in?(obj) | |
| members.all? { |x| obj.include?(x) } | |
| end | |
| end | |
| matcher :all_be_included_in do |sub| | |
| match do |includes| | |
| includes.all? { |x| sub.include?(x) } | |
| end | |
| end | |
| Then { %w(a b c d e).should all_be_included_in subject } | |
| Then { first_five_letters.should all_be_included_in subject } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment