PureXML/Tests/XSLTDecimalFormatTests.swift
Mihaela Mihaljevic 38bbb349c5 feat(xslt): full format-number picture parsing
The picture parses into affixes, number part, and the negative
subpattern: literal prefixes and suffixes pass through, the negative
subpattern's affixes apply (with the default minus when it has no
distinguishing ones), per-mille and percent multiply and stay literal,
the grouping size derives from the pattern's last grouping separator,
colliding decimal/grouping separators read as grouping, and U+00A4
renders as the en-US currency symbol. Digit extraction now uses one
scaled rounding and integer arithmetic, eliminating per-digit float
drift. Xalan numberformat 30 -> 1; baseline 475 -> 446.
2026-06-10 12:45:57 +02:00

94 lines
4.0 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSLT xsl:decimal-format")
struct XSLTDecimalFormatTests {
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)
}
private func formatNumber(_ select: String, decimalFormats: String = "") -> String {
"""
<xsl:stylesheet version="1.0" \(xsl)>
<xsl:output method="text"/>
\(decimalFormats)
<xsl:template match="/"><xsl:value-of select="\(select)"/></xsl:template>
</xsl:stylesheet>
"""
}
@Test("The default format still works")
func test_default() throws {
#expect(try transform(formatNumber("format-number(1234.5, '#,##0.00')"), "<x/>") == "1,234.50")
}
@Test("A named European format swaps the decimal and grouping separators")
func test_european() throws {
let style = formatNumber(
"format-number(1234.5, '#.##0,00', 'eu')",
decimalFormats: "<xsl:decimal-format name=\"eu\" decimal-separator=\",\" grouping-separator=\".\"/>",
)
#expect(try transform(style, "<x/>") == "1.234,50")
}
@Test("A custom percent and minus sign are honored")
func test_percentMinus() throws {
let style = formatNumber(
"format-number(-0.25, '0%', 'p')",
decimalFormats: "<xsl:decimal-format name=\"p\" minus-sign=\"!\"/>",
)
#expect(try transform(style, "<x/>") == "!25%")
}
@Test("A custom NaN string is used for non-numbers")
func test_nan() throws {
let style = formatNumber(
"format-number(number('abc'), '0', 'n')",
decimalFormats: "<xsl:decimal-format name=\"n\" NaN=\"not-a-number\"/>",
)
#expect(try transform(style, "<x/>") == "not-a-number")
}
@Test("The unnamed decimal-format overrides the global default")
func test_unnamedDefault() throws {
let style = formatNumber(
"format-number(5, '0')",
decimalFormats: "<xsl:decimal-format zero-digit=\"0\" minus-sign=\"~\"/>",
)
// 5 formats plainly; a negative shows the overridden minus sign.
#expect(try transform(style, "<x/>") == "5")
let neg = formatNumber("format-number(-5, '0')", decimalFormats: "<xsl:decimal-format minus-sign=\"~\"/>")
#expect(try transform(neg, "<x/>") == "~5")
}
@Test("Literal affixes pass through around the number part")
func test_affixes() throws {
#expect(try transform(formatNumber("format-number(95.4857, 'PRE###.####SUF')"), "<x/>") == "PRE95.4857SUF")
#expect(try transform(formatNumber("format-number(26931.4, '+#,###.#')"), "<x/>") == "+26,931.4")
}
@Test("The negative subpattern supplies its affixes; without distinguishing ones the minus sign applies")
func test_negativeSubpattern() throws {
#expect(try transform(formatNumber("format-number(-26931.4, '#,###.#;(#,###.#)')"), "<x/>") == "(26,931.4)")
#expect(try transform(formatNumber("format-number(-26931.4, '#,###.#;#,###.#')"), "<x/>") == "-26,931.4")
#expect(try transform(formatNumber("format-number(-87504.4812, '000,000.000###')"), "<x/>") == "-087,504.4812")
}
@Test("Per-mille multiplies by a thousand and keeps the symbol")
func test_perMille() throws {
#expect(try transform(formatNumber("format-number(0.4857, '###.###\u{2030}')"), "<x/>") == "485.7\u{2030}")
}
@Test("The grouping size comes from the pattern's last grouping separator")
func test_groupingSize() throws {
#expect(try transform(formatNumber("format-number(987654321, '###,##0,00.00')"), "<x/>") == "9,87,65,43,21.00")
}
@Test("Fraction digits render exactly, without float drift")
func test_fractionExact() throws {
#expect(try transform(formatNumber("format-number(87504.4812, '000,000.000###')"), "<x/>") == "087,504.4812")
}
}