Class: SwissKnife::RSpec::Matchers::HaveTag

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, selector, options = {}, &block) ⇒ HaveTag

Returns a new instance of HaveTag.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/swiss_knife/rspec/have_tag.rb', line 15

def initialize(type, selector, options = {}, &block)
  @selector = selector.to_s
  @type = type

  case options
  when Hash
    @options = options
  when Numeric
    @options = {:count => options}
  else
    @options = {:text => options}
  end
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def actual
  @actual
end

#actual_countObject (readonly)

Returns the value of attribute actual_count.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def actual_count
  @actual_count
end

#docObject (readonly)

Returns the value of attribute doc.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def doc
  @doc
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def options
  @options
end

#selectorObject (readonly)

Returns the value of attribute selector.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def selector
  @selector
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/swiss_knife/rspec/have_tag.rb', line 13

def type
  @type
end

Instance Method Details

#descriptionObject



58
59
60
# File 'lib/swiss_knife/rspec/have_tag.rb', line 58

def description
  "have tag #{selector.inspect} with #{options.inspect}"
end

#doc_for(input) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/swiss_knife/rspec/have_tag.rb', line 29

def doc_for(input)
  engine = type == :xml ? Nokogiri::XML : Nokogiri::HTML

  if input.respond_to?(:body)
    engine.parse(input.body.to_s)
  elsif Nokogiri::XML::Element === input
    input
  else
    engine.parse(input.to_s)
  end
end

#failure_messageObject



62
63
64
65
# File 'lib/swiss_knife/rspec/have_tag.rb', line 62

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

#matches?(actual, &block) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/swiss_knife/rspec/have_tag.rb', line 41

def matches?(actual, &block)
  @actual = actual
  @doc = doc_for(actual)

  matches = doc.css(selector)

  return options[:count] == 0 if matches.empty?
  matches = filter_on_inner_text(matches) if options[:text]
  matches = filter_on_nested_expectations(matches, block) if block

  @actual_count = matches.size

  return false if not acceptable_count?(actual_count)

  !matches.empty?
end

#negative_failure_messageObject



67
68
69
70
# File 'lib/swiss_knife/rspec/have_tag.rb', line 67

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