forked from aeolus-incubator/thor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds nustom matchers for multiple in(ex)clude
prepares for aeolus-incubator#35 and aeolus-incubator#36
- Loading branch information
Petr Blaho
committed
Feb 11, 2013
1 parent
991889e
commit 5364e5e
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
RSpec::Matchers.define :include_all do |*expected| | ||
match do |actual| | ||
@missing = [] | ||
[ expected ].flatten.each do |expected_one| | ||
@missing << expected_one unless actual.include? expected_one | ||
end | ||
@missing.empty? | ||
end | ||
|
||
failure_message_for_should do |actual| | ||
"expected that '#{actual}' would include all of #{nice_join(expected)}\n it misses #{nice_join(@missing)}" | ||
end | ||
|
||
failure_message_for_should_not do |actual| | ||
"expected that '#{actual}' would not include all of #{nice_join(expected)}\n it misses #{nice_join(@missing)}" | ||
end | ||
|
||
description do | ||
"include all of #{nice_join(expected)}" | ||
end | ||
|
||
def nice_join(words, common_sep = ', ', last_sep = ' and ', quotes = "\"") | ||
unless words.empty? | ||
quoted_words = words.map{|word| quotes + word + quotes} | ||
quoted_words[0..-2].join(common_sep) + last_sep + quoted_words.last | ||
end | ||
end | ||
end | ||
|
||
RSpec::Matchers.define :exclude_all do |*expected| | ||
match do |actual| | ||
@redundant = [] | ||
[ expected ].flatten.each do |expected_one| | ||
@redundant << expected_one if actual.include? expected_one | ||
end | ||
@redundant.empty? | ||
end | ||
|
||
failure_message_for_should do |actual| | ||
"expected that '#{actual}' would exclude all of #{nice_join(expected)}\n it contains #{nice_join(@redundant)}" | ||
end | ||
|
||
failure_message_for_should_not do |actual| | ||
"expected that '#{actual}' would not exclude all of #{nice_join(expected)}\n it contains #{nice_join(@redundant)}" | ||
end | ||
|
||
description do | ||
"exclude all of #{nice_join(expected)}" | ||
end | ||
|
||
def nice_join(words, common_sep = ', ', last_sep = ' and ', quotes = "\"") | ||
unless words.empty? | ||
quoted_words = words.map{|word| quotes + word + quotes} | ||
quoted_words[0..-2].join(common_sep) + last_sep + quoted_words.last | ||
end | ||
end | ||
end |