@testable import PureYAML import Testing @Suite("Type-directed check machinery") struct ValidationCheckMachineryTests { private typealias Check = PureYAML.Validation.Check 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 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) } }