A Rule was exactly a Check<Model.Value, Model.Value>: it fired on every node, switched on the value itself, and carried the root. It was therefore a second copy of the one check machinery, the duplication the validation idiom exists to prevent (principle 10), now also resolved for the tagged layer. Remove Rule and its Context. The model validator holds only checks; its default is the type-directed BuiltinCheck.mappingKeysAreUnique; validating takes a Check. Custom whole-tree rules become Check<Model.Value, Model.Value> with the former context.root now context.document. Behaviour is byte-identical: the mapping-key builtin reports the same issues at the same paths in the same DFS order, so the public validate/collect/blank surface is unchanged. All 399 tests pass; the coverage gate reconciles to 60 public types.
50 lines
2.6 KiB
Swift
50 lines
2.6 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
/// "Coverage is itself a tested law" (validation-rules.md): every rule in the default validator must
|
|
/// ship a failing fixture and a near-miss succeeding fixture, and a meta-test asserts the registry
|
|
/// covers exactly the default rule set, naming any gap. Adding a default rule without its fixtures,
|
|
/// or leaving a fixture for a rule that no longer ships, fails a test before it is deliberate.
|
|
@Suite("Validation finding-coverage law")
|
|
struct ValidationCoverageMetaTests {
|
|
/// One failing document and one near-miss (just-valid) document per default rule, keyed by the
|
|
/// rule's description (the same identity the validator removes and lists by).
|
|
private static let fixtures: [String: (failing: PureYAML.Model.Value, nearMiss: PureYAML.Model.Value)] = [
|
|
"Mapping keys are unique": (
|
|
failing: .mapping(.init([
|
|
.init(key: "a", value: .int(1)),
|
|
.init(key: "a", value: .int(2)),
|
|
])),
|
|
// The boundary the rule almost trips: two keys one character apart.
|
|
nearMiss: .mapping(.init([
|
|
.init(key: "a", value: .int(1)),
|
|
.init(key: "b", value: .int(2)),
|
|
]))
|
|
),
|
|
]
|
|
|
|
@Test("Every default rule has a failing and a near-miss fixture, and no fixture is orphaned")
|
|
func test_fixturesCoverExactlyTheDefaultRuleSet() {
|
|
let ruled = Set(PureYAML.Validation.Validator().validationDescriptions)
|
|
let covered = Set(Self.fixtures.keys)
|
|
let missing = ruled.subtracting(covered)
|
|
let orphaned = covered.subtracting(ruled)
|
|
#expect(missing.isEmpty, "default rules without coverage fixtures: \(missing.sorted())")
|
|
#expect(orphaned.isEmpty, "coverage fixtures for rules that no longer ship: \(orphaned.sorted())")
|
|
}
|
|
|
|
@Test("Each default check fails its failing fixture and passes its near-miss")
|
|
func test_eachCheckFailsAndNearMisses() throws {
|
|
// The default validator carries exactly the default checks, each keyed by a description the
|
|
// fixture registry is asserted (above) to cover one-for-one.
|
|
let validator = PureYAML.Validation.Validator()
|
|
for (description, fixture) in Self.fixtures {
|
|
#expect(throws: PureYAML.Validation.Issue.Collection.self, "check '\(description)' did not fail its failing fixture") {
|
|
try validator.validate(fixture.failing)
|
|
}
|
|
// The near miss must pass: nothing proves the check stays quiet on valid input otherwise.
|
|
_ = try validator.validate(fixture.nearMiss)
|
|
}
|
|
}
|
|
}
|