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



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

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

#failure_messageObject



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

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

#failure_message_when_negatedObject



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

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
# File 'lib/rspec-xml/xml_matchers/have_xpath/attr_matcher.rb', line 14

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