Blank lines between a block scalar header and its first content line come before the indent token, so the earlier blank-line capture skipped them. Buffer leading blanks while the block indentation is still pending and flush them as empty content once the indent is established, in the shared indentation path used by both scan loops. Also fix folded bodies to emit a break for a leading blank rather than folding it into a leading space. This closes the last block-scalar blank-line edge from #57.
59 lines
2.6 KiB
Swift
59 lines
2.6 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 more-indented line inside a folded block stays literal")
|
|
func test_foldedMoreIndentedLineStaysLiteral() throws {
|
|
#expect(try value("k: >\n normal\n indented\n back\nnext: 1\n") == .string("normal\n indented\nback\n"))
|
|
#expect(try value("k: >\n a\n code\nnext: 1\n") == .string("a\n code\n"))
|
|
}
|
|
|
|
@Test("Leading blank lines before the first content line are preserved")
|
|
func test_leadingBlankLinesPreserved() throws {
|
|
#expect(try value("k: |\n\n a\n b\nnext: 1\n") == .string("\na\nb\n"))
|
|
#expect(try value("k: |\n\n\n a\nnext: 1\n") == .string("\n\na\n"))
|
|
#expect(try value("k: >\n\n a\n b\nnext: 1\n") == .string("\na b\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"))
|
|
}
|
|
}
|