import Testing
@testable import PureXML
/// Per-rule isolation tests (Validation rules XSD): Validation rules XSD: identity constraints.
@Suite("Validation rules XSD: identity constraints")
struct ValidationRuleXSDIdentityTests {
// MARK: identityConstraintXPathsValid
private let uniqueListSchemaTemplate = """
"""
@Test("identityConstraintXPathsValid rejects a malformed selector XPath")
func test_identityConstraintXPathsValid() throws {
let schema = String(format: uniqueListSchemaTemplate, "[[[")
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityConstraintXPathsValid,
schema: schema,
instance: "
",
rootName: "list",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("invalid selector XPath") == true)
}
@Test("identityConstraintXPathsValid accepts a well-formed selector XPath")
func test_identityConstraintXPathsValid_succeeds() throws {
let schema = String(format: uniqueListSchemaTemplate, "item")
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityConstraintXPathsValid,
schema: schema,
instance: "
",
rootName: "list",
)
#expect(errors.isEmpty)
}
// MARK: identityKeyFieldPresent
private let keyListSchema = """
"""
@Test("identityKeyFieldPresent rejects a key target missing its field")
func test_identityKeyFieldPresent() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityKeyFieldPresent,
schema: keyListSchema,
instance: "
",
rootName: "list",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("a field is missing") == true)
}
@Test("identityKeyFieldPresent accepts a key target with its field present")
func test_identityKeyFieldPresent_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityKeyFieldPresent,
schema: keyListSchema,
instance: "
",
rootName: "list",
)
#expect(errors.isEmpty)
}
// MARK: identityValueUnique
private let uniqueListSchema = """
"""
@Test("identityValueUnique reports a duplicate key field")
func test_identityValueUnique() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityValueUnique,
schema: uniqueListSchema,
instance: "
",
rootName: "list",
)
#expect(!errors.isEmpty)
}
@Test("identityValueUnique accepts distinct field values")
func test_identityValueUnique_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityValueUnique,
schema: uniqueListSchema,
instance: "
",
rootName: "list",
)
#expect(errors.isEmpty)
}
// MARK: identityKeyrefResolves
private let keyrefSchema = """
"""
@Test("identityKeyrefResolves rejects a keyref value with no matching key")
func test_identityKeyrefResolves() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityKeyrefResolves,
schema: keyrefSchema,
instance: "",
rootName: "root",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("no matching key") == true)
}
@Test("identityKeyrefResolves accepts a keyref value that matches a key")
func test_identityKeyrefResolves_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityKeyrefResolves,
schema: keyrefSchema,
instance: "",
rootName: "root",
)
#expect(errors.isEmpty)
}
// MARK: identityFieldCardinality
private let multiChildFieldSchema = """
"""
@Test("identityFieldCardinality rejects a field that selects more than one node")
func test_identityFieldCardinality() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityFieldCardinality,
schema: multiChildFieldSchema,
instance: "- ab
",
rootName: "list",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("must select at most one node") == true)
}
@Test("identityFieldCardinality accepts a field that selects exactly one node")
func test_identityFieldCardinality_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityFieldCardinality,
schema: multiChildFieldSchema,
instance: "- a
",
rootName: "list",
)
#expect(errors.isEmpty)
}
// MARK: identityFieldMustBeSimple
private let complexChildFieldSchema = """
"""
private let simpleChildFieldSchema = """
"""
@Test("identityFieldMustBeSimple rejects a field identifying a node with element children")
func test_identityFieldMustBeSimple() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityFieldMustBeSimple,
schema: complexChildFieldSchema,
instance: "- x
",
rootName: "list",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("must identify a node with simple content") == true)
}
@Test("identityFieldMustBeSimple accepts a field identifying a simple-content node")
func test_identityFieldMustBeSimple_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.identityFieldMustBeSimple,
schema: simpleChildFieldSchema,
instance: "- a
",
rootName: "list",
)
#expect(errors.isEmpty)
}
}