import Testing
@testable import PureXML
/// The instance-validity derivation/substitution subsystem keys its `block`,
/// abstract-type, and derivation-backbone tables by namespaced identity
/// (`{ns}local`), so two complex types sharing a local name in different
/// namespaces do not collide. Without this, a `blockDefault` in one namespace
/// would wrongly block an `xsi:type` substitution among the same-named types of
/// another namespace (a false rejection).
@Suite("namespaced derivation keys do not cross-block")
struct SchemaNamespacedDerivationTests {
/// Schema A (`urn:a`, `blockDefault="#all"`) imports schema B (`urn:b`, no
/// block). Both declare a complex type `T`, a `TD` extending their own `T`, and
/// an element `e` typed by their own `T`.
private let schemaA = """
"""
private let schemaB = """
"""
private var loader: (String) -> String? {
let schemaB = schemaB
return { $0 == "b.xsd" ? schemaB : nil }
}
@Test("an extension xsi:type in urn:b is not blocked by urn:a's blockDefault")
func test_otherNamespaceNotBlocked() throws {
let document = try PureXML.Schema.Document(schemaA, schemaLoader: loader)
let instance = """
ab
"""
#expect(try document.validate(instance, schemaLoader: loader).isEmpty)
}
@Test("the same extension xsi:type in urn:a IS blocked by its own blockDefault")
func test_ownNamespaceBlocked() throws {
let document = try PureXML.Schema.Document(schemaA, schemaLoader: loader)
let instance = """
ab
"""
#expect(try !document.validate(instance, schemaLoader: loader).isEmpty)
}
/// The streaming driver resolves `xsi:type` through its in-scope prefix bindings,
/// not the bare local name (#186). With the namespaced type table keyed by
/// `{ns}local`, a bare `"TD"` lookup misses, so the old streaming path reported a
/// false "unknown xsi:type"; the prefixed `b:TD` must resolve to urn:b's type and
/// agree with the tree path.
@Test("streaming xsi:type resolves by namespace and agrees with the tree path (#186)")
func test_streamingNamespacedXsiType() throws {
let document = try PureXML.Schema.Document(schemaA, schemaLoader: loader)
let valid = """
ab
"""
let tree = try document.validate(valid, schemaLoader: loader).map(\.reason).sorted()
let streamed = try document.validate(streaming: valid).map(\.reason).sorted()
#expect(tree.isEmpty, "tree rejected a valid namespaced xsi:type: \(tree)")
#expect(streamed == tree, "streaming disagreed with tree: \(streamed)")
}
}