PureYAML/Tests/ValidationCheckMachineryTests.swift
Mihaela M 2e45ecaffe feat(validation): add type-directed Check dispatch
Introduce the core of the validation-rules idiom: a Check<Subject> with a
positive description, a when predicate, and a Bool or multi-error check
that fires only on values of its Subject type wherever they occur. AnyCheck
erases the subject and dispatches by runtime type, guarding an optional or
a wrong type from matching. The validator gains a checks list, a
validating(_:) overload, and a traversal that offers each node the values
it may specialise on. BuiltinCheck.mappingKeysAreUnique is the
type-directed counterpart of the legacy rule.

The machinery has its own negative tests (wrong type, optional, false
predicate yield nothing; same type twice yields two errors) and the builtin
ships a failing and a near-miss test. The coverage gate catches the five
new public types, which are classified as validation machinery. The default
validator is unchanged, so this is additive; retiring the legacy Rule,
moving the default onto the check, the combinator algebra, the error
rendering, and de-duplicating the Tagged validator are the remaining steps.
2026-06-28 07:23:23 +02:00

100 lines
4.1 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Type-directed check machinery")
struct ValidationCheckMachineryTests {
private typealias Check = PureYAML.Validation.Check<PureYAML.Model.Mapping>
private typealias AnyCheck = PureYAML.Validation.AnyCheck
private typealias Issue = PureYAML.Validation.Issue
private var mapping: PureYAML.Model.Mapping {
.init([.init(key: "a", value: .int(1))])
}
private func alwaysFails(description: String = "always fails") -> Check {
Check(description: description) { context in
[Issue(severity: .error, reason: "boom", path: context.path)]
}
}
// MARK: machinery negatives (the dispatch must not fire where it should not)
@Test("A check fires on its subject type and produces its error")
func test_firesOnSubjectType() {
let erased = AnyCheck(alwaysFails())
#expect(erased.apply(to: mapping, at: .root, in: .null).count == 1)
}
@Test("A check does not fire on a value of another type")
func test_doesNotFireOnWrongType() {
let erased = AnyCheck(alwaysFails())
// A Model.Value (even one wrapping a mapping) is not a Model.Mapping, so nothing fires.
#expect(erased.apply(to: PureYAML.Model.Value.mapping(mapping), at: .root, in: .null).isEmpty)
#expect(erased.apply(to: PureYAML.Model.Key.string("a"), at: .root, in: .null).isEmpty)
}
@Test("A false `when` predicate yields no errors")
func test_falsePredicateYieldsNothing() {
let gated = Check(description: "gated", check: { _ in
[Issue(severity: .error, reason: "boom", path: .root)]
}, when: { _ in false })
#expect(AnyCheck(gated).apply(to: mapping, at: .root, in: .null).isEmpty)
}
// MARK: positive control (proving the negatives are not vacuous)
@Test("The same subject type occurring twice yields two errors")
func test_sameTypeTwiceYieldsTwoErrors() {
// A document with two mappings; an always-failing Check<Mapping> must fire on both.
let document = PureYAML.Model.Value.sequence(.init([
.mapping(.init([.init(key: "a", value: .int(1))])),
.mapping(.init([.init(key: "b", value: .int(2))])),
]))
let validator = PureYAML.Validation.Validator.blank.validating(alwaysFails())
let issues = validator.collect(document).issues
#expect(issues.count == 2)
}
// MARK: the type-directed builtin (failing and a near miss)
@Test("The type-directed mappingKeysAreUnique builtin catches duplicate keys")
func test_builtinCatchesDuplicateKeys() {
let validator = PureYAML.Validation.Validator.blank.validating(
PureYAML.Validation.BuiltinCheck.mappingKeysAreUnique,
)
#expect(validator.validationDescriptions == ["Mapping keys are unique"])
#expect(throws: PureYAML.Validation.Issue.Collection.self) {
try validator.validate(.mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "a", value: .int(2)),
])))
}
}
@Test("The builtin passes a near-miss document whose keys are unique")
func test_builtinPassesUniqueKeys() throws {
let validator = PureYAML.Validation.Validator.blank.validating(
PureYAML.Validation.BuiltinCheck.mappingKeysAreUnique,
)
// The boundary the rule almost trips: two keys that differ by one character.
_ = try validator.validate(.mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "b", value: .int(1)),
])))
}
@Test("Type-directed dispatch reaches a nested mapping")
func test_dispatchReachesNestedMapping() {
let validator = PureYAML.Validation.Validator.blank.validating(
PureYAML.Validation.BuiltinCheck.mappingKeysAreUnique,
)
let nested = PureYAML.Model.Value.mapping(.init([
.init(key: "outer", value: .mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "a", value: .int(2)),
]))),
]))
#expect(!validator.collect(nested).errors.isEmpty)
}
}