@testable import PureXML
import Testing
@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 {
try PureXML.XSLT.transform(stylesheet: stylesheet, source: source)
}
@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("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")
}
}