@testable import PureXML
import Testing
@Suite("XSD nillable and value constraints")
struct XSDValueConstraintTests {
private func validate(_ xsd: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] {
try PureXML.Schema.Document(xsd).validate(xml)
}
private let xsi = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
@Test("A nillable element accepts xsi:nil with empty content")
func test_nillableEmpty() throws {
let xsd = """
"""
#expect(try validate(xsd, "").isEmpty)
// Even though the content would otherwise have to be an integer.
#expect(try validate(xsd, "notanumber").isEmpty == false)
}
@Test("A nilled element must be empty")
func test_nilMustBeEmpty() throws {
let xsd = """
"""
let found = try validate(xsd, "5")
#expect(found.count == 1)
#expect(found[0].reason.contains("must be empty"))
}
@Test("xsi:nil on a non-nillable element is rejected")
func test_nilNotNillable() throws {
let xsd = """
"""
let found = try validate(xsd, "")
#expect(found.count == 1)
#expect(found[0].reason.contains("not nillable"))
}
@Test("A fixed attribute must carry its fixed value")
func test_fixedAttribute() throws {
let xsd = """
"""
#expect(try validate(xsd, "- ").isEmpty)
#expect(try validate(xsd, "
").isEmpty)
let found = try validate(xsd, "- ")
#expect(found.count == 1)
#expect(found[0].reason.contains("is fixed and must be 'book'"))
}
@Test("A fixed element value must match")
func test_fixedElement() throws {
let xsd = """
"""
#expect(try validate(xsd, "1.0").isEmpty)
let found = try validate(xsd, "2.0")
#expect(found.count == 1)
#expect(found[0].reason.contains("is fixed and must be '1.0'"))
}
}