import Testing
@testable import PureXML
/// cvc-identity-constraint.3: a `key`/`keyref`/`unique` field must identify a node
/// of simple type. A field that reaches an element whose declared type is the
/// ur-type `anyType` (no type) or a complex type without simple content is invalid,
/// unless the instance gives the node a simple type through `xsi:type`. Mirrors the
/// W3C XSTS idZ010 shape (a `key`/`keyref` whose fields select no-type elements
/// nested through a path), the case PureXML previously accepted.
@Suite("Identity-constraint field must select a simple-typed node")
struct SchemaIdentityFieldSimpleTypeTests {
/// idZ010 in miniature: `root/a` carries a `key` whose field path reaches `c`,
/// declared with no type (so `anyType`). The field is not simple, so the
/// instance is invalid even though the value would otherwise be a fine key.
private func anyTypeFieldSchema() throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
""")
}
@Test("a key field reaching a no-type (anyType) element is rejected")
func test_anyTypeFieldRejected() throws {
let schema = try anyTypeFieldSchema()
let errors = try schema.validate("1")
#expect(!errors.isEmpty)
#expect(errors.contains { $0.reason.contains("simple content") })
}
@Test("a simple-typed field at the same path is accepted")
func test_simpleTypedFieldAccepted() throws {
let schema = try PureXML.Schema.Document("""
""")
#expect(try schema.validate("12").isEmpty)
}
@Test("an anyType field rescued by a simple xsi:type is accepted")
func test_anyTypeRescuedByXsiType() throws {
// The element is declared anyType, but the instance gives it xsi:type=int,
// so its governing type is simple and the field is valid (cvc reads the
// governing type). This is the guard that keeps the check from a false
// positive on a legitimately simple-at-runtime node.
let schema = try anyTypeFieldSchema()
let xml = """
\
1
"""
#expect(try schema.validate(xml).isEmpty)
}
}