import Testing
@testable import PureXML
@Suite("XSLT transform")
struct XSLTTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ stylesheet: String, _ source: String) throws -> String {
// These cases assert result markup, not the prolog: drop the
// spec-default XML declaration the xml method now writes.
let output = try PureXML.XSLT.transform(stylesheet: stylesheet, source: source)
guard output.hasPrefix("") else { return output }
var body = String(output[end.upperBound...])
if body.hasPrefix("\n") { body.removeFirst() }
return body
}
@Test("call-template passes with-param values, falling back to param defaults")
func test_callTemplateParams() throws {
let stylesheet = """
"""
#expect(try transform(stylesheet, "") == "Adaworld")
}
@Test("apply-templates passes parameters to the matched template")
func test_applyTemplatesParams() throws {
let stylesheet = """
"""
#expect(try transform(stylesheet, "ab") == "XaXb")
}
@Test("Modes route nodes to mode-specific templates")
func test_modes() throws {
let stylesheet = """
"""
let source = "AB"
#expect(try transform(stylesheet, source) == "AB
A
B
")
}
@Test("A mode is matched by expanded name: prefixes sharing a namespace name the same mode")
func test_modeMatchedByExpandedName() throws {
// foo and moo are both bound to urn:m, so apply-templates mode="foo:m"
// selects the template with mode="moo:m" (XSLT 1.0 5.7, Xalan modes16).
let stylesheet = """
matched
unmatched
"""
#expect(try transform(stylesheet, "") == "matched")
}
@Test("xsl:copy of the root with use-attribute-sets adds the attributes to the enclosing element")
func test_copyRootWithAttributeSets() throws {
// Copying the root makes no element of its own, so the attribute-set
// attributes join the enclosing (XSLT 1.0 7.5, Xalan attribset29).
let stylesheet = """
1
"""
#expect(try transform(stylesheet, "") == "")
}
@Test("xsl:element with an unusable name passes its content through")
func test_elementInvalidNamePassThrough() throws {
func out(_ name: String) throws -> String {
try transform("""
""", "")
}
// An undeclared prefix or non-QName name is unusable; the recovery emits
// the content without the wrapper element (XSLT 1.0 7.1.2, Xalan namespace40/43).
#expect(try out("none:foo") == "") // undeclared prefix
#expect(try out("this is bad") == "") // not an NCName
#expect(try out("good") == "") // a valid name still wraps
// A name using a non-ASCII NameChar (U+00B7 middle dot) is a valid XML
// name and must still wrap, not be dropped as unusable.
#expect(try out("a\u{B7}b") == "")
}
@Test("xsl:copy-of carries an element's inherited namespace nodes")
func test_copyOfInheritedNamespaces() throws {
// The copied inherits xmlns:ped from the root, with no ped-prefixed
// name of its own, so copy-of must still carry the ped namespace node
// (XSLT 1.0 11.3, Apache Xalan copy09).
let stylesheet = """
"""
let source = ""
#expect(try transform(stylesheet, source) == "")
}
@Test("xsl:processing-instruction and xsl:comment keep only text-node content")
func test_piCommentIgnoreNonTextContent() throws {
// Content that creates a non-text node (here a copied element) is ignored
// with its content, not flattened to its string value (XSLT 1.0 7.4/7.6,
// Apache Xalan copy60), so only the leading text node survives.
let stylesheet = """
"""
// doc = text "foo" then an element; the element node is dropped.
#expect(try transform(stylesheet, "foobar") == "")
}
@Test("xsl:attribute keeps only text-node content (errata E27)")
func test_attributeIgnoresNonTextContent() throws {
// a = "T1" + BBBB + "T2"; the element node is ignored with its
// content, so the attribute is "T1T2", not "T1BBBBT2" (Xalan copy58).
let stylesheet = """
"""
#expect(try transform(stylesheet, "T1BBBBT2") == "")
}
@Test("Whitespace around a CDATA section in a template is preserved")
func test_literalWhitespaceAroundCData() throws {
// Text and CDATA coalesce into one node, so " " is the
// non-whitespace node " test " and its spaces survive stripping, while a
// purely whitespace run between elements is still dropped (Xalan
// whitespace13).
let stylesheet = """
"""
#expect(try transform(stylesheet, "") == " test ")
}
@Test("A match pattern predicate sees top-level variables")
func test_patternReferencesGlobalVariable() throws {
// foo[. > $screen] matches only foo elements over the global threshold
// (XSLT 1.0 5.2: a pattern predicate sees the global variables; Xalan
// match14). Without them the predicate fails and nothing matches.
let stylesheet = """
:over,
:under,
"""
let source = "859"
#expect(try transform(stylesheet, source) == "a:over,b:under,c:over,")
}
@Test("The identity transform reproduces the input")
func test_identity() throws {
let stylesheet = """
"""
let source = "text"
#expect(try transform(stylesheet, source) == source)
}
@Test("value-of outputs the string value of an expression")
func test_valueOf() throws {
let stylesheet = """
"""
#expect(try transform(stylesheet, "Ada") == "Ada")
}
@Test("apply-templates dispatches to matching templates")
func test_applyTemplates() throws {
let stylesheet = """
"""
let source = "- a
- b
"
#expect(try transform(stylesheet, source) == "")
}
@Test("for-each iterates a node-set")
func test_forEach() throws {
let stylesheet = """
"""
let source = ""
#expect(try transform(stylesheet, source) == "12")
}
@Test("if and choose select conditionally")
func test_conditionals() throws {
let stylesheet = """
"""
let source = "5-3"
#expect(try transform(stylesheet, source) == "5")
}
@Test("xsl:attribute and xsl:element build the result dynamically")
func test_dynamicElement() throws {
let stylesheet = """
"""
#expect(try transform(stylesheet, "") == "")
}
@Test("xsl:sort orders the selected nodes")
func test_sort() throws {
let stylesheet = """
"""
let source = "312"
#expect(try transform(stylesheet, source) == "123")
}
@Test("Attribute value templates substitute expressions")
func test_attributeValueTemplate() throws {
let stylesheet = """
link
"""
#expect(try transform(stylesheet, "") == "link")
}
}