PureYAML/Tests/InlineBlockSequenceValueTests.swift
Mihaela M b0dc31922b fix(parsing): reject a block sequence inline as a mapping value
A block sequence cannot be an implicit key's value on the same line as its
':' (key: - a); the value must begin on a following line, or use flow
style. The scanner accepted the inline block entry and parsed it as the
key's sequence value.

The block-entry case now rejects a '-' that starts a sequence when the
line already holds an implicit-key mapping value, reusing the same
same-line-pair flag as the inline nested mapping rule. As there, an
explicit- or null-key ':' opens its line and does not set the flag, so a
compact sequence value after it stays valid, as do a sequence on the next
line, a nested sequence, a flow sequence, and a negative number (key: -1).

Measured against the official YAML test-suite, parse-level conformance
rises from 260/333 (78.1%) to 261/333 (78.4%) with no regression.
2026-06-27 18:35:58 +02:00

35 lines
1.6 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Block sequence as an inline mapping value")
struct InlineBlockSequenceValueTests {
@Test("A block sequence on the same line as an implicit key's `:` is rejected")
func test_inlineBlockSequenceValueRejected() throws {
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse("key: - a\n - b\n")
}
}
@Test("Block sequences positioned correctly, and look-alikes, stay valid", arguments: [
"key:\n - a\n - b\n", // sequence indented on the next line
"key:\n- a\n- b\n", // sequence at the key's indentation
"- - a\n", // a nested block sequence
"- key: value\n", // a sequence item that is a mapping
"key: [a, b]\n", // a flow sequence value
"key: -1\n", // a negative number, not a block entry
"a: b - c\n", // a dash inside a plain scalar value
])
func test_validSequencesStillParse(_ yaml: String) throws {
_ = try PureYAML.parse(yaml)
}
@Test("An explicit key's value on its own `:` line is not rejected as inline")
func test_explicitKeyCompactSequenceValueNotRejected() throws {
// The `:` opens the line, so it is an explicit-key value indicator and the compact sequence
// after it is allowed: the reject above is specific to an implicit key. The element folding
// of an explicit-key compact sequence is a separate, pre-existing concern, so this asserts
// only that the construct still parses rather than its exact value.
_ = try PureYAML.parse("? k\n: - a\n - b\n")
}
}