A block scalar's leading empty lines must not be more indented than its
first non-empty line, so a blank line wider than the content (a 5-space
blank above 2-space content, or 2,3-space blanks above 1-space content) is
malformed. The scanner buffered the leading blanks without comparing their
indentation to the content, folding such input into the value.
The scanner now records the widest leading blank and rejects the scalar
when it exceeds the content indentation, established for both auto-detected
and explicit indicators. Indentation is counted in leading spaces, not
tab-expanded width, so a tab on a blank line ('␣\t') is one space rather
than a jump to the next tab stop, keeping such valid block scalars intact.
Measured against the official YAML test-suite, parse-level conformance
rises from 270/333 (81.1%) to 273/333 (82.0%) with no regression.
28 lines
1.2 KiB
Swift
28 lines
1.2 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
@Suite("Block scalar leading blank indentation")
|
|
struct BlockScalarLeadingBlankTests {
|
|
@Test("A leading blank line more indented than the content is rejected", arguments: [
|
|
"block scalar: >\n \n \n \n invalid\n", // blanks at 2,3 spaces over content at 1
|
|
"---\nblock scalar: |\n \n more spaces\n are invalid\n", // a 5-space blank over content at 2
|
|
])
|
|
func test_overIndentedLeadingBlankRejected(_ yaml: String) throws {
|
|
#expect(throws: PureYAML.Parsing.ParseError.self) {
|
|
try PureYAML.parse(yaml)
|
|
}
|
|
}
|
|
|
|
@Test("Leading blanks within the content indent, including a tab, stay valid")
|
|
func test_validLeadingBlanks() throws {
|
|
#expect(try PureYAML.parse("x: |\n\n content\n") == .mapping(.init([
|
|
.init(key: "x", value: .string("\ncontent\n")),
|
|
])))
|
|
#expect(try PureYAML.parse("x: |\n \n content\n") == .mapping(.init([
|
|
.init(key: "x", value: .string("\ncontent\n")),
|
|
])))
|
|
// A tab on a blank line is not indentation, so `␣\t` is one space, not a tab-stop jump.
|
|
_ = try PureYAML.parse("- >\n \t\n detected\n")
|
|
}
|
|
}
|