import Testing
@testable import PureXML
@Suite("XSLT document() fragments and base-URI resolution")
struct XSLTDocumentFunctionTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ stylesheet: String, _ source: String, baseURI: String = "", loader: @escaping (String) -> String?) throws -> String {
try PureXML.XSLT.transform(stylesheet: stylesheet, source: source, documentLoader: loader, baseURI: baseURI)
}
private let external = ""
@Test("document() with no fragment loads the whole document")
func test_wholeDocument() throws {
let style = """
"""
let out = try transform(style, "") { $0 == "ext.xml" ? external : nil }
#expect(out == "2")
}
@Test("document() with an xpointer fragment selects a subset")
func test_xpointerFragment() throws {
let style = """
"""
let out = try transform(style, "") { $0 == "ext.xml" ? external : nil }
#expect(out == "second")
}
@Test("A relative document() URI is resolved against the base URI")
func test_baseURI() throws {
let style = """
"""
var requested: [String] = []
let out = try transform(style, "", baseURI: "http://example.org/docs/") { uri in
requested.append(uri)
return uri == "http://example.org/docs/ext.xml" ? external : nil
}
#expect(out == "2")
#expect(requested.contains("http://example.org/docs/ext.xml"))
}
@Test("document() with a node-set argument unions the loaded documents")
func test_nodeSetArgument() throws {
let style = """
"""
let source = "[a.xml][b.xml]"
let out = try transform(style, source) { uri in
switch uri {
case "a.xml": ""
case "b.xml": ""
default: nil
}
}
#expect(out == "3")
}
@Test("unparsed-entity-uri returns the system URI of a DTD unparsed entity")
func test_unparsedEntityURI() throws {
// XSLT 1.0 12.4: unparsed-entity-uri returns the URI of the named NDATA
// entity declared in the source DTD, or empty for an unknown name
// (Apache Xalan expression02).
let style = """
\
,
"""
let source = """
]>
"""
#expect(try transform(style, source, loader: { _ in nil }) == "images/hatch.gif,")
}
@Test("document('') is the stylesheet's own document")
func test_emptyReferenceIsStylesheet() throws {
// XSLT 1.0 12.1: an empty URI reference is the document containing the
// expression, so a stylesheet can read its own xsl:* nodes (Apache Xalan
// select67, select68, idkey50, mdocs17, namespace20).
let style = """
FOUND
"""
#expect(try transform(style, "", loader: { _ in nil }) == "FOUND")
}
}