PureYAML/Tests/FlowDashScalarTests.swift
Mihaela M 30dbd9316b fix(parsing): reject a lone dash before a flow indicator
In a flow collection a '-' that begins a plain scalar must be followed by
a non-indicator character. Immediately before a flow indicator ('[-]',
'[-, -]', '{a: -}') the dash is not a valid scalar, but the scanner
accepted it as the scalar '-'.

scanToken now rejects a '-' in flow context when the next character is a
flow indicator, before falling through to plain-scalar scanning. A dash
followed by ordinary content stays valid: negative numbers, a dash inside
a scalar, a dash starting a word, and a quoted dash are unaffected.

Measured against the official YAML test-suite, parse-level conformance
rises from 273/333 (82.0%) to 275/333 (82.6%) with no regression.
2026-06-28 01:00:57 +02:00

27 lines
922 B
Swift

@testable import PureYAML
import Testing
@Suite("A dash before a flow indicator")
struct FlowDashScalarTests {
@Test("A `-` immediately before a flow indicator is not a valid scalar", arguments: [
"[-]\n", // a lone dash before `]`
"---\n- [-, -]\n", // dashes before `,` and `]`
"{a: -}\n", // a lone dash before `}`
])
func test_dashBeforeFlowIndicatorRejected(_ yaml: String) throws {
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse(yaml)
}
}
@Test("A dash followed by a non-indicator character stays a valid scalar", arguments: [
"[-1, -2]\n", // negative numbers
"[a-b, c-d]\n", // a dash inside a scalar
"[-x, -y]\n", // a dash starting a word
"[\"-\", '-']\n", // a quoted dash
])
func test_validDashScalars(_ yaml: String) throws {
_ = try PureYAML.parse(yaml)
}
}