PureYAML/Tests/DoubleQuotedFoldingTests.swift
Mihaela M 08e1979085 fix(parsing): fold line breaks in multi-line double-quoted scalars
The double-quoted decoder appended real line breaks and continuation-line
indentation verbatim, so a multi-line "..." scalar kept its raw newlines
and leading whitespace instead of folding. It now applies YAML flow
folding at each unescaped break: a single break folds to a space, a blank
line to one newline, and whitespace adjacent to a break (the line's
trailing, the next line's leading) is stripped. First-line leading and
last-line trailing whitespace stay (they sit next to a quote, not a
break), and \n escapes are still decoded to literal newlines.

Measured against the official YAML test-suite, value-level conformance
rises from 135/169 (79.9%) to 143/169 (84.6%) with no regression.
Parse-level is unchanged: these documents already parsed, just to the
wrong tree, which only the value-level metric exposes.
2026-06-26 21:13:13 +02:00

28 lines
1.3 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Double-quoted scalar folding")
struct DoubleQuotedFoldingTests {
@Test("A multi-line double-quoted scalar folds breaks, blank lines, and adjacent whitespace")
func test_doubleQuotedLineFolding() throws {
// First-line leading and last-line trailing whitespace are kept (next to a quote, not a
// break); a single break folds to a space; a blank line folds to one newline; leading
// whitespace on continuation lines and trailing whitespace before a break are stripped.
let parsed = try PureYAML.parse("\" 1st non-empty\n\n 2nd non-empty \n\t3rd non-empty \"")
#expect(parsed == .string(" 1st non-empty\n2nd non-empty 3rd non-empty "))
}
@Test("An escaped newline stays literal while real breaks fold to a space")
func test_doubleQuotedEscapeVersusBreak() throws {
// A real line break between the two lines folds to a space; the trailing `\n` escape
// decodes to a literal newline.
let parsed = try PureYAML.parse("\"So does this\n quoted scalar.\\n\"")
#expect(parsed == .string("So does this quoted scalar.\n"))
}
@Test("A single-line double-quoted scalar is unchanged")
func test_doubleQuotedSingleLine() throws {
#expect(try PureYAML.parse("\"hello world\"") == .string("hello world"))
}
}