@testable import PureXML
import Testing
/// The full `xsl:number` of XSLT 1.0 section 7.7 (#130): level
/// single/multiple/any with count and from patterns, the format-token engine
/// with punctuation and separators, digit grouping, and the value expression.
@Suite("XSLT numbering")
struct XSLTNumberingTests {
private let xsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
private func transform(_ number: String, source: String) throws -> String {
let style = """
\(number)
"""
return try PureXML.XSLT.transform(stylesheet: style, source: source)
}
@Test("level=multiple numbers every matching ancestor-or-self, outermost first")
func test_levelMultiple() throws {
let source = """
"""
let result = try transform(
"",
source: source,
)
#expect(result == "1.1.1. 1.2.1. 1.2.2. 2.1.1. ")
}
@Test("level=any counts matching nodes before this one, bounded by from")
func test_levelAny() throws {
let source = ""
let across = try transform("", source: source)
#expect(across == "1 2 3 ")
let bounded = try transform("", source: source)
#expect(bounded == "1 2 1 ")
}
@Test("single/multiple with no matching node renders as the empty string")
func test_emptyMatchIsEmptyOutput() throws {
let result = try transform(
"",
source: "",
)
#expect(result == " ")
}
@Test("format tokens: padding, alphabetic, roman, punctuation, last-token reuse")
func test_formatTokens() {
#expect(XSLTNumbering.format([3], "(001) ") == "(003) ")
#expect(XSLTNumbering.format([2, 4], "A.i. ") == "B.iv. ")
#expect(XSLTNumbering.format([1, 2, 3], "1-1. ") == "1-2-3. ")
#expect(XSLTNumbering.format([1, 2, 3], "1") == "1.2.3")
#expect(XSLTNumbering.format([], "(1) ").isEmpty)
}
@Test("digit grouping applies the separator from the right")
func test_grouping() {
#expect(XSLTNumbering.format([1234], "1", (separator: ",", size: 3)) == "1,234")
#expect(XSLTNumbering.format([123], "1", (separator: ":", size: 2)) == "1:23")
#expect(XSLTNumbering.format([12], "1", (separator: ",", size: 3)) == "12")
}
@Test("value= evaluates the expression; below one it bypasses formatting")
func test_valueExpression() throws {
let formatted = try transform("", source: "")
#expect(formatted == "(VII) ")
let zero = try transform("", source: "")
#expect(zero == "0 ")
}
@Test("A value beyond 2^53 renders as a plain number without trapping")
func test_hugeValue() throws {
let result = try transform(
"",
source: "",
)
#expect(result == "1" + String(repeating: "0", count: 40) + " ")
}
}