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-04 02:53:57 -04:00
|
|
|
# document() function for XSLT isn't supported
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_message ; process_test_case("message") ; end
|
2018-04-04 02:53:57 -04:00
|
|
|
def test_moreover ; process_test_case("moreover") ; end
|
2018-04-22 05:38:06 -04:00
|
|
|
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
|
|
|
|
# Exception should be considered
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_numbers ; process_test_case("numbers") ; end
|
2018-04-21 02:39:43 -04:00
|
|
|
def test_pi ; process_test_case("pi") ; end
|
2018-04-21 02:45:42 -04:00
|
|
|
def test_pi2 ; process_test_case("pi2") ; end
|
2018-04-21 02:43:58 -04:00
|
|
|
def test_simple ; process_test_case("simple") ; end
|
2018-04-22 05:38:06 -04:00
|
|
|
# TODO: namespace node is needed
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_testNamespaces ; process_test_case("testNamespaces") ; end
|
2018-04-22 05:38:06 -04:00
|
|
|
# document() function for XSLT isn't supported
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_text ; process_test_case("text") ; end
|
2018-04-21 02:45:42 -04:00
|
|
|
def test_underscore ; process_test_case("underscore") ; end
|
2018-04-01 12:17:52 -04:00
|
|
|
def _test_web ; process_test_case("web") ; end
|
2018-04-21 02:45:42 -04:00
|
|
|
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
|
2018-04-22 04:09:04 -04:00
|
|
|
namespaces.delete("var")
|
|
|
|
namespaces = nil if namespaces.empty?
|
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"]
|
2018-04-27 21:36:18 -04:00
|
|
|
matched = XPath.match(context, xpath, namespaces, variables, strict: true)
|
2018-04-03 11:51:08 -04:00
|
|
|
|
2018-04-04 02:53:57 -04:00
|
|
|
message = user_message(context, xpath, matched)
|
2018-04-21 02:39:43 -04:00
|
|
|
assert_equal(expected || "",
|
|
|
|
REXML::Functions.string(matched),
|
|
|
|
message)
|
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"]
|
2018-04-27 21:36:18 -04:00
|
|
|
matched = XPath.match(context, xpath, namespaces, variables, strict: true)
|
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),
|
2018-04-04 02:53:57 -04:00
|
|
|
matched.size,
|
|
|
|
user_message(context, xpath, matched))
|
2018-04-01 12:17:52 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2018-04-01 12:17:52 -04:00
|
|
|
XPath.each(test, "valueOf") do |value_of|
|
2018-04-04 02:53:57 -04:00
|
|
|
process_value_of(matched, variables, namespaces, value_of)
|
2018-04-01 12:17:52 -04:00
|
|
|
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)
|
2018-04-21 02:21:41 -04:00
|
|
|
xpath = test.attributes["select"]
|
2018-04-21 02:21:02 -04:00
|
|
|
assert_raise(REXML::ParseException) do
|
2018-04-27 21:36:18 -04:00
|
|
|
XPath.match(context, xpath, namespaces, variables, strict: true)
|
2018-04-01 12:17:52 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
2018-04-04 02:53:57 -04:00
|
|
|
|
|
|
|
def user_message(context, xpath, matched)
|
|
|
|
message = ""
|
|
|
|
context.each_with_index do |node, i|
|
|
|
|
message << "Node#{i}:\n"
|
|
|
|
message << "#{node}\n"
|
|
|
|
end
|
|
|
|
message << "XPath: <#{xpath}>\n"
|
|
|
|
message << "Matched <#{matched}>"
|
|
|
|
message
|
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
end
|
2014-05-27 06:21:10 -04:00
|
|
|
end
|