PureYAML/Tests/MultilineFlowKeyTests.swift
Mihaela M 5c99086a38 fix(parsing): let a flow plain key span lines
A plain scalar continuation stops before a line ending at `:`, since a block
implicit key may not span lines and the next line is a new key. A flow key may
span lines, so the same rule wrongly split `{ multi\n  line: v }`: the scalar
returned just `multi` and the parser mis-read `line: v` as a separate entry.

Fold across the `:` in flow as well, leaving the `:` to be scanned as the
value indicator, so the key folds to `multi line`. A block implicit key keeps
the previous behaviour and still stops before the `:`.

Raises yaml-test-suite parse-level conformance to 301/333 (90.4%) and
value-level to 193/195, with the should-error rejection rate unchanged.
476 tests pass.
2026-06-29 01:38:33 +02:00

38 lines
1.5 KiB
Swift

@testable import PureYAML
import Testing
/// A plain scalar key in a flow collection may span lines: the continuation folds across the line
/// break even when the line ends at the `:` value indicator, since a flow key, unlike a block implicit
/// key, is allowed to be multi-line. A block implicit key still stops before such a `:`, treating the
/// next line as a new key.
@Suite("Multiline flow key")
struct MultilineFlowKeyTests {
@Test("A flow mapping key folds across a line break")
func test_flowMappingKey() throws {
#expect(try PureYAML.parse("{ multi\n line: v }\n") == .mapping(.init([
.init(key: "multi line", value: .string("v")),
])))
}
@Test("A flow sequence plain element still folds, unchanged")
func test_flowSequenceElement() throws {
#expect(try PureYAML.parse("[ multi\n line ]\n") == .sequence([.string("multi line")]))
}
@Test("An ordinary single-line flow mapping is unchanged")
func test_singleLineFlowMapping() throws {
#expect(try PureYAML.parse("{a: 1, b: 2}\n") == .mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "b", value: .int(2)),
])))
}
@Test("A block implicit key does not span lines")
func test_blockImplicitKeyDoesNotSpan() {
// `foo` then `bar: baz` is a value and a new key, not the key `foo bar`, so this is an error.
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse("foo\n bar: baz\n")
}
}
}