import Testing
@testable import PureXML
/// Unique Particle Attribution (cos-nonambig, XSD 1.0 3.8.6): a content model
/// must be deterministic, so each element in an instance is attributable to a
/// single particle without lookahead. The check runs on a literal, QName-resolved
/// view of the raw schema tree (refs are not substitution-group expanded, the
/// overlap test is QName-only), and is per particle: a particle competing with a
/// repetition of itself is never a violation. These cases pin both the rejections
/// and the deterministic shapes that must keep compiling (XSTS valid mgZ005,
/// ctZ008, particlesHa142, groupH001).
@Suite("Content model determinism (UPA)")
struct ContentModelDeterminismTests {
private func compile(_ body: String) throws {
_ = try PureXML.Schema.Document("""
\(body)
""")
}
private func rejects(_ body: String) -> Bool {
do { try compile(body)
return false
} catch { return true }
}
private func complexType(_ model: String) -> String {
#"\#(model)"#
}
// MARK: - Ambiguous models are rejected
@Test("a choice of two same-name elements is ambiguous")
func test_choiceOfSameName() {
#expect(rejects(complexType(#""#)))
}
@Test("an optional element followed by a same-name element is ambiguous")
func test_optionalThenSameName() {
#expect(rejects(complexType(#""#)))
}
@Test("a repeating element followed by a same-name element is ambiguous")
func test_repeatThenSameName() {
#expect(rejects(complexType(#""#)))
}
@Test("two same-name members of an all group are ambiguous")
func test_allWithSameName() {
#expect(rejects(complexType(#""#)))
}
@Test("an element competing with a wildcard that admits it is ambiguous")
func test_elementAndWildcard() {
#expect(rejects(complexType(#""#)))
}
// MARK: - Deterministic models keep compiling
@Test("distinct elements in sequence and choice are deterministic")
func test_distinctNames() throws {
try compile(complexType(#""#))
try compile(complexType(#""#))
try compile(complexType(#""#))
}
@Test("a fixed-count element followed by a distinct same-name element is deterministic (mgZ005)")
func test_fixedCountThenSameName() throws {
try compile(complexType(#"""
"""#))
}
@Test("a fixed-count group wrapping a repeating element is deterministic (ctZ008)")
func test_fixedCountGroupWithRepeat() throws {
try compile(complexType(#"""
"""#))
}
@Test("an element followed by its own optional repeat is deterministic (particlesHa142)")
func test_elementThenOptionalSameName() throws {
try compile(complexType(#"""
"""#))
}
@Test("a bounded-repeat choice of group references is deterministic (groupH001)")
func test_repeatedChoiceOfGroups() throws {
try compile(#"""
"""#)
}
}