@testable import PureXML
import Testing
/// `pattern` and `enumeration` are the only constraining facets XSD allows on a
/// union, and both must be enforced on top of member-type membership (#147).
/// They had been ignored, so any value valid for a member slipped through.
@Suite("Union restriction facets")
struct SchemaUnionFacetTests {
private func schema(_ facets: String) throws -> PureXML.Schema.Document {
try PureXML.Schema.Document("""
\(facets)
""")
}
@Test("Enumeration on a union restricts to the listed values")
func test_enumeration() throws {
let doc = try schema(#""#)
#expect(try doc.validate("5").isEmpty) // listed
#expect(try doc.validate("foo").isEmpty) // listed
#expect(try !doc.validate("7").isEmpty) // valid int, not listed
#expect(try !doc.validate("bar").isEmpty) // valid string, not listed
}
@Test("Union enumeration compares in value space, not lexically")
func test_enumerationValueSpace() throws {
let doc = try schema(#""#)
// 05 is a valid xs:int equal to the enumerated 5, so it is in the set.
#expect(try doc.validate("05").isEmpty)
}
@Test("Pattern on a union constrains the lexical value")
func test_pattern() throws {
let doc = try schema(#""#)
#expect(try doc.validate("abc").isEmpty) // valid string, matches
#expect(try !doc.validate("5").isEmpty) // valid int, fails pattern
}
@Test("A union with no facets still admits any member value")
func test_noFacets() throws {
let doc = try schema("")
#expect(try doc.validate("42").isEmpty)
#expect(try doc.validate("anything").isEmpty)
}
}