PureYAML/Tests/BlockScalarChompingTests.swift
Mihaela M d3ff3823a5 fix(parsing): preserve blank lines in block scalars
The scanner dropped every blank line within and after a block scalar, so
a literal block lost internal blank lines, a folded block turned a blank
line into a space rather than a newline, and the keep indicator could not
preserve trailing blank lines. Capture blank lines as empty content in
both the batch scan loop and the streaming TokenCursor loop, which had
drifted as duplicate copies. Rework blockScalarValue to apply literal
versus folded line semantics and clip/strip/keep chomping over the
captured lines; content without blank lines is unchanged. Re-pin the
multiline-scalar test whose expectation encoded the old dropped-blank
behavior, and add a chomping-and-folding test matrix.
2026-06-26 08:09:45 +02:00

46 lines
1.9 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Block Scalar Chomping And Folding")
struct BlockScalarChompingTests {
private func value(_ yaml: String) throws -> PureYAML.Model.Value? {
guard case let .mapping(mapping) = try PureYAML.parse(yaml) else {
return nil
}
return mapping["k"]
}
@Test("Literal clip preserves internal blank lines and keeps one trailing newline")
func test_literalClipPreservesInternalBlankAndClipsTrailing() throws {
#expect(try value("k: |\n a\n\n b\n\n\nnext: 1\n") == .string("a\n\nb\n"))
}
@Test("Literal strip preserves internal blank lines and drops trailing newlines")
func test_literalStripDropsTrailing() throws {
#expect(try value("k: |-\n a\n\n b\n\n\nnext: 1\n") == .string("a\n\nb"))
}
@Test("Literal keep preserves internal and trailing blank lines")
func test_literalKeepPreservesTrailingBlankLines() throws {
#expect(try value("k: |+\n a\n\n b\n\n\nnext: 1\n") == .string("a\n\nb\n\n\n"))
}
@Test("Folded turns a blank line into a newline and a single break into a space")
func test_foldedBlankBecomesNewlineAndBreakBecomesSpace() throws {
#expect(try value("k: >\n a\n\n b\nnext: 1\n") == .string("a\nb\n"))
#expect(try value("k: >\n a\n b\nnext: 1\n") == .string("a b\n"))
}
@Test("A run of N blank lines folds to N newlines")
func test_foldedConsecutiveBlankLinesBecomeThatManyNewlines() throws {
#expect(try value("k: >\n a\n\n\n b\nnext: 1\n") == .string("a\n\nb\n"))
}
@Test("A block scalar with no blank lines is unaffected")
func test_blockScalarWithoutBlankLinesUnchanged() throws {
#expect(try value("k: |\n a\n b\nnext: 1\n") == .string("a\nb\n"))
#expect(try value("k: |-\n a\n b\nnext: 1\n") == .string("a\nb"))
#expect(try value("k: >\n a\n b\nnext: 1\n") == .string("a b\n"))
}
}