class RoadForest::Testing::HaveXpath

Attributes

actual[R]
found[RW]
trace[R]
value[R]
xpath[R]

Public Class Methods

new(xpath, value, trace) click to toggle source
# File lib/roadforest/test-support/matchers.rb, line 115
def initialize(xpath, value, trace)
  @xpath, @value, @trace = xpath, value, trace
end

Public Instance Methods

description() click to toggle source
# File lib/roadforest/test-support/matchers.rb, line 121
def description
  "should match #{@xpath.inspect}"
end
failure_message_for_should() click to toggle source
# File lib/roadforest/test-support/matchers.rb, line 144
def failure_message_for_should
  msg =
    case value
    when true
      "expected that #{xpath.inspect} would be present\nwas:\n  #{found.inspect}\n"
    when false
      "expected that #{xpath.inspect} would be absent\nwas:\n  #{found.inspect}\n"
    else
      "expected that #{xpath.inspect} would be\n  #{value.inspect}\nwas:\n  #{found.inspect}\n"
    end
  msg += "in:\n" + actual.to_s
  msg +=  "\nDebug:#{trace.join("\n")}" if trace
  msg
end
failure_message_for_should_not() click to toggle source
# File lib/roadforest/test-support/matchers.rb, line 159
def failure_message_for_should_not
  msg = "expected that #{xpath.inspect} would not be #{value.inspect} in:\n" + actual.to_s
  msg +=  "\nDebug:#{trace.join("\n")}" if trace
  msg
end
matches?(actual) click to toggle source
# File lib/roadforest/test-support/matchers.rb, line 125
def matches?(actual)
  @actual = actual
  @doc = Nokogiri::HTML.parse(actual)
  @namespaces = @doc.namespaces.merge("xhtml" => "http://www.w3.org/1999/xhtml", "xml" => "http://www.w3.org/XML/1998/namespace")
  self.found = @doc.root.at_xpath(xpath, @namespaces)
  case value
  when false
    found.nil?
  when true
    !found.nil?
  when Array
    found.to_s.split(" ").include?(*value)
  when Regexp
    found.to_s =~ value
  else
    found.to_s == value
  end
end