format-number now honors named and default decimal formats: the decimal separator, grouping separator, percent, minus sign, zero-digit (for non-Latin digit sets), NaN, and infinity symbols come from the chosen xsl:decimal-format, in both the picture and the output. FormatNumber is parameterized over the symbol set and the formats thread through the function table; folded across include/import.
66 lines
2.5 KiB
Swift
66 lines
2.5 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")
|
|
}
|
|
}
|