PureYAML/Tests/CommentInPlainScalarTests.swift
Mihaela M 8649f1722a fix(parsing): reject a comment interrupting a multi-line plain scalar
A comment terminates a plain scalar, so a multi-line plain scalar cannot
resume after one. The parser accepted 'key: word1' then a '#' line then
'  word2', and 'a' then 'b # ...' then 'c', merging the trailing content
back into the value ('word1 word2', 'a b c').

The scanner folds a genuine multi-line plain scalar into a single token
and splits it into a value-line scalar, an indent, and a further scalar
only when a comment broke its continuation folding. That token shape
therefore always denotes a comment-interrupted scalar, so the parser now
rejects it rather than re-merging. The re-merge path and its helper are
removed as they only ever served this malformed shape. A real multi-line
plain scalar and a trailing comment after a value stay valid.

Measured against the official YAML test-suite, parse-level conformance
rises from 264/333 (79.3%) to 266/333 (79.9%) with no regression.
2026-06-27 19:47:28 +02:00

33 lines
1.4 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Comments and multi-line plain scalars")
struct CommentInPlainScalarTests {
@Test("A comment interrupting a multi-line plain scalar is rejected", arguments: [
"key: word1\n# xxx\n word2\n", // a comment line between the value and its continuation
"plain: a\n b # end of scalar\n c\n", // a comment ends the scalar, then it tries to continue
])
func test_commentInterruptedScalarRejected(_ yaml: String) throws {
#expect(throws: PureYAML.Parsing.ParseError.self) {
try PureYAML.parse(yaml)
}
}
@Test("Multi-line plain scalars and trailing comments stay valid")
func test_validMultilineScalarsAndComments() throws {
#expect(try PureYAML.parse("plain: a\n b\n c\n") == .mapping(.init([
.init(key: "plain", value: .string("a b c")),
])))
#expect(try PureYAML.parse("key: a b # comment\n") == .mapping(.init([
.init(key: "key", value: .string("a b")),
])))
#expect(try PureYAML.parse("key: a\n b # comment\n") == .mapping(.init([
.init(key: "key", value: .string("a b")),
])))
#expect(try PureYAML.parse("a: 1\n# comment\nb: 2\n") == .mapping(.init([
.init(key: "a", value: .int(1)),
.init(key: "b", value: .int(2)),
])))
}
}