test(conformance): serializer options and HTML depth corpora
Deepen the two thin coverage zones (#119) through the validation-driven harness. Serializer options matrix (10 cases): self-close on/off, CDATA kept vs as-text, asciiOnly hex character references, round-trip carriage-return escaping, declaration with standalone, exact pretty-print shape, attribute escapes, and the Latin-1 output-encoding declaration via the byte path. HTML depth (8 cases): the adoption agency's furthest-block and deep-misnesting paths, foster-parented table text (with the implied tbody per the HTML5 algorithm), template content nesting through the document parser, and raw-text script, implied dt/dd and table-cell closes, and comments through the fragment parser. All conform; the one initial mismatch was a wrong expectation in the test (the implied tbody), not a code divergence. Closes #119
This commit is contained in:
parent
83f96d68a0
commit
f778cbbd08
@ -99,7 +99,7 @@ flowchart TB
|
||||
|
||||
PureXML is a working, dependency-free XML library today: parse, emit, validate,
|
||||
query, and stream documents on macOS, Linux, Windows, and WASM. The test suite
|
||||
currently runs **830 tests in 132 suites** (`swift test`).
|
||||
currently runs **832 tests in 134 suites** (`swift test`).
|
||||
|
||||
### Shipped (libxml2-aligned surface)
|
||||
|
||||
|
||||
56
Tests/ConformanceEmittingTests.swift
Normal file
56
Tests/ConformanceEmittingTests.swift
Normal file
@ -0,0 +1,56 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
/// Serializer options-matrix conformance, driven through the validation
|
||||
/// framework: each case pins the exact output of one option against the
|
||||
/// documented behavior (#119, the previously thin Emitting zone).
|
||||
@Suite("Conformance corpus: serializer options")
|
||||
struct ConformanceEmittingTests {
|
||||
private let compact = PureXML.Emitting.Options(prettyPrint: false)
|
||||
|
||||
private func emitted(_ xml: String, _ options: PureXML.Emitting.Options) throws -> String {
|
||||
try PureXML.serialize(PureXML.parse(xml), options: options)
|
||||
}
|
||||
|
||||
private func corpus() throws -> [PureXML.Validation.ConformanceCase] {
|
||||
var cases: [PureXML.Validation.ConformanceCase] = []
|
||||
func add(_ name: String, _ actual: String, _ expected: String) {
|
||||
cases.append(.init(name: name, actual: actual, expected: expected))
|
||||
}
|
||||
try add("self-close-default", emitted("<a/>", compact), "<a/>")
|
||||
var expanded = compact
|
||||
expanded.selfCloseEmptyElements = false
|
||||
try add("expanded-empty", emitted("<a/>", expanded), "<a></a>")
|
||||
try add("cdata-kept", emitted("<r><![CDATA[a<b]]></r>", compact), "<r><![CDATA[a<b]]></r>")
|
||||
var cdataText = compact
|
||||
cdataText.cdataAsText = true
|
||||
try add("cdata-as-text", emitted("<r><![CDATA[a<b]]></r>", cdataText), "<r>a<b</r>")
|
||||
var ascii = compact
|
||||
ascii.asciiOnly = true
|
||||
try add("ascii-only-hex-ncr", emitted("<r>\u{E9}\u{20AC}</r>", ascii), "<r>é€</r>")
|
||||
var roundTrip = compact
|
||||
roundTrip.textEscaping = .roundTrip
|
||||
// A literal carriage return survives serialization as a reference (the
|
||||
// parser would otherwise normalize it away); built programmatically since
|
||||
// parsing normalizes line endings first.
|
||||
let carriage = PureXML.serialize(.element(.init("r", children: [.text("a\rb")])), options: roundTrip)
|
||||
add("roundtrip-carriage-return", carriage, "<r>a
b</r>")
|
||||
var declared = compact
|
||||
declared.includeXMLDeclaration = true
|
||||
declared.standalone = true
|
||||
try add("declaration-standalone", emitted("<r/>", declared), "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<r/>")
|
||||
try add("pretty-deep-nesting", emitted("<a><b><c/></b></a>", .default), "<a>\n <b>\n <c/>\n </b>\n</a>\n")
|
||||
try add("attribute-escapes", emitted("<a t=\"a&b<c\"/>", compact), "<a t=\"a&b<c\"/>")
|
||||
// The byte path: a non-UTF-8 output encoding writes its canonical name in
|
||||
// the declaration (pure-ASCII content, so decoding as UTF-8 is faithful).
|
||||
let latin = try PureXML.serialize(PureXML.parse("<r>x</r>"), encoding: .latin1, options: compact)
|
||||
add("latin1-declaration", String(decoding: latin, as: UTF8.self), "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<r>x</r>")
|
||||
return cases
|
||||
}
|
||||
|
||||
@Test("The serializer options corpus passes with no located failures")
|
||||
func test_emittingCorpusConforms() throws {
|
||||
let failures = try PureXML.Validation.Conformance.failures(in: corpus())
|
||||
#expect(failures.isEmpty, "\(failures.map(\.reason))")
|
||||
}
|
||||
}
|
||||
50
Tests/ConformanceHTMLDepthTests.swift
Normal file
50
Tests/ConformanceHTMLDepthTests.swift
Normal file
@ -0,0 +1,50 @@
|
||||
@testable import PureXML
|
||||
import Testing
|
||||
|
||||
/// HTML5 depth conformance, driven through the validation framework (#119, the
|
||||
/// zone that produced #109): the spec-fidelity cases go through the document
|
||||
/// parser; the lenient-model cases through the fragment parser.
|
||||
@Suite("Conformance corpus: HTML depth")
|
||||
struct ConformanceHTMLDepthTests {
|
||||
/// The serialized body content of a full document parse.
|
||||
private func documentBody(_ html: String) -> String {
|
||||
let full = PureXML.HTML.serialize(PureXML.HTML.parseDocument(html))
|
||||
let wrapper = "<html><head></head><body>"
|
||||
guard full.hasPrefix(wrapper), full.hasSuffix("</body></html>") else { return full }
|
||||
return String(full.dropFirst(wrapper.count).dropLast("</body></html>".count))
|
||||
}
|
||||
|
||||
private func fragment(_ html: String) -> String {
|
||||
PureXML.HTML.serialize(PureXML.HTML.parse(html))
|
||||
}
|
||||
|
||||
private func corpus() -> [PureXML.Validation.ConformanceCase] {
|
||||
var cases: [PureXML.Validation.ConformanceCase] = []
|
||||
func document(_ name: String, _ input: String, _ expected: String) {
|
||||
cases.append(.init(name: name, actual: documentBody(input), expected: expected))
|
||||
}
|
||||
func lenient(_ name: String, _ input: String, _ expected: String) {
|
||||
cases.append(.init(name: name, actual: fragment(input), expected: expected))
|
||||
}
|
||||
// The adoption agency's furthest-block path and the deep misnesting case.
|
||||
document("adoption-furthest-block", "<b>1<p>2</b>3</p>", "<b>1</b><p><b>2</b>3</p>")
|
||||
document("adoption-deep-misnesting", "<p>1<b>2<i>3</b>4</i>5</p>", "<p>1<b>2<i>3</i></b><i>4</i>5</p>")
|
||||
// Foster parenting: stray table content surfaces before the table, and
|
||||
// the tr gets its implied tbody (both per the HTML5 algorithm).
|
||||
document("foster-parented-text", "<table>x<tr><td>a</td></tr></table>", "x<table><tbody><tr><td>a</td></tr></tbody></table>")
|
||||
// template keeps its flow content nested.
|
||||
document("template-content-nested", "<template><div>x</div></template>", "<template><div>x</div></template>")
|
||||
// Lenient fragment model: raw-text elements, implied closes.
|
||||
lenient("raw-text-script", "<script>if(a<b)x()</script>", "<script>if(a<b)x()</script>")
|
||||
lenient("implied-dt-dd-close", "<dl><dt>t<dd>d</dl>", "<dl><dt>t</dt><dd>d</dd></dl>")
|
||||
lenient("implied-cell-close", "<table><tr><td>a<td>b</table>", "<table><tr><td>a</td><td>b</td></tr></table>")
|
||||
lenient("comment-kept-in-flow", "<div><!-- note -->x</div>", "<div><!-- note -->x</div>")
|
||||
return cases
|
||||
}
|
||||
|
||||
@Test("The HTML depth corpus passes with no located failures")
|
||||
func test_htmlDepthCorpusConforms() {
|
||||
let failures = PureXML.Validation.Conformance.failures(in: corpus())
|
||||
#expect(failures.isEmpty, "\(failures.map(\.reason))")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user