import Testing
@testable import PureXML
@Suite("SimpleType restriction and top-level constraints")
struct SchemaSimpleTypeRestrictionTests {
private func compile(_ body: String) throws {
_ = try PureXML.Schema.Document("""
\(body)
""")
}
private func rejects(_ body: String) -> Bool {
do {
try compile(body)
return false
} catch {
return true
}
}
@Test("restricting xs:anySimpleType directly in simpleType is rejected")
func test_anySimpleTypeRestrictionSimple() {
#expect(rejects("""
"""))
}
@Test("extending xs:anySimpleType under simpleContent is accepted")
func test_anySimpleTypeSimpleContent() throws {
try compile("""
""")
}
@Test("using xs:anySimpleType as list itemType is rejected")
func test_anySimpleTypeInList() {
#expect(rejects("""
"""))
}
@Test("using xs:anySimpleType as union memberTypes is accepted")
func test_anySimpleTypeInUnion() throws {
try compile("""
""")
}
@Test("top-level anonymous definitions are rejected")
func test_topLevelUnnamed() {
#expect(rejects("""
"""))
#expect(rejects("""
"""))
}
@Test("simpleType facet restriction constraints on digits and length are enforced")
func test_facetRestrictionValidation() throws {
// totalDigits: base has 4, restriction has 5 -> rejected
#expect(rejects("""
"""))
// totalDigits: base has 4, restriction has 4 -> accepted
try compile("""
""")
// fractionDigits: base has 2, restriction has 3 -> rejected
#expect(rejects("""
"""))
// minLength/maxLength/length restrictions
#expect(rejects("""
"""))
}
@Test("debug xsd.xsd")
func test_debugXSD() {
let path = "/private/tmp/xsts/xmlschema2006-11-06/msData/particles/particlesZ001.xsd"
if let xml = try? String(contentsOfFile: path) {
do {
_ = try PureXML.Schema.Document(xml)
print("--- particlesZ001.xsd compiled successfully ---")
} catch {
print("--- particlesZ001.xsd failed compile: ---")
print(error)
}
}
}
@Test("restricting xs:anyType directly in simpleType is rejected")
func test_anyTypeRestrictionSimple() {
#expect(rejects("""
"""))
}
@Test("unbound QName prefix in simpleType restriction base is rejected")
func test_unboundPrefixRestrictionBase() {
#expect(rejects("""
"""))
}
@Test("unbound QName prefix in list itemType is rejected")
func test_unboundPrefixListItemType() {
#expect(rejects("""
"""))
}
@Test("multiple inline simpleType base definitions in restriction is rejected")
func test_multipleInlineSimpleTypeRestriction() {
#expect(rejects("""
"""))
}
@Test("both base attribute and inline simpleType in restriction is rejected")
func test_baseAndInlineSimpleTypeRestriction() {
#expect(rejects("""
"""))
}
@Test("anyAttribute inside simpleType restriction is rejected")
func test_anyAttributeInSimpleTypeRestriction() {
#expect(rejects("""
"""))
}
@Test("list with multiple inline simpleTypes is rejected")
func test_listWithMultipleInlineSimpleTypes() {
#expect(rejects("""
"""))
}
@Test("list with both itemType and inline simpleType is rejected")
func test_listWithItemTypeAndInlineSimpleType() {
#expect(rejects("""
"""))
}
@Test("list itemType referencing a complex type is rejected")
func test_listItemTypeComplexType() {
#expect(rejects("""
"""))
}
}