@testable import PureXML
import Testing
/// Schema-validity of constraining-facet definitions (XSD Part 2 Datatypes 4.3):
/// a length-family facet value must be a valid `nonNegativeInteger`
/// (`totalDigits` a `positiveInteger`), `length` may not co-occur with
/// `minLength`/`maxLength`, and the integer ranges must be ordered. Such a schema
/// is invalid and must be rejected at compile time; previously the malformed
/// value was silently dropped and the schema accepted (XSTS invalid-schema
/// MS DataTypes facet cases).
@Suite("Facet definition validity")
struct SchemaFacetDefinitionTests {
private func compile(_ facets: String, base: String = "xs:string") throws {
_ = try PureXML.Schema.Document("""
\(facets)
""")
}
private func rejects(_ facets: String, base: String = "xs:string") -> Bool {
do { try compile(facets, base: base)
return false
} catch { return true }
}
@Test("a well-formed facet definition compiles")
func test_valid() throws {
try compile(#""#)
try compile(#""#)
try compile(#""#)
try compile(#""#, base: "xs:decimal")
try compile(#""#)
// A well-formed nonNegativeInteger is accepted regardless of machine-integer
// range, and tolerates a leading "+" and leading zeros.
try compile(#""#)
try compile(#""#)
try compile(#""#)
try compile(#""#)
}
@Test("a malformed length-family lexical is rejected")
func test_malformedLexical() {
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
}
@Test("totalDigits must be a positiveInteger")
func test_totalDigitsPositive() {
#expect(rejects(#""#, base: "xs:decimal"))
#expect(rejects(#""#, base: "xs:decimal"))
}
@Test("length may not co-occur with minLength or maxLength")
func test_lengthCoOccurrence() {
#expect(rejects(#""#))
#expect(rejects(#""#))
}
@Test("integer facet ranges must be ordered")
func test_rangeOrder() {
#expect(rejects(#""#))
#expect(rejects(#""#, base: "xs:decimal"))
}
@Test("a value-bound facet value must be valid for the base type")
func test_boundValueValidity() throws {
try compile(#""#, base: "xs:decimal")
try compile(#""#, base: "xs:decimal")
#expect(rejects(#""#, base: "xs:decimal"))
#expect(rejects(#""#, base: "xs:decimal"))
#expect(rejects(#""#, base: "xs:decimal"))
}
@Test("an enumeration value must be valid for the base type")
func test_enumerationValueValidity() throws {
try compile(#""#, base: "xs:decimal")
#expect(rejects(#""#, base: "xs:float"))
#expect(rejects(#""#, base: "xs:decimal"))
}
@Test("inclusive and exclusive bounds on the same side may not co-occur")
func test_boundMutualExclusion() {
#expect(rejects(#""#, base: "xs:decimal"))
#expect(rejects(#""#, base: "xs:decimal"))
}
@Test("the lower bound may not exceed (or, if exclusive, equal) the upper bound")
func test_boundOrder() {
#expect(rejects(#""#, base: "xs:decimal"))
#expect(rejects(#""#, base: "xs:decimal"))
// An inclusive single-point range is valid.
do { try compile(#""#, base: "xs:decimal") } catch {
Issue.record("a single-point inclusive range must be valid: \(error)")
}
}
@Test("the rejection message names the offending facet")
func test_locatedMessage() {
do {
try compile(#""#)
Issue.record("expected the malformed facet to be rejected")
} catch {
#expect(String(describing: error).contains("length"))
}
}
}