import Testing @testable import PureXML /// Per-rule isolation tests (Validation rules XSD): Validation rules XSD: simple type and structure. @Suite("Validation rules XSD: simple type and structure") struct ValidationRuleXSDStructureTests { // MARK: simpleTypeDisallowedAttribute / simpleTypeForbidsChildren / simpleTypeValueValid private let simpleStringSchema = """ """ @Test("simpleTypeDisallowedAttribute rejects an attribute on a simple-typed element") func test_simpleTypeDisallowedAttribute() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.simpleTypeDisallowedAttribute, schema: simpleStringSchema, instance: "text", rootName: "n", ) #expect(errors.count == 1) #expect(errors.first?.reason.contains("has a simple type") == true) } @Test("simpleTypeDisallowedAttribute accepts a simple-typed element with no attributes") func test_simpleTypeDisallowedAttribute_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.simpleTypeDisallowedAttribute, schema: simpleStringSchema, instance: "text", rootName: "n", ) #expect(errors.isEmpty) } @Test("simpleTypeForbidsChildren rejects element children on a simple-typed element") func test_simpleTypeForbidsChildren() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.simpleTypeForbidsChildren, schema: simpleStringSchema, instance: "", rootName: "n", ) #expect(errors.count == 1) #expect(errors.first?.reason.contains("must not have children") == true) } @Test("simpleTypeForbidsChildren accepts a simple-typed element with only text") func test_simpleTypeForbidsChildren_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors( PureXML.Validation.XSD.simpleTypeForbidsChildren, schema: simpleStringSchema, instance: "text", rootName: "n", ) #expect(errors.isEmpty) } @Test("simpleTypeValueValid locates a simple-type violation at the root element") func test_simpleTypeValueValid() throws { let schema = """ """ let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.simpleTypeValueValid, schema: schema, instance: "x", rootName: "n") #expect(errors.count == 1) #expect(errors.first?.reason.contains("n") == true) #expect(errors.first.map(ValidationRuleTestSupport.path) == ["n"]) } @Test("simpleTypeValueValid accepts a valid integer at the boundary") func test_simpleTypeValueValid_succeeds() throws { let schema = """ """ let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.simpleTypeValueValid, schema: schema, instance: "0", rootName: "n") #expect(errors.isEmpty) } // MARK: typeReferenceResolves @Test("typeReferenceResolves rejects a reference to an undeclared type") func test_typeReferenceResolves() throws { let schema = """ """ let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.typeReferenceResolves, schema: schema, instance: "", rootName: "n") #expect(errors.count == 1) #expect(errors.first?.reason.contains("unknown type") == true) } @Test("typeReferenceResolves accepts a reference that resolves to a declared type") func test_typeReferenceResolves_succeeds() throws { let schema = """ """ let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.typeReferenceResolves, schema: schema, instance: "", rootName: "n") #expect(errors.isEmpty) } // MARK: contentModelStructure private let sequenceSchema = """ """ @Test("contentModelStructure rejects a child the content model does not allow") func test_contentModelStructure() throws { let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.contentModelStructure, schema: sequenceSchema, instance: "", rootName: "n") #expect(errors.count == 1) #expect(errors.first?.reason.contains("is not allowed here") == true) } @Test("contentModelStructure accepts children matching the content model") func test_contentModelStructure_succeeds() throws { let errors = try ValidationRuleTestSupport.xsdErrors(PureXML.Validation.XSD.contentModelStructure, schema: sequenceSchema, instance: "x", rootName: "n") #expect(errors.isEmpty) } }