@testable import PureXML import Testing @Suite("XSD includes, substitution groups, xsi:type") struct XSDImportTests { private func validate( _ xsd: String, _ xml: String, loader: @escaping (String) -> String? = { _ in nil }, ) throws -> [PureXML.Validation.ValidationError] { try PureXML.Schema.Document(xsd, schemaLoader: loader).validate(xml) } @Test("xs:include pulls in another schema's named type") func test_include() throws { let library = """ """ let main = """ """ let loader: (String) -> String? = { $0 == "lib.xsd" ? library : nil } #expect(try validate(main, "ABC", loader: loader).isEmpty) #expect(try !validate(main, "abc", loader: loader).isEmpty) } @Test("xs:redefine overrides an included type") func test_redefine() throws { let base = """ """ let main = """ """ let loader: (String) -> String? = { $0 == "base.xsd" ? base : nil } #expect(try validate(main, "5", loader: loader).isEmpty) #expect(try !validate(main, "50", loader: loader).isEmpty) } @Test("A substitution-group member is accepted where the head is referenced") func test_substitutionGroup() throws { let xsd = """ """ #expect(try validate(xsd, "ab").isEmpty) #expect(try !validate(xsd, "b").isEmpty) } @Test("xsi:type overrides the declared type at the instance") func test_xsiType() throws { let xsd = """ """ let valid = "3" let invalid = "x" #expect(try validate(xsd, valid).isEmpty) #expect(try !validate(xsd, invalid).isEmpty) } }