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).
70 lines
3.2 KiB
Swift
70 lines
3.2 KiB
Swift
import PureYAML
|
|
@testable import PureYAMLJSON
|
|
import Testing
|
|
|
|
/// "Coverage is itself a tested law" (validation-rules.md), applied to the JSON-representability
|
|
/// validator, the same discipline `ValidationCoverageMetaTests` in the core target applies to the model
|
|
/// and tagged validators.
|
|
@Suite("JSON representability finding-coverage law")
|
|
struct JSONCoverageMetaTests {
|
|
private struct Fixture {
|
|
let check: PureYAML.Validation.AnyValidation<PureYAML.Model.Value>
|
|
let failing: PureYAML.Model.Value
|
|
let nearMiss: PureYAML.Model.Value
|
|
}
|
|
|
|
private static let fixtures: [Fixture] = [
|
|
.init(
|
|
check: .init(PureYAML.Validation.BuiltinValidation.mappingKeysAreUnique),
|
|
failing: .mapping(.init([
|
|
.init(key: "a", value: .int(1)),
|
|
.init(key: "a", value: .int(2)),
|
|
])),
|
|
nearMiss: .mapping(.init([
|
|
.init(key: "a", value: .int(1)),
|
|
.init(key: "b", value: .int(2)),
|
|
])),
|
|
),
|
|
.init(
|
|
check: .init(PureYAML.Validation.BuiltinValidation.mappingKeysAreStrings),
|
|
failing: .mapping(.init([.init(keyNode: .sequence([.int(1)]), value: .int(2))])),
|
|
// The boundary: a string key, the only key shape JSON permits.
|
|
nearMiss: .mapping(.init([.init(key: "a", value: .int(2))])),
|
|
),
|
|
.init(
|
|
check: .init(PureYAML.Validation.BuiltinValidation.numbersAreFinite),
|
|
failing: .mapping(.init([.init(key: "x", value: .double(.infinity))])),
|
|
// The boundary: the largest finite double, one step from non-finite.
|
|
nearMiss: .mapping(.init([.init(key: "x", value: .double(.greatestFiniteMagnitude))])),
|
|
),
|
|
]
|
|
|
|
@Test("The JSON representability validator's descriptions are pinned")
|
|
func test_representabilityDescriptionsArePinned() {
|
|
#expect(PureYAML.JSON.Validator.representability.validationDescriptions == [
|
|
"Mapping keys are unique",
|
|
"Mapping keys are strings",
|
|
"Numbers are finite",
|
|
])
|
|
}
|
|
|
|
@Test("JSON fixtures cover exactly the representability validator's default checks")
|
|
func test_fixturesCoverDefaults() {
|
|
let ruled = Set(PureYAML.JSON.Validator.representability.validationDescriptions)
|
|
let covered = Set(Self.fixtures.map(\.check.description))
|
|
#expect(ruled.subtracting(covered).isEmpty, "JSON default checks without fixtures: \(ruled.subtracting(covered).sorted())")
|
|
#expect(covered.subtracting(ruled).isEmpty, "JSON fixtures for checks that no longer ship: \(covered.subtracting(ruled).sorted())")
|
|
}
|
|
|
|
@Test("Each JSON check fails its failing fixture and passes its near miss in isolation")
|
|
func test_checksFailAndNearMiss() throws {
|
|
for fixture in Self.fixtures {
|
|
let isolated = PureYAML.Validation.Validator<PureYAML.Model.Value, PureYAML.Model.Value>(nonReferenceDefaultValidations: [fixture.check])
|
|
#expect(throws: PureYAML.Validation.ValidationErrorCollection.self, "check '\(fixture.check.description)' did not fail its failing fixture") {
|
|
try isolated.validate(fixture.failing)
|
|
}
|
|
_ = try isolated.validate(fixture.nearMiss)
|
|
}
|
|
}
|
|
}
|