test(validation): expand fixture corpus
This commit is contained in:
parent
ad73413e68
commit
307d40c053
280
Tests/ValidationCorpusFixtures.swift
Normal file
280
Tests/ValidationCorpusFixtures.swift
Normal file
@ -0,0 +1,280 @@
|
||||
@testable import PureYAML
|
||||
|
||||
let validationCorpusSuccessFixtures: [ValidationCorpusSuccessFixture] = [
|
||||
.init(
|
||||
name: "required title present and draft absent",
|
||||
source: .yaml("""
|
||||
title: Example
|
||||
slug: example
|
||||
"""),
|
||||
validator: .blank
|
||||
.validating(requiredRootStringKeyRule("title"))
|
||||
.validating(forbiddenMappingKeyRule("draft", severity: .error)),
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'title' is missing",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Forbidden key 'draft' is present",
|
||||
path: .init([.key("draft")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "alias resolves to valid anchored mapping",
|
||||
source: .yaml("""
|
||||
shared: &shared {title: Shared, slug: shared}
|
||||
first: *shared
|
||||
second:
|
||||
title: Local
|
||||
slug: local
|
||||
"""),
|
||||
validator: .init(),
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'title'",
|
||||
path: .init([.key("shared"), .key("title")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'title'",
|
||||
path: .init([.key("first"), .key("title")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "punctuation-heavy direct value has unique keys",
|
||||
source: .value(.mapping(.init([
|
||||
.init(key: "/users/{id}", value: .string("show")),
|
||||
.init(key: "name.with.dot", value: .string("safe")),
|
||||
.init(key: "", value: .string("empty")),
|
||||
]))),
|
||||
validator: .init(),
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key '/users/{id}'",
|
||||
path: .init([.key("/users/{id}")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key ''",
|
||||
path: .init([.key("")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
let validationCorpusIssueFixtures: [ValidationCorpusIssueFixture] = [
|
||||
.init(
|
||||
name: "required root string key reports exact missing path",
|
||||
source: .yaml("""
|
||||
slug: missing-title
|
||||
"""),
|
||||
validator: .blank.validating(requiredRootStringKeyRule("title")),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'title' is missing",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'slug' is missing",
|
||||
path: .init([.key("slug")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "required root string key reports exact type mismatch path",
|
||||
source: .yaml("""
|
||||
title: 42
|
||||
"""),
|
||||
validator: .blank.validating(requiredRootStringKeyRule("title")),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'title' must be a string",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'title' is missing",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "forbidden key reports required absence failure",
|
||||
source: .yaml("""
|
||||
title: Example
|
||||
draft: true
|
||||
"""),
|
||||
validator: .blank
|
||||
.validating(requiredRootStringKeyRule("title"))
|
||||
.validating(forbiddenMappingKeyRule("draft", severity: .error)),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Forbidden key 'draft' is present",
|
||||
path: .init([.key("draft")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Required string key 'title' is missing",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "duplicate route names report only repeated occurrence",
|
||||
source: .yaml("""
|
||||
routes:
|
||||
- name: /users
|
||||
method: GET
|
||||
- name: /users
|
||||
method: POST
|
||||
- name: /status
|
||||
method: GET
|
||||
"""),
|
||||
validator: .blank.validating(uniqueRouteNamesRule()),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate route name '/users'",
|
||||
path: .init([.key("routes"), .index(1), .key("name")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate route name '/users'",
|
||||
path: .init([.key("routes"), .index(0), .key("name")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate route name '/status'",
|
||||
path: .init([.key("routes"), .index(2), .key("name")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "aliases preserve duplicate keys for validation",
|
||||
source: .yaml("""
|
||||
shared: &shared {title: First, title: Second}
|
||||
article: *shared
|
||||
"""),
|
||||
validator: .init(),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'title'",
|
||||
path: .init([.key("shared"), .key("title")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'title'",
|
||||
path: .init([.key("article"), .key("title")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'article'",
|
||||
path: .init([.key("article")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "merge-like duplicate keys remain inspectable until merge support exists",
|
||||
source: .yaml("""
|
||||
defaults: &defaults {retries: 1}
|
||||
service: {<<: *defaults, <<: {retries: 2}}
|
||||
"""),
|
||||
validator: .init(),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key '<<'",
|
||||
path: .init([.key("service"), .key("<<")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'retries'",
|
||||
path: .init([.key("service"), .key("<<"), .key("retries")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "punctuation-heavy direct duplicate keys escape paths exactly",
|
||||
source: .value(.mapping(.init([
|
||||
.init(key: "paths", value: .mapping(.init([
|
||||
.init(key: "/users/{id}", value: .string("first")),
|
||||
.init(key: "/users/{id}", value: .string("second")),
|
||||
.init(key: "", value: .string("empty one")),
|
||||
.init(key: "", value: .string("empty two")),
|
||||
.init(key: "quote\"slash\\line\nnext\tend", value: .string("one")),
|
||||
.init(key: "quote\"slash\\line\nnext\tend", value: .string("two")),
|
||||
]))),
|
||||
]))),
|
||||
validator: .init(),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key '/users/{id}'",
|
||||
path: .init([.key("paths"), .key("/users/{id}")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key ''",
|
||||
path: .init([.key("paths"), .key("")]),
|
||||
),
|
||||
.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate mapping key 'quote\"slash\\line\nnext\tend'",
|
||||
path: .init([.key("paths"), .key("quote\"slash\\line\nnext\tend")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
.init(
|
||||
name: "strict warning fixture throws exact warning collection",
|
||||
source: .yaml("""
|
||||
title: Example
|
||||
deprecated: true
|
||||
nested:
|
||||
deprecated: true
|
||||
"""),
|
||||
validator: .blank.validating(forbiddenMappingKeyRule("deprecated", severity: .warning)),
|
||||
expectedIssues: [
|
||||
.init(
|
||||
severity: .warning,
|
||||
reason: "Forbidden key 'deprecated' is present",
|
||||
path: .init([.key("deprecated")]),
|
||||
),
|
||||
.init(
|
||||
severity: .warning,
|
||||
reason: "Forbidden key 'deprecated' is present",
|
||||
path: .init([.key("nested"), .key("deprecated")]),
|
||||
),
|
||||
],
|
||||
forbiddenIssues: [
|
||||
.init(
|
||||
severity: .warning,
|
||||
reason: "Forbidden key 'deprecated' is present",
|
||||
path: .init([.key("title")]),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
161
Tests/ValidationCorpusSupport.swift
Normal file
161
Tests/ValidationCorpusSupport.swift
Normal file
@ -0,0 +1,161 @@
|
||||
@testable import PureYAML
|
||||
|
||||
enum ValidationCorpusSource {
|
||||
case yaml(String)
|
||||
case value(PureYAML.Model.Value)
|
||||
|
||||
func load() throws -> PureYAML.Model.Value {
|
||||
switch self {
|
||||
case let .yaml(yaml):
|
||||
try PureYAML.parse(yaml)
|
||||
case let .value(value):
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ValidationCorpusIssueFixture: CustomStringConvertible {
|
||||
var name: String
|
||||
var source: ValidationCorpusSource
|
||||
var validator: PureYAML.Validation.Validator
|
||||
var strict: Bool
|
||||
var expectedIssues: [PureYAML.Validation.Issue]
|
||||
var forbiddenIssues: [PureYAML.Validation.Issue]
|
||||
|
||||
init(
|
||||
name: String,
|
||||
source: ValidationCorpusSource,
|
||||
validator: PureYAML.Validation.Validator,
|
||||
strict: Bool = true,
|
||||
expectedIssues: [PureYAML.Validation.Issue],
|
||||
forbiddenIssues: [PureYAML.Validation.Issue] = [],
|
||||
) {
|
||||
self.name = name
|
||||
self.source = source
|
||||
self.validator = validator
|
||||
self.strict = strict
|
||||
self.expectedIssues = expectedIssues
|
||||
self.forbiddenIssues = forbiddenIssues
|
||||
}
|
||||
|
||||
var description: String {
|
||||
name
|
||||
}
|
||||
}
|
||||
|
||||
struct ValidationCorpusSuccessFixture: CustomStringConvertible {
|
||||
var name: String
|
||||
var source: ValidationCorpusSource
|
||||
var validator: PureYAML.Validation.Validator
|
||||
var forbiddenIssues: [PureYAML.Validation.Issue]
|
||||
|
||||
init(
|
||||
name: String,
|
||||
source: ValidationCorpusSource,
|
||||
validator: PureYAML.Validation.Validator,
|
||||
forbiddenIssues: [PureYAML.Validation.Issue] = [],
|
||||
) {
|
||||
self.name = name
|
||||
self.source = source
|
||||
self.validator = validator
|
||||
self.forbiddenIssues = forbiddenIssues
|
||||
}
|
||||
|
||||
var description: String {
|
||||
name
|
||||
}
|
||||
}
|
||||
|
||||
func requiredRootStringKeyRule(_ key: String) -> PureYAML.Validation.Rule {
|
||||
PureYAML.Validation.Rule(description: "Root requires string key '\(key)'") { context in
|
||||
guard case let .mapping(mapping) = context.subject else {
|
||||
return []
|
||||
}
|
||||
|
||||
guard let value = mapping[key] else {
|
||||
return [
|
||||
PureYAML.Validation.Issue(
|
||||
severity: .error,
|
||||
reason: "Required string key '\(key)' is missing",
|
||||
path: context.path.appending(.key(key)),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
guard case .string = value else {
|
||||
return [
|
||||
PureYAML.Validation.Issue(
|
||||
severity: .error,
|
||||
reason: "Required string key '\(key)' must be a string",
|
||||
path: context.path.appending(.key(key)),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
return []
|
||||
} when: { context in
|
||||
context.path.isRoot
|
||||
}
|
||||
}
|
||||
|
||||
func forbiddenMappingKeyRule(
|
||||
_ key: String,
|
||||
severity: PureYAML.Validation.Severity,
|
||||
) -> PureYAML.Validation.Rule {
|
||||
PureYAML.Validation.Rule(description: "Mapping forbids key '\(key)'") { context in
|
||||
guard case let .mapping(mapping) = context.subject,
|
||||
mapping[key] != nil
|
||||
else {
|
||||
return []
|
||||
}
|
||||
|
||||
return [
|
||||
PureYAML.Validation.Issue(
|
||||
severity: severity,
|
||||
reason: "Forbidden key '\(key)' is present",
|
||||
path: context.path.appending(.key(key)),
|
||||
),
|
||||
]
|
||||
} when: { context in
|
||||
if case .mapping = context.subject {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueRouteNamesRule() -> PureYAML.Validation.Rule {
|
||||
PureYAML.Validation.Rule(description: "Route names are unique") { context in
|
||||
guard case let .mapping(root) = context.subject,
|
||||
case let .sequence(routes)? = root["routes"]
|
||||
else {
|
||||
return []
|
||||
}
|
||||
|
||||
var seen = Set<String>()
|
||||
var issues: [PureYAML.Validation.Issue] = []
|
||||
for index in routes.indices {
|
||||
guard case let .mapping(route) = routes[index],
|
||||
case let .string(name)? = route["name"]
|
||||
else {
|
||||
continue
|
||||
}
|
||||
|
||||
if seen.insert(name).inserted {
|
||||
continue
|
||||
}
|
||||
|
||||
issues.append(.init(
|
||||
severity: .error,
|
||||
reason: "Duplicate route name '\(name)'",
|
||||
path: context.path
|
||||
.appending(.key("routes"))
|
||||
.appending(.index(index))
|
||||
.appending(.key("name")),
|
||||
))
|
||||
}
|
||||
return issues
|
||||
} when: { context in
|
||||
context.path.isRoot
|
||||
}
|
||||
}
|
||||
89
Tests/ValidationCorpusTests.swift
Normal file
89
Tests/ValidationCorpusTests.swift
Normal file
@ -0,0 +1,89 @@
|
||||
@testable import PureYAML
|
||||
import Testing
|
||||
|
||||
@Suite("Validation Corpus")
|
||||
struct ValidationCorpusTests {
|
||||
@Test("Fixture corpus succeeds without diagnostics", arguments: validationCorpusSuccessFixtures)
|
||||
func test_fixtureCorpusSucceedsWithoutDiagnostics(testCase: ValidationCorpusSuccessFixture) throws {
|
||||
let value = try testCase.source.load()
|
||||
let result = testCase.validator.collect(value)
|
||||
|
||||
#expect(result == PureYAML.Validation.Result())
|
||||
#expect(result.isValid)
|
||||
#expect(result.errors.isEmpty)
|
||||
#expect(result.warnings.isEmpty)
|
||||
#expect(try PureYAML.validate(value, using: testCase.validator).isEmpty)
|
||||
|
||||
for forbiddenIssue in testCase.forbiddenIssues {
|
||||
#expect(!result.issues.contains(forbiddenIssue))
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Fixture corpus reports exact validation issues", arguments: validationCorpusIssueFixtures)
|
||||
func test_fixtureCorpusReportsExactValidationIssues(testCase: ValidationCorpusIssueFixture) throws {
|
||||
let value = try testCase.source.load()
|
||||
let result = testCase.validator.collect(value)
|
||||
|
||||
#expect(result.issues == testCase.expectedIssues)
|
||||
#expect(result.issues.map(\.description) == testCase.expectedIssues.map(\.description))
|
||||
|
||||
for forbiddenIssue in testCase.forbiddenIssues {
|
||||
#expect(!result.issues.contains(forbiddenIssue))
|
||||
}
|
||||
|
||||
expectValidationError(value, using: testCase.validator, strict: testCase.strict) { collection in
|
||||
#expect(collection.issues == testCase.expectedIssues)
|
||||
#expect(collection.description == testCase.expectedIssues.map(\.description).joined(separator: "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Fixture corpus returns warnings in non-strict mode")
|
||||
func test_fixtureCorpusReturnsWarningsInNonStrictMode() throws {
|
||||
let fixture = validationCorpusIssueFixtures.first {
|
||||
$0.name == "strict warning fixture throws exact warning collection"
|
||||
}
|
||||
|
||||
let testCase = try #require(fixture)
|
||||
let value = try testCase.source.load()
|
||||
let warnings = try PureYAML.validate(value, using: testCase.validator, strict: false)
|
||||
|
||||
#expect(warnings == testCase.expectedIssues)
|
||||
#expect(warnings.allSatisfy { $0.severity == .warning })
|
||||
#expect(!warnings.contains(.init(
|
||||
severity: .warning,
|
||||
reason: "Forbidden key 'deprecated' is present",
|
||||
path: .init([.key("title")]),
|
||||
)))
|
||||
}
|
||||
|
||||
@Test("Fixture corpus preserves traversal order")
|
||||
func test_fixtureCorpusPreservesTraversalOrder() throws {
|
||||
let value = try PureYAML.parse(
|
||||
"""
|
||||
root:
|
||||
mode: legacy
|
||||
list:
|
||||
- mode: legacy
|
||||
""",
|
||||
)
|
||||
let validator = PureYAML.Validation.Validator.blank
|
||||
.validating(traceRule(label: "first"))
|
||||
.validating(forbiddenMappingKeyRule("mode", severity: .warning))
|
||||
.validating(legacyModeRule(severity: .error))
|
||||
|
||||
let result = validator.collect(value)
|
||||
|
||||
#expect(result.issues.map(\.description) == [
|
||||
"warning: first at root at root",
|
||||
"warning: first at $.root at $.root",
|
||||
"warning: Forbidden key 'mode' is present at $.root.mode",
|
||||
"warning: first at $.root.mode at $.root.mode",
|
||||
"error: Legacy mode is not allowed at $.root.mode",
|
||||
"warning: first at $.list at $.list",
|
||||
"warning: first at $.list[0] at $.list[0]",
|
||||
"warning: Forbidden key 'mode' is present at $.list[0].mode",
|
||||
"warning: first at $.list[0].mode at $.list[0].mode",
|
||||
"error: Legacy mode is not allowed at $.list[0].mode",
|
||||
])
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user