Skip to content

Commit

Permalink
Adds custom matchers for multiple in(ex)clude
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Blaho committed Feb 11, 2013
1 parent 739a71d commit 5218f00
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
45 changes: 45 additions & 0 deletions spec/support/matchers/string_matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
RSpec::Matchers.define :include_all do |*expected|
include Utils::NiceJoin
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
end

RSpec::Matchers.define :exclude_all do |*expected|
include Utils::NiceJoin
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
end
10 changes: 10 additions & 0 deletions spec/support/utils/nice_join.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Utils
module NiceJoin
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
end

0 comments on commit 5218f00

Please sign in to comment.