import Testing @testable import PureXML /// Per-rule isolation tests (Validation rules XSD): Validation rules XSD: derivation and identifiers. @Suite("Validation rules XSD: derivation and identifiers") struct ValidationRuleXSDDerivationTests { // MARK: xsiTypeSubstitutionValid / xsiTypeOverrideResolves private let abstractDerivedSchema = """ """ @Test("xsiTypeSubstitutionValid rejects an abstract declared type with no xsi:type override") func test_xsiTypeSubstitutionValid() throws { let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.xsiTypeSubstitutionValid, schema: abstractDerivedSchema, instance: "", rootName: "n") #expect(errors.count == 1) #expect(errors.first?.reason.contains("abstract type") == true) } @Test("xsiTypeSubstitutionValid accepts an xsi:type naming a concrete derived type") func test_xsiTypeSubstitutionValid_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.xsiTypeSubstitutionValid, schema: abstractDerivedSchema, instance: "", rootName: "n", ) #expect(errors.isEmpty) } private let simpleStringSchema = """ """ @Test("xsiTypeOverrideResolves rejects an xsi:type naming an unknown type") func test_xsiTypeOverrideResolves() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.xsiTypeOverrideResolves, schema: simpleStringSchema, instance: "text", rootName: "n", ) #expect(errors.count == 1) #expect(errors.first?.reason.contains("unknown xsi:type") == true) } @Test("xsiTypeOverrideResolves accepts an xsi:type naming a resolvable type") func test_xsiTypeOverrideResolves_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.xsiTypeOverrideResolves, schema: simpleStringSchema, instance: "text", rootName: "n", ) #expect(errors.isEmpty) } // MARK: idIdrefConsistent private let idElementSchema = """ """ @Test("idIdrefConsistent rejects a duplicate xs:ID value") func test_idIdrefConsistent() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.idIdrefConsistent, schema: idElementSchema, instance: "", rootName: "n", ) #expect(!errors.isEmpty) } @Test("idIdrefConsistent accepts distinct xs:ID values") func test_idIdrefConsistent_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.idIdrefConsistent, schema: idElementSchema, instance: "", rootName: "n", ) #expect(errors.isEmpty) } // MARK: xsdStreamingIdConsistency @Test("xsdStreamingIdConsistency rejects a duplicate xs:ID recorded during streaming") func test_xsdStreamingIdConsistency() { let validator = PureXML.Schema.ComplexValidator() validator.recordIDs(PureXML.Schema.SimpleType(base: .id), value: "x1", at: []) validator.recordIDs(PureXML.Schema.SimpleType(base: .id), value: "x1", at: []) let errors = PureXML.Validation.Validator.blank .validating(PureXML.Schema.ComplexValidator.idConsistency) .errors(for: validator, in: ()) #expect(!errors.isEmpty) } @Test("xsdStreamingIdConsistency accepts distinct xs:ID values recorded during streaming") func test_xsdStreamingIdConsistency_succeeds() { let validator = PureXML.Schema.ComplexValidator() validator.recordIDs(PureXML.Schema.SimpleType(base: .id), value: "x1", at: []) validator.recordIDs(PureXML.Schema.SimpleType(base: .id), value: "x2", at: []) let errors = PureXML.Validation.Validator.blank .validating(PureXML.Schema.ComplexValidator.idConsistency) .errors(for: validator, in: ()) #expect(errors.isEmpty) } }