PureYAML/Tests/FlowMappingNoValueTests.swift
Mihaela M 31da488055 fix(parsing): allow a flow mapping entry with no value
A flow mapping entry may omit its `:` value indicator, in which case its key
maps to null: `{ a }` and `{ a, b }` are flow sets. parseFlowMappingPair
required a `:` after every key, so it rejected these entries, and rejected a
valueless key that spanned lines (`{ multi\n  line, a: b}`) with them.

Parse a value only when a `:` follows the key; otherwise emit a null value for
the key. A unit test that asserted `{ a, }` was an error is corrected to expect
the set `{a: null}`, the behaviour the yaml-test-suite cases 8KB6 and 9BXH
require.

Raises yaml-test-suite parse-level conformance to 304/333 (91.3%) and
value-level to 195/197, with the should-error rejection rate unchanged.
480 tests pass.
2026-06-29 01:42:02 +02:00

40 lines
1.4 KiB
Swift

@testable import PureYAML
import Testing
/// A flow mapping entry may omit its `:` value indicator, in which case the key maps to null: `{ a }`
/// and `{ a, b }` are flow sets. Entries with and without values mix freely, and a key without a value
/// may itself span lines (`{ multi`/`line, a: b}`).
@Suite("Flow mapping no value")
struct FlowMappingNoValueTests {
@Test("A single key with no value maps to null")
func test_singleKey() throws {
#expect(try PureYAML.parse("{ a }\n") == .mapping(.init([
.init(key: "a", value: .null),
])))
}
@Test("Several keys with no value form a set")
func test_set() throws {
#expect(try PureYAML.parse("{ a, b }\n") == .mapping(.init([
.init(key: "a", value: .null),
.init(key: "b", value: .null),
])))
}
@Test("Valued and valueless entries mix")
func test_mixed() throws {
#expect(try PureYAML.parse("{ a: 1, b }\n") == .mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "b", value: .null),
])))
}
@Test("A valueless key may span lines")
func test_multilineValuelessKey() throws {
#expect(try PureYAML.parse("{ multi\n line, a: b}\n") == .mapping(.init([
.init(key: "multi line", value: .null),
.init(key: "a", value: .string("b")),
])))
}
}