Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created January 29, 2013 17:56
Show Gist options
  • Select an option

  • Save jimweirich/4666180 to your computer and use it in GitHub Desktop.

Select an option

Save jimweirich/4666180 to your computer and use it in GitHub Desktop.
Exploring several alternatives to iterating over a Then block.
$ 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
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