import Testing
@testable import PureXML
/// Per-rule isolation tests (Validation rules XSD): nillable and fixed-value rules.
@Suite("Validation rules XSD: nillable and fixed-value rules")
struct ValidationRuleXSDNilFixedTests {
// MARK: nilAttributeRequiresNillable
@Test("nilAttributeRequiresNillable rejects xsi:nil on a non-nillable element")
func test_nilAttributeRequiresNillable() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilAttributeRequiresNillable,
schema: schema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("not nillable") == true)
}
@Test("nilAttributeRequiresNillable accepts xsi:nil on a nillable element")
func test_nilAttributeRequiresNillable_succeeds() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilAttributeRequiresNillable,
schema: schema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: nilElementMustBeEmpty
@Test("nilElementMustBeEmpty rejects content on a nilled element")
func test_nilElementMustBeEmpty() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilElementMustBeEmpty,
schema: schema,
instance: "x",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("must be empty") == true)
}
@Test("nilElementMustBeEmpty accepts an empty nilled element")
func test_nilElementMustBeEmpty_succeeds() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilElementMustBeEmpty,
schema: schema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: nilElementNoFixedConstraint
@Test("nilElementNoFixedConstraint rejects a nilled element declared fixed")
func test_nilElementNoFixedConstraint() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilElementNoFixedConstraint,
schema: schema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("fixed value constraint") == true)
}
@Test("nilElementNoFixedConstraint accepts a nilled element with no fixed constraint")
func test_nilElementNoFixedConstraint_succeeds() throws {
let schema = """
"""
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.nilElementNoFixedConstraint,
schema: schema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: fixedElementNoChildren / fixedElementValueMatches
private let fixedStringSchema = """
"""
@Test("fixedElementNoChildren rejects element children on a fixed element")
func test_fixedElementNoChildren() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.fixedElementNoChildren,
schema: fixedStringSchema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("must not have element children") == true)
}
@Test("fixedElementNoChildren accepts a fixed element with only text")
func test_fixedElementNoChildren_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.fixedElementNoChildren,
schema: fixedStringSchema,
instance: "5",
rootName: "n",
)
#expect(errors.isEmpty)
}
@Test("fixedElementValueMatches rejects text that differs from the fixed value")
func test_fixedElementValueMatches() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.fixedElementValueMatches,
schema: fixedStringSchema,
instance: "6",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("is fixed and must be") == true)
}
@Test("fixedElementValueMatches accepts text equal to the fixed value")
func test_fixedElementValueMatches_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.fixedElementValueMatches,
schema: fixedStringSchema,
instance: "5",
rootName: "n",
)
#expect(errors.isEmpty)
}
}