import Testing
@testable import PureXML
@Suite("Line-ending normalization and XML 1.1")
struct LineEndingTests {
private func rootText(_ xml: String) throws -> String {
let node = try PureXML.parse(xml)
guard case let .document(children) = node, let root = children.compactMap(\.element).first else {
return ""
}
return root.children.reduce(into: "") { result, child in
if case let .text(text) = child {
result += text
}
}
}
@Test("CRLF is normalized to a single line feed")
func test_crlf() throws {
#expect(try rootText("x\r\ny") == "x\ny")
}
@Test("A lone carriage return is normalized to a line feed")
func test_loneCR() throws {
#expect(try rootText("x\ry") == "x\ny")
}
@Test("In XML 1.0, NEL is preserved as data")
func test_nel10() throws {
#expect(try rootText("x\u{85}y") == "x\u{85}y")
}
@Test("In XML 1.1, NEL is normalized to a line feed")
func test_nel11() throws {
#expect(try rootText("x\u{85}y") == "x\ny")
}
@Test("In XML 1.1, LINE SEPARATOR is normalized to a line feed")
func test_lineSeparator11() throws {
#expect(try rootText("x\u{2028}y") == "x\ny")
}
@Test("In XML 1.1, CR followed by NEL collapses to one line feed")
func test_crNel11() throws {
#expect(try rootText("x\r\u{85}y") == "x\ny")
}
@Test("In XML 1.0, LINE SEPARATOR is preserved as data")
func test_lineSeparator10() throws {
#expect(try rootText("x\u{2028}y") == "x\u{2028}y")
}
}