PureYAML/Tests/ExplicitKeyTests.swift
Mihaela M 58f30b4343 fix(parsing): an explicit key with no value maps to null
The block mapping parser required a ':' after every explicit key ('? key'),
so a key with no value, including several '? key' entries in a row (a set),
failed to parse with 'expected mapping value'. An explicit key with no
following ':' now takes an implicit null value, shared with the existing
'key:'-with-nothing-after path via one emitNullMappingValue helper.

Explicit keys with values and complex keys in general are unaffected. A
test that asserted '? [a, b]' followed by a block entry is a missing-value
error was corrected: the key is a valid null entry, and the trailing
sequence is now rejected as extra content (a second root node), which is
the more accurate diagnosis.

Measured against the official YAML test-suite, parse-level conformance
rises from 229/333 (68.8%) to 235/333 (70.6%), with several of the newly
parsed documents also value-correct and no regression.
2026-06-26 22:28:13 +02:00

35 lines
954 B
Swift

@testable import PureYAML
import Testing
@Suite("Explicit keys")
struct ExplicitKeyTests {
@Test("Explicit keys with no value each map to null")
func test_explicitKeysWithoutValuesMapToNull() throws {
// Several `? key` entries in a row (a set, for instance) are valid; each maps to null.
let value = try PureYAML.parse("""
? a
? b
c:
""")
#expect(value == .mapping(.init([
.init(key: "a", value: .null),
.init(key: "b", value: .null),
.init(key: "c", value: .null),
])))
}
@Test("Explicit keys with values still parse")
func test_explicitKeysWithValues() throws {
let value = try PureYAML.parse("""
? a
: b
? c
: d
""")
#expect(value == .mapping(.init([
.init(key: "a", value: .string("b")),
.init(key: "c", value: .string("d")),
])))
}
}