PureYAML/Tests/AnchoredMappingKeyTests.swift
Mihaela M 0ae88855da fix(parsing): allow an anchor or tag on a non-first mapping key
A block mapping pair after the first could not carry an anchor or tag on
its key (k: 1 then &b key2: 2): the first pair's key properties are
consumed when the mapping is entered, but subsequent pairs were not, so an
anchored key past the first failed with 'expected mapping key, found
anchor'. The pair parser now consumes leading key properties for every
pair, attaching them to the key node, and a block mapping continues across
an anchor or tag token that begins a key.

Normal mappings, anchored values, sequences, and explicit keys are
unaffected. Measured against the official YAML test-suite, parse-level
conformance rises from 235/333 (70.6%) to 238/333 (71.5%), with no
regression.
2026-06-27 00:21:38 +02:00

20 lines
652 B
Swift

@testable import PureYAML
import Testing
@Suite("Anchored mapping keys")
struct AnchoredMappingKeyTests {
@Test("A subsequent pair's key may carry an anchor")
func test_anchoredKeyOnSubsequentPair() throws {
let value = try PureYAML.parse("k: 1\n&b key2: 2\n")
#expect(value == .mapping(.init([
.init(key: "k", value: .int(1)),
.init(key: "key2", value: .int(2)),
])))
}
@Test("An anchored key on the first pair still parses")
func test_anchoredKeyOnFirstPair() throws {
#expect(try PureYAML.parse("&a c: 3\n") == .mapping(.init([.init(key: "c", value: .int(3))])))
}
}