PureXML/Tests/SerializerTests.swift
Mihaela Mihaljevic 708390242c chore: scaffold PureXML pure-Swift XML package
Root Swift package mirroring PureYAML's structure, rules, and verification
gates. Includes the XML node model (Node, Element, Attribute, QualifiedName),
a working emitter (Serializer) with escaping and pretty/compact options, and
structural validation. The parser surface is stable; parse() raises
notImplemented until the scanner lands, pinned by a test.

- dependency-free, namespaced under PureXML
- portability and style gates carried over (style, namespacing, forbidden
  patterns, swiftformat, swiftlint, linux, wasm, windows CI)
- 11 tests passing
2026-06-07 23:20:20 +02:00

41 lines
1.5 KiB
Swift

@testable import PureXML
import Testing
@Suite("Serializer")
struct SerializerTests {
@Test("Empty element self-closes")
func test_emptyElementSelfCloses() {
let node = PureXML.Model.Node.element(.init("br"))
#expect(PureXML.serialize(node, options: .compact) == "<br/>")
}
@Test("Attributes and text are escaped")
func test_escapesAttributesAndText() {
let element = PureXML.Model.Element(
"a",
attributes: [.init("href", "x?b=1&c=2")],
children: [.text("1 < 2")],
)
let xml = PureXML.serialize(.element(element), options: .compact)
#expect(xml == "<a href=\"x?b=1&amp;c=2\">1 &lt; 2</a>")
}
@Test("Pretty printing indents nested elements")
func test_prettyPrintsNested() {
let inner = PureXML.Model.Element("title", children: [.text("Guide")])
let outer = PureXML.Model.Element("book", children: [.element(inner)])
let xml = PureXML.serialize(.element(outer))
#expect(xml == "<book>\n <title>Guide</title>\n</book>\n")
}
@Test("CDATA and comments round-trip verbatim")
func test_emitsCDATAAndComments() {
#expect(PureXML.serialize(.cdata("a<b")) == "<![CDATA[a<b]]>")
#expect(PureXML.serialize(.comment(" note ")) == "<!-- note -->")
#expect(
PureXML.serialize(.processingInstruction(target: "xml-stylesheet", data: "href=\"s.css\""))
== "<?xml-stylesheet href=\"s.css\"?>",
)
}
}