@testable import PureXML import Testing /// Component-name uniqueness (XSD 1.0 symbol-space constraints): within a schema, /// global type names (simpleType and complexType share one space), global element /// names, global attribute names, named model-group names, and named /// attribute-group names must each be unique, and identity-constraint names must /// be unique across the document. A duplicate was previously accepted, with the /// later definition silently overwriting the earlier one. @Suite("Schema component-name uniqueness") struct SchemaNameUniquenessTests { 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("distinct global names compile") func test_valid() throws { try compile(#""" """#) // A name shared across different symbol spaces (a type and an element) is // allowed. try compile(#""" """#) } @Test("a duplicate global type name is rejected (one symbol space)") func test_duplicateType() { #expect(rejects(#""#)) // simpleType and complexType share the type symbol space. #expect(rejects(#""#)) } @Test("a duplicate global element or attribute name is rejected") func test_duplicateElementAttribute() { #expect(rejects(#""#)) #expect(rejects(#""#)) } @Test("a keyref refer must name a key or unique") func test_keyrefRefer() throws { // refer to a defined key resolves. try compile(#""" """#) // refer to a name that is not a key/unique is rejected. #expect(rejects(#""" """#)) } @Test("a keyref arity must match its referenced key or unique") func test_keyrefArityMismatch() { // keyref and key have different arities (1 vs 2): must be rejected. #expect(rejects(#""" """#)) } @Test("a duplicate identity-constraint name is rejected") func test_duplicateIdentityConstraint() { #expect(rejects(#""" """#)) } @Test("duplicate identity-constraint names across included files are rejected") func test_duplicateIdentityConstraintAcrossIncluded() { let imported = """ """ let main = """ """ #expect((try? PureXML.Schema.Document(main, schemaLoader: { $0 == "sub.xsd" ? imported : nil })) == nil) } @Test("duplicate global element names across included files are rejected") func test_duplicateGlobalElementAcrossIncluded() { let imported = """ """ let main = """ """ #expect((try? PureXML.Schema.Document(main, schemaLoader: { $0 == "sub.xsd" ? imported : nil })) == nil) } @Test("debug name00101m2") func test_debugName() { let path = "/private/tmp/xsts/xmlschema2006-11-06/sunData/IdConstrDefs/name/name00101m/name00101m2.xsd" guard let source = try? String(contentsOfFile: path) else { return } do { _ = try PureXML.Schema.Document(source) } catch { // Expected } } @Test("duplicate global names in different imported files are rejected even if files are structurally identical") func test_duplicateAcrossSeparateFilesIdenticalContent() { let imported = """ """ let main = """ """ #expect((try? PureXML.Schema.Document(main, schemaLoader: { location in if location == "sub1.xsd" || location == "sub2.xsd" { return imported } return nil })) == nil) } @Test("circular import of the same schema does not trigger duplicate global name error") func test_circularImportSelfDeduplication() throws { let imported = """ """ let main = """ """ _ = try PureXML.Schema.Document(main, schemaLoader: { location in if location == "sub.xsd" { return imported } if location == "main.xsd" { return main } return nil }) } }