In a flow sequence or mapping a comma separates entries, so one must
precede it. The parser advanced past a comma without checking that an
entry came before, accepting a leading comma ([ , a ]) or a repeated comma
([ a, , b ]) as empty entries. Each flow loop now tracks whether an entry
precedes the comma and rejects it otherwise. A single trailing comma
([ a, b, ]) and empty collections ([], {}) remain valid, since the closing
bracket is handled before that check.
Measured against the official YAML test-suite, parse-level conformance
rises from 249/333 (74.8%) to 251/333 (75.4%) with no regression.
26 lines
1.1 KiB
Swift
26 lines
1.1 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
@Suite("Flow collections reject empty entries")
|
|
struct FlowEmptyEntryTests {
|
|
@Test("A leading or repeated comma is an empty entry and is rejected", arguments: [
|
|
"[ , a, b, c ]\n", // leading comma in a sequence
|
|
"[ a, b, c, , ]\n", // repeated comma in a sequence
|
|
"{ , a: 1 }\n", // leading comma in a mapping
|
|
"{ a: 1, , b: 2 }\n", // repeated comma in a mapping
|
|
])
|
|
func test_emptyEntryIsRejected(_ yaml: String) throws {
|
|
#expect(throws: PureYAML.Parsing.ParseError.self) {
|
|
try PureYAML.parse(yaml)
|
|
}
|
|
}
|
|
|
|
@Test("A single trailing comma and empty collections stay valid")
|
|
func test_trailingCommaAndEmptyCollectionsAccepted() throws {
|
|
#expect(try PureYAML.parse("[ a, b, ]\n") == .sequence(.init([.string("a"), .string("b")])))
|
|
#expect(try PureYAML.parse("[]\n") == .sequence(.init([])))
|
|
#expect(try PureYAML.parse("{ a: 1, }\n") == .mapping(.init([.init(key: "a", value: .int(1))])))
|
|
#expect(try PureYAML.parse("{}\n") == .mapping(.init([])))
|
|
}
|
|
}
|