Module: RSpec::Matchers::AllBe
- Defined in:
- lib/rspec-collection/all_be.rb
Overview
Helper functions gathered here to avoid global name space pollution.
Class Method Summary collapse
-
.format_predicate(pred, *args) ⇒ Object
Format a predicate function (with option arguments) for human readability.
-
.make_matcher(condition_string, &block) ⇒ Object
Create a collection matcher using
blockas the matching condition.
Class Method Details
.format_predicate(pred, *args) ⇒ Object
Format a predicate function (with option arguments) for human readability.
28 29 30 31 32 33 |
# File 'lib/rspec-collection/all_be.rb', line 28 def format_predicate(pred, *args) = ["be #{pred.gsub(/_/, ' ')}"] + args.map { |a| a.inspect } .join(' ') end |
.make_matcher(condition_string, &block) ⇒ Object
Create a collection matcher using block as the matching
condition. Block should take one or two arguments:
lambda { |element| ... }
lambda { |element, | ... }
If a block wishes to use custom failure messages, it should
append the message to the messages array. Otherwise we will
format an appropriate error message for each failing element.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rspec-collection/all_be.rb', line 45 def make_matcher(condition_string, &block) Matcher.new :all_be, block do |_block_| = [] @broken = [] match do |actual| actual.each do |element| unless _block_.call(element, ) @broken << element end end @broken.empty? end do |actual| = ["in #{actual.inspect}:"] if .empty? += @broken.map { |element| "expected #{element.inspect} to #{condition_string}" } else += end .join("\n") end do |actual| "expected #{actual.inspect} to not all #{condition_string}" end description do "all be" end end end |