2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2013-02-24 08:10:46 -05:00
|
|
|
require_relative 'rexml_test_utils'
|
2010-09-18 04:34:37 -04:00
|
|
|
|
|
|
|
require "rexml/document"
|
|
|
|
require "rexml/xpath"
|
|
|
|
|
|
|
|
# Harness to test REXML's capabilities against the test suite from Jaxen
|
|
|
|
# ryan.a.cox@gmail.com
|
|
|
|
|
2014-05-27 08:07:40 -04:00
|
|
|
module REXMLTests
|
2014-05-27 09:10:55 -04:00
|
|
|
class JaxenTester < Test::Unit::TestCase
|
|
|
|
include REXMLTestUtils
|
|
|
|
include REXML
|
2010-09-18 04:34:37 -04:00
|
|
|
|
2018-04-01 12:17:52 -04:00
|
|
|
def test_axis ; process_test_case("axis") ; end
|
2018-04-03 11:51:08 -04:00
|
|
|
def test_basic ; process_test_case("basic") ; end
|
|
|
|
def test_basicupdate ; process_test_case("basicupdate") ; end
|
|
|
|
def test_contents ; process_test_case("contents") ; end
|
|
|
|
def test_defaultNamespace ; process_test_case("defaultNamespace") ; end
|
|
|
|
def test_fibo ; process_test_case("fibo") ; end
|
2018-04-03 23:27:20 -04:00
|
|
|
def test_id ; process_test_case("id") ; end
|
|
|
|
def test_jaxen24 ; process_test_case("jaxen24") ; end
|
|
|
|
def test_lang ; process_test_case("lang") ; end
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_message ; process_test_case("message") ; end
|
|
|
|
def _test_moreover ; process_test_case("moreover") ; end
|
|
|
|
def _test_much_ado ; process_test_case("much_ado") ; end
|
|
|
|
def _test_namespaces ; process_test_case("namespaces") ; end
|
|
|
|
def _test_nitf ; process_test_case("nitf") ; end
|
|
|
|
def _test_numbers ; process_test_case("numbers") ; end
|
|
|
|
def _test_pi ; process_test_case("pi") ; end
|
|
|
|
def _test_pi2 ; process_test_case("pi2") ; end
|
|
|
|
def _test_simple ; process_test_case("simple") ; end
|
|
|
|
def _test_testNamespaces ; process_test_case("testNamespaces") ; end
|
|
|
|
def _test_text ; process_test_case("text") ; end
|
|
|
|
def _test_underscore ; process_test_case("underscore") ; end
|
|
|
|
def _test_web ; process_test_case("web") ; end
|
|
|
|
def _test_web2 ; process_test_case("web2") ; end
|
2010-09-18 04:34:37 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
private
|
2018-04-01 12:17:52 -04:00
|
|
|
def process_test_case(name)
|
|
|
|
xml_path = "#{name}.xml"
|
|
|
|
doc = File.open(fixture_path(xml_path)) do |file|
|
|
|
|
Document.new(file)
|
|
|
|
end
|
|
|
|
test_doc = File.open(fixture_path("test/tests.xml")) do |file|
|
|
|
|
Document.new(file)
|
|
|
|
end
|
|
|
|
XPath.each(test_doc,
|
|
|
|
"/tests/document[@url='xml/#{xml_path}']/context") do |context|
|
|
|
|
process_context(doc, context)
|
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
2010-09-18 04:34:37 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# processes a tests/document/context node
|
2018-04-01 12:17:52 -04:00
|
|
|
def process_context(doc, context)
|
|
|
|
test_context = XPath.match(doc, context.attributes["select"])
|
|
|
|
namespaces = context.namespaces
|
2014-05-27 09:10:55 -04:00
|
|
|
variables = {}
|
2018-04-01 12:17:52 -04:00
|
|
|
var_namespace = "http://jaxen.org/test-harness/var"
|
|
|
|
XPath.each(context,
|
|
|
|
"@*[namespace-uri() = '#{var_namespace}']") do |attribute|
|
|
|
|
variables[attribute.name] = attribute.value
|
|
|
|
end
|
|
|
|
XPath.each(context, "valueOf") do |value|
|
|
|
|
process_value_of(test_context, variables, namespaces, value)
|
|
|
|
end
|
|
|
|
XPath.each(context,
|
|
|
|
"test[not(@exception) or (@exception != 'true')]") do |test|
|
|
|
|
process_nominal_test(test_context, variables, namespaces, test)
|
|
|
|
end
|
|
|
|
XPath.each(context,
|
|
|
|
"test[@exception = 'true']") do |test|
|
|
|
|
process_exceptional_test(test_context, variables, namespaces, test)
|
|
|
|
end
|
2010-09-18 04:34:37 -04:00
|
|
|
end
|
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# processes a tests/document/context/valueOf or tests/document/context/test/valueOf node
|
2018-04-01 12:17:52 -04:00
|
|
|
def process_value_of(context, variables, namespaces, value_of)
|
|
|
|
expected = value_of.text
|
2018-04-03 11:51:08 -04:00
|
|
|
xpath = value_of.attributes["select"]
|
|
|
|
matched = XPath.first(context, xpath, namespaces, variables)
|
|
|
|
|
|
|
|
message = ""
|
|
|
|
context.each_with_index do |node, i|
|
|
|
|
message << "Node#{i}:\n"
|
|
|
|
message << node.to_s
|
|
|
|
end
|
|
|
|
message << "XPath: <#{xpath}>\n"
|
|
|
|
message << "Matched <#{matched.class}>"
|
|
|
|
|
2018-04-01 12:17:52 -04:00
|
|
|
if expected.nil?
|
2018-04-03 11:51:08 -04:00
|
|
|
assert_nil(matched, message)
|
2014-05-27 09:10:55 -04:00
|
|
|
else
|
2018-04-01 12:17:52 -04:00
|
|
|
case matched
|
|
|
|
when Element
|
2018-04-03 11:51:08 -04:00
|
|
|
assert_equal(expected, matched.text, message)
|
2018-04-01 12:17:52 -04:00
|
|
|
when Attribute, Text, Comment, TrueClass, FalseClass
|
2018-04-03 11:51:08 -04:00
|
|
|
assert_equal(expected, matched.to_s, message)
|
2018-04-01 12:17:52 -04:00
|
|
|
when Instruction
|
2018-04-03 11:51:08 -04:00
|
|
|
assert_equal(expected, matched.content, message)
|
|
|
|
when Integer, Float
|
|
|
|
assert_equal(expected.to_f, matched, message)
|
2018-04-01 12:17:52 -04:00
|
|
|
when String
|
2018-04-03 11:51:08 -04:00
|
|
|
assert_equal(expected, matched, message)
|
2018-04-01 12:17:52 -04:00
|
|
|
else
|
2018-04-03 11:51:08 -04:00
|
|
|
flunk("#{message}\n" +
|
|
|
|
"Unexpected match value: <#{matched.inspect}>")
|
2018-04-01 12:17:52 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
2010-09-18 04:34:37 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# processes a tests/document/context/test node ( where @exception is false or doesn't exist )
|
2018-04-01 12:17:52 -04:00
|
|
|
def process_nominal_test(context, variables, namespaces, test)
|
2018-04-03 23:27:20 -04:00
|
|
|
xpath = test.attributes["select"]
|
|
|
|
matched = XPath.match(context, xpath, namespaces, variables)
|
2014-05-27 09:10:55 -04:00
|
|
|
# might be a test with no count attribute, but nested valueOf elements
|
2018-04-01 12:17:52 -04:00
|
|
|
expected = test.attributes["count"]
|
|
|
|
if expected
|
|
|
|
assert_equal(Integer(expected, 10),
|
|
|
|
matched.size)
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2018-04-01 12:17:52 -04:00
|
|
|
XPath.each(test, "valueOf") do |value_of|
|
|
|
|
process_value_of(mathched, variables, namespaces, value_of)
|
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# processes a tests/document/context/test node ( where @exception is true )
|
2018-04-01 12:17:52 -04:00
|
|
|
def process_exceptional_test(context, variables, namespaces, test)
|
|
|
|
select = test.attributes["select"]
|
|
|
|
assert_raise do
|
|
|
|
XPath.match(context, select, namespaces, variables)
|
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-27 06:21:10 -04:00
|
|
|
end
|