Class: RSpecXML::XMLMatchers::HaveXPath::AttrMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AttrMatcher

Returns a new instance of AttrMatcher.



9
10
11
12
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 9

def initialize(options={})
  self.xpath = options[:xpath]
  self.attr = options[:attr]
end

Instance Method Details

#descriptionObject



24
25
26
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 24

def description
  "have xpath #{xpath} with attribute: #{attr}"
end

#failure_messageObject



28
29
30
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 28

def failure_message
  "expected #{xpath} to contain attribute: #{attr}"
end

#failure_message_when_negatedObject



32
33
34
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 32

def failure_message_when_negated
  "expected #{xpath} to not exist with attribute: #{attr}"
end

#matches?(xml) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 14

def matches?(xml)
  nodes = ::Nokogiri::XML(xml).xpath(xpath).to_a
  !nodes.empty? && nodes.any? do |node|
    attr.all? do |k, v|
      attr_value = node.attr(k.to_s)
      !attr_value.nil? && attr_value == v.to_s
    end
  end
end