PureXML/Tests/XSLTApplyImportsTests.swift
Mihaela Mihaljevic 83e6e27d12 style(format): adopt --importgrouping testable-bottom from canonical rules-swift
Group @testable imports at the bottom, per public/rules-swift's swiftformat. A
behavior-preserving import-ordering reformat across 252 files (mostly tests).
--trailingclosures was deliberately not adopted (cosmetic, and swiftformat flags
it as caution-worthy).
2026-06-18 20:33:07 +02:00

52 lines
2.2 KiB
Swift

import Testing
@testable import PureXML
@Suite("XSLT xsl:apply-imports")
struct XSLTApplyImportsTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ stylesheet: String, _ source: String, loader: @escaping (String) -> String?) throws -> String {
try PureXML.XSLT.transform(stylesheet: stylesheet, source: source, documentLoader: loader)
}
@Test("apply-imports invokes the lower-precedence imported template")
func test_applyImports() throws {
let base = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:template match="item">[base:<xsl:value-of select="."/>]</xsl:template>
</xsl:stylesheet>
"""
let main = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:import href="base.xsl"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:apply-templates select="r/item"/></out></xsl:template>
<xsl:template match="item">main(<xsl:apply-imports/>)</xsl:template>
</xsl:stylesheet>
"""
let output = try transform(main, "<r><item>x</item></r>") { $0 == "base.xsl" ? base : nil }
#expect(output == "<out>main([base:x])</out>")
}
@Test("apply-imports with no lower-precedence match falls back to the built-in rule")
func test_applyImportsBuiltIn() throws {
let base = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:template match="other">never</xsl:template>
</xsl:stylesheet>
"""
let main = """
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:import href="base.xsl"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/"><out><xsl:apply-templates select="r/item"/></out></xsl:template>
<xsl:template match="item">main[<xsl:apply-imports/>]</xsl:template>
</xsl:stylesheet>
"""
// No imported template matches "item", so apply-imports uses the built-in
// rule, which outputs the element's text value.
let output = try transform(main, "<r><item>y</item></r>") { $0 == "base.xsl" ? base : nil }
#expect(output == "<out>main[y]</out>")
}
}