PureYAML/Tests/ExplicitKeyMultilineFoldTests.swift
Mihaela M 143624be08 fix(parsing): fold an explicit key ending at its value colon
A plain scalar continuation that ends at a `:` is normally a new implicit
mapping key, so the scalar stops before it. Under an explicit key (`? foo`
then `bar :`) the `:` is the key's own value indicator instead, so the line is
still key content. The scalar dropped that line and the parser stalled, so
`? foo` then `bar : baz` failed rather than yielding the key `foo bar`.

When the explicit-key marker is pending, fold the continuation line in and
stop, leaving the `:` to be scanned as the value indicator. An implicit key
keeps the previous behaviour, so a continuation ending at `:` still begins a
new key.

The test-suite rates are unchanged, since no comparable case isolates the
block form and the flow form needs separate explicit-key work, but block
explicit keys spanning lines now fold correctly. 466 tests pass.
2026-06-29 00:59:18 +02:00

34 lines
1.4 KiB
Swift

@testable import PureYAML
import Testing
/// An explicit key (`? foo`) may span lines: its plain scalar folds across the break, and a `:` on
/// the last key line is the value indicator, not a new entry. Under an implicit key a continuation
/// line ending at `:` is instead a new mapping key, so the scalar stops before it, unchanged.
@Suite("Explicit key multiline fold")
struct ExplicitKeyMultilineFoldTests {
@Test("An explicit key folds a continuation line that ends at the value colon")
func test_continuationEndingAtColon() throws {
#expect(try PureYAML.parse("? foo\n bar : baz\n") == .mapping(.init([
.init(key: "foo bar", value: .string("baz")),
])))
}
@Test("An explicit key with the colon on its own line still folds")
func test_colonOnOwnLine() throws {
#expect(try PureYAML.parse("? foo\n bar\n: baz\n") == .mapping(.init([
.init(key: "foo bar", value: .string("baz")),
])))
}
@Test("An implicit key is unaffected: a continuation ending at a colon stays a new key")
func test_implicitKeyUnchanged() throws {
#expect(try PureYAML.parse("foo: bar\n") == .mapping(.init([
.init(key: "foo", value: .string("bar")),
])))
#expect(try PureYAML.parse("a: 1\nb: 2\n") == .mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "b", value: .int(2)),
])))
}
}