Class: RSpec::TagMatchers::MultipleInputMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/tag_matchers/multiple_input_matcher.rb

Overview

A matcher that matches multiple input elements. It is intended to serve as a base class for building more specific matchers that must match multiple input elements. For example, a matcher to test for Rails' time_select drop-downs must test that the HTML contains a drop-down for hours and another drop-down for minutes.

Examples:

Building a date matcher

matcher = MultipleInputMatcher.new(
    '1i' => HasSelect.new,
    '2i' => HasSelect.new,
    '3i' => HasSelect.new
  )
matcher.for(:user => :birthday) # will match <select> tags with names
                                # "user[birthday(1i)]", "user[birthday(2i)]", and
                                # "user[birthday(3i)]"

Building a time matcher

MultipleInputMatcher.new(       # by default, will match <select> tags with names by regular
    '4i' => HasSelect.new,      # expressions: /\(4i\)/ and /\(5i\)/
    '5i' => HasSelect.new
  )

Direct Known Subclasses

HasDateSelect, HasTimeSelect

Instance Method Summary (collapse)

Constructor Details

- (MultipleInputMatcher) initialize(components)

Initializes a matcher that matches multiple input elements.



29
30
31
32
33
34
# File 'lib/rspec/tag_matchers/multiple_input_matcher.rb', line 29

def initialize(components)
  @components = components
  @components.each do |key, matcher|
    matcher.with_attribute(:name => /\(#{key}\)/)
  end
end

Instance Method Details

- (String) failure_message

Returns the failure messages from each failed matcher.



82
83
84
# File 'lib/rspec/tag_matchers/multiple_input_matcher.rb', line 82

def failure_message
  @failures.map(&:failure_message).join(" and ")
end

- (MultipleInputMatcher) for(*args)

Specifies the inputs' names with more accuracy than the default regular expressions. It delegates to each matcher's for method. But it first appends the matcher's key to the last component of the input's name.

Examples:

Input naming delegation

hour_matcher   = HasSelect.new
minute_matcher = HasSelect.new
time_matcher   = MultipleInputMatcher.new('4i' => hour_matcher, '5i' => minute_matcher)

time_matcher.for(:event => :start_time) # calls hour_matcher.for("event", "start_time(4i)")
                                        # and minute_matcher.for("event", "start_time(5i)")


68
69
70
71
72
73
74
75
76
77
# File 'lib/rspec/tag_matchers/multiple_input_matcher.rb', line 68

def for(*args)
  @for = args.dup
  @for.extend(DeepFlattening)
  @for = @for.deep_flatten

  @components.each do |index, matcher|
    delegated_for(index, matcher, @for)
  end
  self
end

- (Boolean) matches?(rendered)

Tests whether the matcher matches the rendered string. It delegates matching to its matchers. It returns true if all of its matchers return true. It returns false if any of its matchers return false.



44
45
46
47
48
49
50
51
# File 'lib/rspec/tag_matchers/multiple_input_matcher.rb', line 44

def matches?(rendered)
  @rendered = rendered
  @failures = matchers.reject do |matcher|
    matcher.matches?(rendered)
  end

  @failures.empty?
end

- (String) negative_failure_message

Returns the negative failure messages from every matcher.



89
90
91
# File 'lib/rspec/tag_matchers/multiple_input_matcher.rb', line 89

def negative_failure_message
  matchers.map(&:negative_failure_message).join(" and ")
end