@testable import PureXML import Testing @Suite("RELAX NG datatypeLibrary resolution and nsName subtraction") struct RelaxNGDatatypeLibraryTests { private let xsd = "http://www.w3.org/2001/XMLSchema-datatypes" private func validXML(_ rng: String, _ xml: String) throws -> Bool { try PureXML.Schema.RelaxNG(rng).validate(xml) } private func validRNC(_ rnc: String, _ xml: String) throws -> Bool { try PureXML.Schema.RelaxNG(compact: rnc).validate(xml) } @Test("The default library defines only string and token") func test_defaultLibraryStringToken() throws { let rng = """ """ #expect(try validXML(rng, "anything here")) } @Test("An XSD type without the XSD library is an unknown datatype and matches nothing") func test_unknownTypeInDefaultLibrary() throws { let rng = """ """ // `integer` is not in the default library, so no value validates. #expect(try !validXML(rng, "5")) #expect(try !validXML(rng, "x")) } @Test("Declaring the XSD datatypeLibrary makes xsd types resolve") func test_xsdLibraryResolves() throws { let rng = """ """ #expect(try validXML(rng, "5")) #expect(try !validXML(rng, "x")) } @Test("datatypeLibrary is inherited from an ancestor element") func test_libraryInheritedFromAncestor() throws { let rng = """ """ #expect(try validXML(rng, "42")) #expect(try !validXML(rng, "4.2")) } @Test("A value with an unknown explicit type matches nothing") func test_valueUnknownType() throws { let rng = """ 5 """ #expect(try !validXML(rng, "5")) } @Test("nsName with except excludes a name in the namespace") func test_nsNameExceptXML() throws { let rng = """ skip """ // Any element in urn:x except local name "skip" is allowed. let allowed = "" let excluded = "" let wrongNS = "" #expect(try validXML(rng, allowed)) #expect(try !validXML(rng, excluded)) #expect(try !validXML(rng, wrongNS)) } @Test("RNC ns:* wildcard matches any name in the namespace") func test_rncNsWildcard() throws { let rnc = """ namespace p = "urn:x" element root { element p:* { empty } } """ #expect(try validRNC(rnc, "")) #expect(try !validRNC(rnc, "")) } @Test("RNC ns:* - name subtracts a name from the namespace wildcard") func test_rncNsWildcardExcept() throws { let rnc = """ namespace p = "urn:x" element root { element p:* - p:skip { empty } } """ #expect(try validRNC(rnc, "")) #expect(try !validRNC(rnc, "")) } }