PureYAML/Tests/TaggedKeyUniquenessTests.swift
Mihaela M 47960a85c8 feat(validation): enforce tagged mapping-key uniqueness
The coverage manifest claimed Tagged.Mapping and Tagged.Node validate key
uniqueness, but the tagged validator's only default check was the built-in
tag check: the claim was false. Tagged.Mapping keeps every parsed pair, so a
duplicate key survives into the tree and is as invalid as in the plain model.

Add Tagged.BuiltinCheck.mappingKeysAreUnique, a Check<Mapping, Node> on the
one validation framework, to the tagged validator's defaults; the traversal
now offers each mapping to the checks. It reports each duplicate at its key
path, with failing, near-miss, and nested fixtures, and the two default
tagged checks are pinned by description.

Correct the Model.Value coverage reason: merges and aliases resolve at parse
time, so they are not a validation-phase invariant. Every validated manifest
entry now names an invariant a check actually enforces. 403 tests pass.
2026-06-28 17:03:20 +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).issues == [
.init(
severity: .error,
reason: "Duplicate mapping key 'a'",
path: .init([.key("a")]),
),
])
#expect(throws: PureYAML.Validation.Issue.Collection.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).issues == [
.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",
])
}
}