Class: Matchers::XML::XPathContainsText

Inherits:
Object
  • Object
show all
Defined in:
lib/matchers/xml/xpath_contains_text.rb

Instance Method Summary collapse

Constructor Details

#initialize(xpath_expr_s, search_term) ⇒ XPathContainsText

Returns a new instance of XPathContainsText.

Parameters:

  • xpath_expr_s (String)
    • the XPath expression

  • search_term (String | Regexp)
    • the search term



20
21
22
23
# File 'lib/matchers/xml/xpath_contains_text.rb', line 20

def initialize(xpath_expr_s, search_term)
  @xpath_expr_s = xpath_expr_s
  @search_term = search_term
end

Instance Method Details

#matches?(document_s) ⇒ Boolean

Parameters:

  • document_s (String)
    • the XML document given as String

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/matchers/xml/xpath_contains_text.rb', line 26

def matches?(document_s)
  xml_doc = REXML::Document.new(document_s)
  xpath = REXML::XPath.first(xml_doc, @xpath_expr_s)
  return false if xpath.nil?
  return @search_term.match(xpath.text) if @search_term.class == Regexp
  xpath.text.include?(@search_term)
end