PureYAML/Tests/DocumentMarkerNodeTests.swift
Mihaela M 688f0d5c12 fix(parsing): fold a document-root plain scalar across column-1 lines
A plain scalar at the document root failed to parse when it spanned
multiple lines at column 1 (`a\nb\nc`): the continuation check required a
continuation to be indented more than the scalar, but a root scalar has
nothing enclosing it, so its enclosing-block indent is -1 and a
continuation may start at column 1. The base indentation now returns -1 at
the root, and the continuation stops at a document marker (--- / ...) so a
scalar never folds across a document boundary.

A stream test that asserted two bare plain lines (first / second) were
rejected as extra content was corrected: that input is a valid multi-line
scalar; genuine extra content (a flow node followed by a scalar) is still
rejected, which the test now uses.

Measured against the official YAML test-suite, parse-level conformance
rises from 221/333 (66.4%) to 227/333 (68.2%), with four more documents
also now value-correct and no regression across the existing suite.
2026-06-26 21:30:49 +02:00

33 lines
1.6 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Document marker root nodes")
struct DocumentMarkerNodeTests {
@Test("A block scalar header on the document-marker line is recognized, not discarded")
func test_blockScalarHeaderOnDocumentMarkerLine() throws {
// `--- >` / `--- |`: the marker scanner used to skip the rest of the line, dropping the
// block header so the body was mis-read as a plain scalar (folded, no trailing newline).
#expect(try PureYAML.parse("--- >\n a\n b\n") == .string("a b\n"))
#expect(try PureYAML.parse("--- |\n a\n b\n") == .string("a\nb\n"))
}
@Test("A scalar root node may begin on the document-marker line")
func test_scalarRootNodeOnDocumentMarkerLine() throws {
#expect(try PureYAML.parse("--- folded text\n") == .string("folded text"))
// A node still on its own line below the marker keeps working.
#expect(try PureYAML.parse("---\nplain text\n") == .string("plain text"))
}
@Test("A root plain scalar folds across lines, even at column 1")
func test_rootPlainScalarFoldsAcrossLines() throws {
// A document-root scalar has nothing enclosing it, so a continuation may sit at column 1.
#expect(try PureYAML.parse("a\nb \n c\nd\n\ne\n") == .string("a b c d\ne"))
}
@Test("A root plain scalar stops at a document marker, not folding across it")
func test_rootPlainScalarStopsAtDocumentMarker() throws {
let documents = try PureYAML.parseStream("document 1\n---\ndocument 2\n")
#expect(documents.map(\.value) == [.string("document 1"), .string("document 2")])
}
}