import Testing
@testable import PureXML
/// Per-rule isolation tests (Validation rules XSD): content and wildcard rules.
@Suite("Validation rules XSD: content and wildcard rules")
struct ValidationRuleXSDContentTests {
// MARK: emptyContentForbidsChildren
private let emptyContentSchema = """
"""
@Test("emptyContentForbidsChildren rejects children on an empty-content element")
func test_emptyContentForbidsChildren() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.emptyContentForbidsChildren,
schema: emptyContentSchema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason == "element must be empty")
}
@Test("emptyContentForbidsChildren accepts a truly empty element")
func test_emptyContentForbidsChildren_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.emptyContentForbidsChildren,
schema: emptyContentSchema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: simpleContentForbidsChildren / simpleContentTypeValid
private let simpleContentSchema = """
"""
@Test("simpleContentForbidsChildren rejects element children under simpleContent")
func test_simpleContentForbidsChildren() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.simpleContentForbidsChildren,
schema: simpleContentSchema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason == "element must not have children")
}
@Test("simpleContentForbidsChildren accepts text-only simpleContent")
func test_simpleContentForbidsChildren_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.simpleContentForbidsChildren,
schema: simpleContentSchema,
instance: "5",
rootName: "n",
)
#expect(errors.isEmpty)
}
@Test("simpleContentTypeValid rejects text invalid against the simpleContent type")
func test_simpleContentTypeValid() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.simpleContentTypeValid,
schema: simpleContentSchema,
instance: "x",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.hasPrefix("content:") == true)
}
@Test("simpleContentTypeValid accepts text valid against the simpleContent type")
func test_simpleContentTypeValid_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.simpleContentTypeValid,
schema: simpleContentSchema,
instance: "5",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: textForbidden
private let elementOnlyOptionalSchema = """
"""
@Test("textForbidden rejects text under element-only content")
func test_textForbidden() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.textForbidden,
schema: elementOnlyOptionalSchema,
instance: "hello",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason == "element must not contain text")
}
@Test("textForbidden accepts element-only content with no text")
func test_textForbidden_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.textForbidden,
schema: elementOnlyOptionalSchema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
// MARK: wildcardElementUndeclared
private let strictWildcardElementSchema = """
"""
@Test("wildcardElementUndeclared rejects a strict wildcard match with no global declaration")
func test_wildcardElementUndeclared() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.wildcardElementUndeclared,
schema: strictWildcardElementSchema,
instance: "",
rootName: "n",
)
#expect(errors.count == 1)
#expect(errors.first?.reason.contains("no declaration for wildcard-matched element") == true)
}
@Test("wildcardElementUndeclared accepts an optional wildcard with no match")
func test_wildcardElementUndeclared_succeeds() throws {
let errors = try ValidationRuleTestSupport.xsdErrors(
PureXML.Validation.XSD.wildcardElementUndeclared,
schema: strictWildcardElementSchema,
instance: "",
rootName: "n",
)
#expect(errors.isEmpty)
}
}