Class: RspecHpricotMatchers::HaveTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_hpricot_matchers/have_tag.rb

Instance Method Summary collapse

Constructor Details

#initialize(selector, inner_text_or_options, options, &block) ⇒ HaveTag

Returns a new instance of HaveTag.



3
4
5
6
7
8
9
10
11
12
# File 'lib/rspec_hpricot_matchers/have_tag.rb', line 3

def initialize(selector, inner_text_or_options, options, &block)
  @selector = selector
  if Hash === inner_text_or_options
    @inner_text = nil
    @options = inner_text_or_options
  else
    @inner_text = inner_text_or_options
    @options = options
  end
end

Instance Method Details

#failure_messageObject



37
38
39
40
# File 'lib/rspec_hpricot_matchers/have_tag.rb', line 37

def failure_message
  explanation = @actual_count ? "but found #{@actual_count}" : "but did not"
  "expected\n#{@hdoc.to_s}\nto have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}"
end

#matches?(actual, &block) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspec_hpricot_matchers/have_tag.rb', line 14

def matches?(actual, &block)
  @actual = actual
  @hdoc = hdoc_for(@actual)

  matched_elements = @hdoc.search(@selector)
  if matched_elements.empty?
    return @options[:count] == 0
  end

  if @inner_text
    matched_elements = filter_on_inner_text(matched_elements)
  end

  if block
    matched_elements = filter_on_nested_expectations(matched_elements, block)
  end

  @actual_count = matched_elements.length
  return false if not acceptable_count?(@actual_count)

  !matched_elements.empty?
end

#negative_failure_messageObject



42
43
44
45
# File 'lib/rspec_hpricot_matchers/have_tag.rb', line 42

def negative_failure_message
  explanation = @actual_count ? "but found #{@actual_count}" : "but did"
  "expected\n#{@hdoc.to_s}\nnot to have #{failure_count_phrase} #{failure_selector_phrase}, #{explanation}"
end