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

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)
  message_elements =
    ["be #{pred.gsub(/_/, ' ')}"] +
    args.map { |a| a.inspect }
  message_elements.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, messages| ... }

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_|
    @failing_messages = []
    @broken = []
    match do |actual|
      actual.each do |element|
        unless _block_.call(element, @failing_messages)
          @broken << element
        end
      end
      @broken.empty?
    end

    failure_message_for_should do |actual|
      messages = ["in #{actual.inspect}:"]
      if @failing_messages.empty?
        messages += @broken.map { |element| "expected #{element.inspect} to #{condition_string}" }
      else
        messages += @failing_messages
      end
      messages.join("\n")
    end

    failure_message_for_should_not do |actual|
      "expected #{actual.inspect} to not all #{condition_string}"
    end

    description do
      "all be"
    end
  end
end