import Testing @testable import PureXML /// `block="substitution"` on a substitution-group head forbids any member from /// standing in for it, regardless of the member's type derivation (#147, XSTS /// disallowedSubst set). Only type-derivation blocks (`extension`/`restriction`) /// had been applied, so a same-type member still substituted under a /// substitution block. @Suite("Substitution-group block") struct SchemaSubstitutionBlockTests { private func schema(headBlock: String) throws -> PureXML.Schema.Document { let attribute = headBlock.isEmpty ? "" : " block=\"\(headBlock)\"" return try PureXML.Schema.Document(""" """) } private let withMember = #"ab"# private let headsOnly = #"ab"# @Test("block='substitution' on the head rejects a substituting member") func test_substitutionBlocked() throws { #expect(try !schema(headBlock: "substitution").validate(withMember).isEmpty) // Plain heads are still fine. #expect(try schema(headBlock: "substitution").validate(headsOnly).isEmpty) } @Test("without a substitution block the member may substitute") func test_memberAllowedWithoutBlock() throws { #expect(try schema(headBlock: "").validate(withMember).isEmpty) } /// block="substitution" bars members even when the head is an ABSTRACT, UNTYPED /// element (the substitution block is type-independent). Previously the filter was /// skipped for a head with no type, so a member still substituted (particlesDc). @Test("block='substitution' on an untyped abstract head bars members") func test_substitutionBlockedOnUntypedAbstractHead() throws { let schema = try PureXML.Schema.Document(""" """) // A member substituting the substitution-blocked head is invalid. #expect(try !schema.validate(#""#).isEmpty) } @Test("an intermediate substitution block stops descendants from restricting an ancestor head") func test_substitutionBlockedHeadDoesNotChainDescendants() { #expect((try? PureXML.Schema.Document(""" """)) == nil) } @Test("a substitution-blocked member can still restrict its own ancestor head") func test_substitutionBlockedHeadItselfStillRestrictsAncestor() { #expect((try? PureXML.Schema.Document(""" """)) != nil) } /// cvc-elt.4.3.2.1: an xsi:type naming a complex type must be derived from the /// element's declared type. A complex type on a different branch of the /// hierarchy is rejected; the declared type itself is valid. @Test("an xsi:type must be derived from the element's declared type") func test_xsiTypeMustDeriveFromDeclared() throws { let schema = try PureXML.Schema.Document(""" """) func doc(_ type: String) -> String { #"x"# } // Dr (restriction of B) is on a different branch, not derived from De: invalid. #expect(try !schema.validate(doc("Dr")).isEmpty) // The declared type itself is valid. #expect(try schema.validate(doc("De")).isEmpty) } }