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

#initialize(components) ⇒ MultipleInputMatcher

Initializes a matcher that matches multiple input elements.

Parameters:

  • components (Hash)

    A hash of matchers. The keys should be the keys used in Rails’ multi-parameter assignment, e.g., "1i", "2s", etc, and the values are the matchers that must be satisfied.



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

#failure_messageString

Returns the failure messages from each failed matcher.

Returns:

  • (String)

    Failure message



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

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

#for(*args) ⇒ MultipleInputMatcher

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)")

Parameters:

  • args (Array, Hash)

    A hierarchy of string that specify the attribute name.

Returns:



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

#matches?(rendered) ⇒ Boolean

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.

Parameters:

  • rendered (String)

    A string of HTML or an Object whose to_s method returns HTML. (That includes Nokogiri classes.)

Returns:

  • (Boolean)


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

#negative_failure_messageString

Returns the negative failure messages from every matcher.

Returns:

  • (String)

    Negative failure message



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