PureXML/Tests/SchemaElementRefApplicabilityTests.swift
Mihaela Mihaljevic 7ed7059134 feat(schema): element ref excludes declaration-only attributes and inline type (src-element.2.2)
An element ref carrying nillable/default/fixed/form/block, or an element or
attribute ref carrying an inline type definition, was accepted. A ref is a use
of a global declaration, not a declaration, so beyond ref it admits only
minOccurs, maxOccurs, and id (src-element.2.2). The applicability check already
excluded name and type from a ref; this extends the exclusion to the remaining
declaration-only properties and to an inline simpleType/complexType child.
minOccurs/maxOccurs, a group/attributeGroup ref, and an attribute ref's
use/default/fixed (which override on the use) are unaffected.

XSTS: invalid schemas accepted 439 to 424, no other bucket raised. Disclosed
under-detection: a key/unique/keyref child under a ref is not yet flagged.
2026-06-15 14:29:13 +02:00

37 lines
1.5 KiB
Swift

@testable import PureXML
import Testing
@Suite("XSD element-ref applicability (src-element.2.2)")
struct SchemaElementRefApplicabilityTests {
private let xsd = "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
private func compiles(_ body: String) -> Bool {
(try? PureXML.Schema.Document("<xs:schema \(xsd)>\(body)</xs:schema>")) != nil
}
private func refParticle(_ refAttrs: String, inlineType: String = "") -> String {
"<xs:element name=\"Main\" type=\"xs:string\"/>"
+ "<xs:element name=\"e\"><xs:complexType><xs:sequence>"
+ "<xs:element ref=\"Main\" \(refAttrs)>\(inlineType)</xs:element>"
+ "</xs:sequence></xs:complexType></xs:element>"
}
@Test("An element ref carrying a declaration-only attribute is rejected")
func test_refDeclarationAttributesRejected() {
for attr in ["nillable=\"true\"", "default=\"x\"", "fixed=\"x\"", "form=\"qualified\"", "block=\"#all\""] {
#expect(!compiles(refParticle(attr)), "ref with \(attr) should be rejected")
}
}
@Test("An element ref with an inline type is rejected")
func test_refInlineTypeRejected() {
#expect(!compiles(refParticle("", inlineType: "<xs:complexType><xs:sequence/></xs:complexType>")))
}
@Test("An element ref with only occurrence attributes compiles")
func test_refOccurrenceAccepted() {
#expect(compiles(refParticle("minOccurs=\"0\" maxOccurs=\"3\"")))
#expect(compiles(refParticle("")))
}
}