Class: RspecTagMatchers::HaveTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_tag_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.



5
6
7
8
9
10
11
12
13
14
# File 'lib/rspec_tag_matchers/have_tag.rb', line 5

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



39
40
41
42
# File 'lib/rspec_tag_matchers/have_tag.rb', line 39

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)


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

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



44
45
46
47
# File 'lib/rspec_tag_matchers/have_tag.rb', line 44

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