PureYAML/Tests/DirectiveTests.swift
Mihaela M 27c423dbb9 fix(parsing): require an explicit document after a directive
The YAML grammar requires every directive to head an explicit document,
so a directive must be followed by a '---' marker. The scanner accepted a
directive with no document at all: '%YAML 1.2' alone, or '%YAML 1.2' then
a '...' document-end marker, parsed without error.

The scanner now records the line of a directive awaiting its document,
clears it at a document start, and rejects a still-pending directive at a
document-end marker and at stream end. Both scan drivers finalize through
the same stream-end path, so the check lives in one helper called from
each. The rule applies to any directive, including an unrecognized one
(which is otherwise still ignored); the directive test that relied on an
unrecognized directive parsing without a '---' is corrected to include
one, since that leniency is not permitted by the grammar.

Measured against the official YAML test-suite, parse-level conformance
rises from 253/333 (76.0%) to 255/333 (76.6%) with no regression.
2026-06-27 17:18:47 +02:00

18 lines
753 B
Swift

@testable import PureYAML
import Testing
@Suite("Directives")
struct DirectiveTests {
@Test("An unrecognized directive is ignored, not rejected")
func test_unknownDirectiveIsIgnored() throws {
// The YAML spec ignores a directive it does not recognize (with a warning), so the
// document still parses. Directives must still be followed by an explicit `---`, so these
// cases include one: the point here is that `%FOO` itself does not cause a rejection.
#expect(try PureYAML.parse("%FOO bar baz\n---\n\"foo\"\n") == .string("foo"))
#expect(
try PureYAML.parse("%FOO bar\n---\nvalue: one\n")
== .mapping(.init([.init(key: "value", value: .string("one"))])),
)
}
}