@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")) } } @Test("fractionDigits on integer types must be zero") func test_fractionDigitsOnInteger() throws { try compile(#""#, base: "xs:integer") #expect(rejects(#""#, base: "xs:integer")) #expect(rejects(#""#, base: "xs:int")) } @Test("duration range bounds must be ordered") func test_durationBoundOrdering() throws { // minInclusive <= maxInclusive is valid try compile(#""#, base: "xs:duration") // minInclusive > maxInclusive is invalid #expect(rejects(#""#, base: "xs:duration")) #expect(rejects(#""#, base: "xs:duration")) #expect(rejects(#""#, base: "xs:duration")) } @Test("built-in list types require minLength of 1") func test_listBuiltinMinLength() { #expect(rejects(#""#, base: "xs:IDREFS")) #expect(rejects(#""#, base: "xs:NMTOKENS")) } @Test("facet applicability per base type") func test_facetApplicabilityPerBase() { #expect(rejects(#""#, base: "xs:string")) #expect(rejects(#""#, base: "xs:boolean")) #expect(rejects(#""#, base: "xs:decimal")) } @Test("foreign elements inside restriction matching facet names are ignored") func test_foreignElementsInRestriction() throws { let xml = """ """ _ = try PureXML.Schema.Document(xml) } @Test("namespace prefix conflation: user-defined type named decimal is not conflated with built-in decimal") func test_userTypeNamedDecimal() throws { let xml = """ """ _ = try PureXML.Schema.Document(xml) } @Test("incomparable duration bounds are valid") func test_incomparableDurationBounds() throws { let xml = """ """ _ = try PureXML.Schema.Document(xml) } }