PureYAML/Tests/FlowSequenceEmptyKeyTests.swift
Mihaela M c1610f1eec fix(parsing): treat colon-led flow sequence element as empty-key map
A flow sequence element that begins with `:` is a single-pair mapping with an
empty key, e.g. `[ : value ]` is `[{null: value}]`, but parseFlowSequence ran
the element through parseNode, which found the leading `:` and raised
"expected a YAML node".

Detect the leading mapping-value indicator and emit a single-pair flow mapping
with a null key and the following value (itself possibly empty), the sequence
counterpart of the scalar-key element `[ a: b ]` that already becomes a
mapping through its implicit-key marker. Plain elements and scalar-key mapping
elements are untouched, and the allowance is confined to flow sequence element
position.

Raises yaml-test-suite parse-level to 298/333 (89.5%) with the should-error
rejection rate unchanged. 426 tests pass.
2026-06-28 19:43:56 +02:00

43 lines
1.8 KiB
Swift

@testable import PureYAML
import Testing
/// A flow sequence element that begins with `:` is a single-pair mapping with an empty (null) key,
/// e.g. `[ : value ]` is `[{null: value}]`. An element that begins with a node followed by `:` already
/// becomes a mapping through the scalar key's implicit-key marker (`[ a: b ]`), and a plain element is
/// unaffected.
@Suite("Flow sequence empty key")
struct FlowSequenceEmptyKeyTests {
@Test("An element beginning with a colon is a single-pair mapping with an empty key")
func test_emptyKeyElement() throws {
#expect(try PureYAML.parse("[ : x ]\n") == .sequence([
.mapping(.init([.init(keyNode: .string(""), value: .string("x"))])),
]))
}
@Test("The empty key works with no space and an empty value")
func test_noSpaceAndEmptyValue() throws {
#expect(try PureYAML.parse("[: y]\n") == .sequence([
.mapping(.init([.init(keyNode: .string(""), value: .string("y"))])),
]))
#expect(try PureYAML.parse("[ : ]\n") == .sequence([
.mapping(.init([.init(keyNode: .string(""), value: .null)])),
]))
}
@Test("An empty-key element sits alongside ordinary mapping and scalar elements")
func test_mixedElements() throws {
#expect(try PureYAML.parse("[a: b, : c]\n") == .sequence([
.mapping(.init([.init(key: "a", value: .string("b"))])),
.mapping(.init([.init(keyNode: .string(""), value: .string("c"))])),
]))
}
@Test("Plain elements and scalar-key mapping elements are unchanged")
func test_unaffected() throws {
#expect(try PureYAML.parse("[a, b]\n") == .sequence([.string("a"), .string("b")]))
#expect(try PureYAML.parse("[a: b]\n") == .sequence([
.mapping(.init([.init(key: "a", value: .string("b"))])),
]))
}
}