import Testing
@testable import PureXML
/// XInclude and XPointer conformance, driven through the validation framework
/// (Tier 2): inclusion semantics, fallback, fragment selection, and the
/// XPointer schemes against their spec outcomes.
@Suite("Conformance corpus: XInclude and XPointer")
struct ConformanceXIncludeTests {
private let xiNamespace = "xmlns:xi=\"http://www.w3.org/2001/XInclude\""
private func included(_ xml: String, _ documents: [String: String]) throws -> String {
let node = try PureXML.XInclude.process(xml, base: "http://example.com/doc.xml", loadingURI: { documents[$0] })
return PureXML.serialize(node, options: .compact)
}
private func pointed(_ pointer: String, over xml: String) throws -> String {
try PureXML.XPointer.evaluate(pointer, over: PureXML.parse(xml)).map(\.stringValue).joined(separator: "|")
}
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))
}
// XInclude: substitution, text inclusion, fallback, fragment selection.
try add(
"include-xml-substitutes",
included("", ["http://example.com/f.xml": "hi"]),
"hi",
)
try add(
"include-text-escapes",
included("", ["http://example.com/f.txt": "aa<b&c",
)
try add(
"fallback-on-missing",
included("none", [:]),
"none",
)
try add(
"relative-href-resolves",
included("", ["http://example.com/sub/f.xml": ""]),
"",
)
// XPointer schemes over a fixed document.
let book = "firstsecond"
+ "third"
try add("shorthand-id", pointed("intro", over: book), "firstsecond")
try add("element-from-id", pointed("element(intro/2)", over: book), "second")
try add("element-from-root", pointed("element(/1/2)", over: book), "third")
// //para[1] binds the predicate per step: every para first in its parent
// (both chapters), the classic XPath semantic.
try add("xpointer-xpath-per-step-predicate", pointed("xpointer(//para[1])", over: book), "first|third")
try add("xpointer-xpath-document-first", pointed("xpointer((//para)[1])", over: book), "first")
try add("xpointer-count-attribute", pointed("xpointer(//chapter[@id='main']/para)", over: book), "third")
return cases
}
@Test("The XInclude/XPointer conformance corpus passes with no located failures")
func test_xincludeCorpusConforms() throws {
let failures = try PureXML.Validation.Conformance.failures(in: corpus())
#expect(failures.isEmpty, "\(failures.map(\.reason))")
}
}