require "helper"
class TestXsltTransforms < Nokogiri::TestCase
unless Nokogiri.uses_libxml? && Nokogiri::VERSION_INFO['libxml']['loaded'] < '2.6.16'
def test_class_methods
doc = Nokogiri::XML(File.read(XML_FILE))
style = Nokogiri::XSLT(File.read(XSLT_FILE))
assert result = style.apply_to(doc, ['title', '"Grandma"'])
assert_match %r{
Grandma
}, result
end
def test_transform
assert doc = Nokogiri::XML.parse(File.read(XML_FILE))
assert doc.xml?
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
assert result = style.apply_to(doc, ['title', '"Booyah"'])
assert_match %r{Booyah
}, result
assert_match %r{}, result
assert_match %r{}, result
assert_match %r{}, result
assert_match %r{}, result
assert_match %r{EMP0003 | }, result
assert_match %r{Margaret Martin | }, result
assert_match %r{Computer Specialist | }, result
assert_match %r{100,000 | }, result
assert_no_match %r{Dallas|Texas}, result
assert_no_match %r{Female}, result
assert result = style.apply_to(doc, ['title', '"Grandma"'])
assert_match %r{Grandma
}, result
assert result = style.apply_to(doc)
assert_match %r{}, result
end
def test_transform2
assert doc = Nokogiri::XML.parse(File.read(XML_FILE))
assert doc.xml?
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
assert result_doc = style.transform(doc)
assert result_doc.html?
assert_equal "", result_doc.at_css("h1").content
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
assert result_doc = style.transform(doc, ['title', '"Booyah"'])
assert result_doc.html?
assert_equal "Booyah", result_doc.at_css("h1").content
assert result_string = style.apply_to(doc, ['title', '"Booyah"'])
assert_equal result_string, result_doc.to_s
end
def test_transform_with_quote_params
doc = Nokogiri::XML.parse(File.read(XML_FILE))
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
assert result_doc = style.transform(doc, Nokogiri::XSLT.quote_params(['title', 'Booyah']))
assert result_doc.html?
assert_equal "Booyah", result_doc.at_css("h1").content
assert style = Nokogiri::XSLT.parse(File.read(XSLT_FILE))
assert result_doc = style.transform(doc, Nokogiri::XSLT.quote_params({'title' => 'Booyah'}))
assert result_doc.html?
assert_equal "Booyah", result_doc.at_css("h1").content
end
def test_quote_params
h = {
:sym => %{xxx},
'str' => %{"xxx"},
:sym2 => %{'xxx'},
'str2' => %{x'x'x},
:sym3 => %{x"x"x},
}
hh=h.dup
result_hash = Nokogiri::XSLT.quote_params(h)
assert_equal hh, h # non-destructive
a=h.to_a.flatten
result_array = Nokogiri::XSLT.quote_params(a)
assert_equal h.to_a.flatten, a #non-destructive
assert_equal result_array, result_hash
end
if Nokogiri.uses_libxml? # By now, cannot get it working on JRuby.
def test_exslt
assert doc = Nokogiri::XML.parse(File.read(EXML_FILE))
assert doc.xml?
assert style = Nokogiri::XSLT.parse(File.read(EXSLT_FILE))
params = {
:p1 => 'xxx',
:p2 => "x'x'x",
:p3 => 'x"x"x',
:p4 => '"xxx"'
}
result_doc = Nokogiri::XML.parse(style.apply_to(doc,
Nokogiri::XSLT.quote_params(params)))
assert_equal 'func-result', result_doc.at('/root/function').content
assert_equal 3, result_doc.at('/root/max').content.to_i
assert_match(
/\d{4}-\d\d-\d\d[-|+]\d\d:\d\d/,
result_doc.at('/root/date').content
)
result_doc.xpath('/root/params/*').each do |p|
assert_equal p.content, params[p.name.intern]
end
check_params result_doc, params
result_doc = Nokogiri::XML.parse(style.apply_to(doc,
Nokogiri::XSLT.quote_params(params.to_a.flatten)))
check_params result_doc, params
end
end
def test_xslt_parse_error
xslt_str = <<-EOX
}
EOX
assert_raises(RuntimeError) { Nokogiri::XSLT.parse(xslt_str) }
end
def check_params result_doc, params
result_doc.xpath('/root/params/*').each do |p|
assert_equal p.content, params[p.name.intern]
end
end
end
end