Class: RoadForest::Testing::HaveXpath

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/test-support/matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xpath, value, trace) ⇒ HaveXpath

Returns a new instance of HaveXpath.



115
116
117
# File 'lib/roadforest/test-support/matchers.rb', line 115

def initialize(xpath, value, trace)
  @xpath, @value, @trace = xpath, value, trace
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



118
119
120
# File 'lib/roadforest/test-support/matchers.rb', line 118

def actual
  @actual
end

#foundObject

Returns the value of attribute found.



119
120
121
# File 'lib/roadforest/test-support/matchers.rb', line 119

def found
  @found
end

#traceObject (readonly)

Returns the value of attribute trace.



118
119
120
# File 'lib/roadforest/test-support/matchers.rb', line 118

def trace
  @trace
end

#valueObject (readonly)

Returns the value of attribute value.



118
119
120
# File 'lib/roadforest/test-support/matchers.rb', line 118

def value
  @value
end

#xpathObject (readonly)

Returns the value of attribute xpath.



118
119
120
# File 'lib/roadforest/test-support/matchers.rb', line 118

def xpath
  @xpath
end

Instance Method Details

#descriptionObject



121
122
123
# File 'lib/roadforest/test-support/matchers.rb', line 121

def description
  "should match #{@xpath.inspect}"
end

#failure_message_for_shouldObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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_notObject



159
160
161
162
163
# 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) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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