PureYAML/Tests/TaggedKeyUniquenessTests.swift
Mihaela Mihaljevic bf2dc4ece7 refactor(validation): rename framework to match the validation idiom
The validation module already implemented the composable-rule idiom under
its own names (Check/Issue/SubjectContext) and a value-type Validator with
duplicated traversal machinery per layer. Renamed the core types to the
canonical names (Validation/ValidationError/ValidationErrorCollection/
ValidationContext/BuiltinValidation/AnyValidation), converted Validator to
a reference-type class with the two-tier default-rule split, and genericized
it over both Subject and Document via a new Traversable protocol so the
model and tagged layers share one walk engine instead of two copies. Wired
the JSON-representability rule set into the same shape and closed the two
missing pieces (unwrap combinator, JSON coverage/config-pin tests).
2026-07-12 18:10:00 +02:00

50 lines
2.0 KiB
Swift

@testable import PureYAML
import Testing
/// The tagged validator enforces mapping-key uniqueness, the invariant the coverage manifest claims
/// for `Tagged.Mapping` and `Tagged.Node`. `Tagged.Mapping` preserves every parsed pair, so a
/// duplicate key survives into the tree and must be reported, exactly as in the plain model.
@Suite("Tagged mapping key uniqueness")
struct TaggedKeyUniquenessTests {
@Test("A duplicate key in a tagged mapping is reported at the key's path")
func test_reportsDuplicateTaggedKey() throws {
let node = try PureYAML.parseTagged("a: 1\na: 2")
#expect(PureYAML.Tagged.Validator().collect(node).values == [
.init(
severity: .error,
reason: "Duplicate mapping key 'a'",
path: .init([.key("a")]),
),
])
#expect(throws: PureYAML.Validation.ValidationErrorCollection.self) {
try PureYAML.Tagged.Validator().validate(node)
}
}
@Test("The near miss, a tagged mapping whose keys differ by one character, passes")
func test_uniqueTaggedKeysPass() throws {
let node = try PureYAML.parseTagged("a: 1\nb: 2")
_ = try PureYAML.Tagged.Validator().validate(node)
}
@Test("A duplicate key nested under a tag is reported at its nested path")
func test_reportsNestedDuplicateTaggedKey() throws {
let node = try PureYAML.parseTagged("outer:\n a: 1\n a: 2")
#expect(PureYAML.Tagged.Validator().collect(node).values == [
.init(
severity: .error,
reason: "Duplicate mapping key 'a'",
path: .init([.key("outer"), .key("a")]),
),
])
}
@Test("The default tagged validator runs exactly its two built-in checks")
func test_defaultTaggedCheckDescriptions() {
#expect(PureYAML.Tagged.Validator().validationDescriptions == [
"Built-in tags are supported and kind-consistent",
"Mapping keys are unique",
])
}
}