@testable import PureXML
import Testing
@Suite("XSD substitution-group member type derivation (e-props-correct.4)")
struct SchemaSubstitutionTypeTests {
private let xsd = "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
private func compiles(_ body: String) -> Bool {
(try? PureXML.Schema.Document("\(body)")) != nil
}
@Test("A member whose type is unrelated to the head's type is rejected")
func test_unrelatedMemberType() {
#expect(!compiles(
""
+ "",
))
}
@Test("A member whose type equals or derives from the head's type is accepted")
func test_derivingMemberType() {
// Identical type.
#expect(compiles(
""
+ "",
))
// Built-in lattice: xs:int derives from xs:integer.
#expect(compiles(
""
+ "",
))
// User restriction chain.
#expect(compiles(
""
+ ""
+ "",
))
}
@Test("An untyped head admits any member type")
func test_untypedHeadIsUrType() {
#expect(compiles(
""
+ "",
))
}
/// `cos-st-derived-ok` clause 2.4: a type validly derives from a union when it
/// derives from one of the union's member types. List/union derivation is not
/// modelled, so the check stands down: an integer member of a union-typed head
/// must compile rather than be wrongly rejected.
@Test("A member of a union-typed head is not rejected (list/union stand-down)")
func test_unionTypedHeadStandsDown() {
#expect(compiles(
""
+ ""
+ "",
))
}
/// A local element sharing the member's name carries a different type; the
/// member's type must be read from its own declaration, not conflated with the
/// local one, so this valid schema compiles.
@Test("A local element sharing the member name does not supply the member type")
func test_localNameDoesNotShadowMemberType() {
#expect(compiles(
""
+ ""
+ ""
+ ""
+ ""
+ "",
))
}
/// With an import but no loader the check stands down; once the import is
/// loaded through a `schemaLoader` the member type is checked against the head.
@Test("With an unloaded import the check stands down")
func test_importWithoutLoaderStandsDown() {
let main = ""
+ ""
+ ""
#expect((try? PureXML.Schema.Document(main)) != nil)
}
@Test("With a loaded import an unrelated member type is rejected")
func test_loadedImportChecksMemberType() {
let imported = ""
+ ""
let main = ""
+ ""
+ ""
#expect((try? PureXML.Schema.Document(main, schemaLoader: { $0 == "imp.xsd" ? imported : nil })) == nil)
}
@Test("Substitution group member is blocked if type derivation method is final on the head element")
func test_finalSubstitutionExclusion() {
// Direct extension derivation rejected when final="extension"
#expect(!compiles(
""
+ ""
+ ""
+ "",
))
// Direct restriction derivation accepted when final="extension"
#expect(compiles(
""
+ ""
+ ""
+ "",
))
// Indirect extension derivation rejected when final="extension"
#expect(!compiles(
""
+ ""
+ ""
+ ""
+ "",
))
// Schema finalDefault="extension" rejects extension derivation
#expect((try? PureXML.Schema.Document("""
""")) == nil)
// final="#all" rejects both extension and restriction
#expect(!compiles(
""
+ ""
+ ""
+ "",
))
}
@Test("Substitution group member final exclusions: local name collisions and built-ins")
func test_finalSubstitutionExclusionEdgeCases() {
// Local element name collision: local element does not overwrite global final setting
#expect((try? PureXML.Schema.Document("""
""")) != nil)
// Built-in simple type restriction rejected when final="restriction"
#expect(!compiles(
""
+ "",
))
}
@Test("Substitution group member is blocked if inline complexType derivation method is final on the head element")
func test_finalSubstitutionExclusionInlineType() {
// Member3 derives by extension from HeadType inline, but Head has final="extension" -> rejected
#expect(!compiles(
""
+ ""
+ ""
+ " "
+ " "
+ " "
+ "",
))
// Member3 derives by extension from HeadType inline, Head has final="restriction" -> accepted
#expect(compiles(
""
+ ""
+ ""
+ " "
+ " "
+ " "
+ "",
))
}
@Test("Adversarial Critic: member with inline simpleType restricting another type")
func test_inlineSimpleTypeRestrictingAnotherType() {
#expect(compiles(
""
+ ""
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ " "
+ "",
))
}
@Test("Adversarial Critic: member with inline simpleType restricting a named list type")
func test_inlineSimpleTypeRestrictingNamedList() {
#expect(compiles(
""
+ ""
+ ""
+ " "
+ " "
+ " "
+ "",
))
}
}