import Testing
@testable import PureXML
@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 a schema error")
func test_unknownTypeInDefaultLibrary() throws {
let rng = """
"""
// The default library defines only string and token; an unknown
// datatype is a schema error (the spec suite's incorrect class), not
// a match-nothing pattern.
#expect(throws: PureXML.Schema.SchemaError.self) {
_ = try PureXML.Schema.RelaxNG(rng)
}
}
@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 is a schema error")
func test_unknownValueType() throws {
let rng = """
5
"""
#expect(throws: PureXML.Schema.SchemaError.self) {
_ = try PureXML.Schema.RelaxNG(rng)
}
}
@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, ""))
}
}