@testable import PureYAML import Testing @Suite("Validation Built-in Rules") struct ValidationBuiltinRuleTests { @Test("Validates preserved value trees that ordinary YAML loaders may collapse") func test_validatesPreservedValueTreesThatOrdinaryYAMLLoadersMayCollapse() { let result = PureYAML.Validation.Validator().collect(preservedDuplicateKeyValue) #expect(result.values == expectedIssues) #expect(result.values.map(\.description) == [ "Duplicate mapping key 'title' at path: $.title", "Duplicate mapping key 'items' at path: $.items", "Duplicate mapping key '/users' at path: $.paths[\"/users\"]", "Duplicate mapping key 'name.with.dot' at path: $.items[0][\"name.with.dot\"]", ]) #expect(!result.values.contains(.init( severity: .error, reason: "Duplicate mapping key 'paths'", path: .init([.key("paths")]), ))) expectValidationError(preservedDuplicateKeyValue) { collection in #expect(collection.values == expectedIssues) #expect(collection.description == expectedIssues.map(\.description).joined(separator: "\n")) } } @Test("Mapping key uniqueness is scoped to mappings by type-directed dispatch") func test_mappingKeyUniquenessIsScopedToMappings() { let validator = PureYAML.Validation.Validator() // A scalar value carries no mapping, so the mapping-keyed check never fires on it. #expect(validator.collect(.string("subject")).values.isEmpty) // A mapping with a duplicate key reports exactly one issue, at the key's path. #expect(validator.collect(.mapping(.init([ .init(key: "title", value: .string("one")), .init(key: "title", value: .string("two")), ]))).values == [ .init( severity: .error, reason: "Duplicate mapping key 'title'", path: .init([.key("title")]), ), ]) } } private let preservedDuplicateKeyValue = PureYAML.Model.Value.mapping(.init([ .init(key: "title", value: .string("First")), .init(key: "title", value: .string("Second")), .init(key: "paths", value: .mapping(.init([ .init(key: "/users", value: .string("first")), .init(key: "/users", value: .string("second")), ]))), .init(key: "items", value: .sequence([ .mapping(.init([ .init(key: "name.with.dot", value: .string("one")), .init(key: "name.with.dot", value: .string("two")), ])), ])), .init(key: "items", value: .sequence([])), ])) private let expectedIssues: [PureYAML.Validation.ValidationError] = [ .init( severity: .error, reason: "Duplicate mapping key 'title'", path: .init([.key("title")]), ), .init( severity: .error, reason: "Duplicate mapping key 'items'", path: .init([.key("items")]), ), .init( severity: .error, reason: "Duplicate mapping key '/users'", path: .init([.key("paths"), .key("/users")]), ), .init( severity: .error, reason: "Duplicate mapping key 'name.with.dot'", path: .init([.key("items"), .index(0), .key("name.with.dot")]), ), ]