@testable import PureXML import Testing @Suite("XSD derivation control") struct XSDDerivationControlTests { private func validate(_ xsd: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] { try PureXML.Schema.Document(xsd).validate(xml) } /// A schema with an abstract `shape` head, a concrete `circle` member, and a /// `figure` element whose content references the head. private let substitution = """ """ @Test("An abstract element may not appear at the document root") func test_abstractRootRejected() throws { let xsd = """ """ #expect(try !validate(xsd, "x").isEmpty) } @Test("An abstract head is not admitted in content, only its members") func test_abstractHeadInContent() throws { #expect(try !validate(substitution, "
x
").isEmpty) #expect(try validate(substitution, "
x
").isEmpty) } @Test("An element of abstract type requires xsi:type naming a concrete type") func test_abstractTypeNeedsXsiType() throws { let xsd = """ """ #expect(try !validate(xsd, "a").isEmpty) #expect(try validate(xsd, "a1").isEmpty) } @Test("block=extension on a type forbids xsi:type substitution by extension") func test_blockExtensionXsiType() throws { let xsd = """ """ // Substitution by extension is blocked. #expect(try !validate(xsd, "a1").isEmpty) // Substitution by restriction is still permitted. #expect(try validate(xsd, "a").isEmpty) } @Test("final=extension forbids deriving a new type by extension") func test_finalExtension() throws { let xsd = """ """ #expect(throws: PureXML.Schema.SchemaError.self) { _ = try PureXML.Schema.Document(xsd) } } @Test("final=#all forbids both extension and restriction") func test_finalAll() throws { let xsd = """ """ #expect(throws: PureXML.Schema.SchemaError.self) { _ = try PureXML.Schema.Document(xsd) } } @Test("xs:redefine requires the redefined type to derive from itself") func test_redefineSelfReference() throws { let base = """ """ let bad = """ """ #expect(throws: PureXML.Schema.SchemaError.self) { _ = try PureXML.Schema.Document(bad, schemaLoader: { _ in base }) } } @Test("xs:redefine compiles when the type derives from itself") func test_redefineCompiles() throws { let base = """ """ let good = """ """ _ = try PureXML.Schema.Document(good, schemaLoader: { _ in base }) } @Test("A concrete element with a non-blocked xsi:type still validates") func test_xsiTypeStillWorks() throws { let xsd = """ """ #expect(try validate(xsd, "a1").isEmpty) } }