Port the located value tree onto the hand-rolled engine, reusing the
engine-agnostic located layer (Model.LocatedValue, LocatedPair,
SourcePosition, SourceRange, and the located composer helper) authored
on the conformance branch. The streaming composer now builds a parallel
located tree behind an opt-in tracksLocations flag, exposed as
parseLocated / parseStreamLocated; the plain Value path stays
byte-identical, gated behind the flag.
The scalar event carries the scanner token's end mark, so a located
scalar spans the whole token (end exclusive, one past the last
character) rather than being start-anchored. The hand-rolled scanner
already tracked token end marks, so this surfaces information the engine
was already computing.
Also stop emitting a spurious null document for a comment-only or empty
stream: YAML 1.2 gives such a stream zero documents, while the
single-document value API still maps it to null, so parse("") is
unchanged.
A whole-suite test proves the located projection equals a plain parse
for every passing case; the value, event-dump, and conformance suites
stay green (507 tests).
101 lines
4.3 KiB
Swift
101 lines
4.3 KiB
Swift
import Foundation
|
|
@testable import PureYAML
|
|
import Testing
|
|
|
|
/// Proves the opt-in located parse (``PureYAML/parseLocated(_:)`` and
|
|
/// ``PureYAML/parseStreamLocated(_:)``) is correct in two ways: its projected value is identical
|
|
/// to a plain parse for every case in the official suite (so its tree shape never drifts from the
|
|
/// value tree, even through merge keys and aliases), and it reports exact source coordinates on a
|
|
/// hand-checked document. The plain value path is untouched, so the conformance gates are unaffected.
|
|
@Suite("Located Value")
|
|
struct LocatedValueConformanceTests {
|
|
@Test("A located parse projects back to exactly the plain value")
|
|
func test_locatedProjectsToPlainValue() throws {
|
|
let yaml = """
|
|
name: web
|
|
port: 8080
|
|
items:
|
|
- first
|
|
- second
|
|
nested:
|
|
key: value
|
|
"""
|
|
let located = try PureYAML.parseLocated(yaml)
|
|
let plain = try PureYAML.parse(yaml)
|
|
#expect(located.value == plain)
|
|
}
|
|
|
|
@Test("A located parse reports exact line and column for each node")
|
|
func test_locatedReportsExactCoordinates() throws {
|
|
let yaml = """
|
|
name: web
|
|
port: 8080
|
|
items:
|
|
- first
|
|
- second
|
|
"""
|
|
guard case let .mapping(pairs, mappingRange) = try PureYAML.parseLocated(yaml) else {
|
|
Issue.record("expected a mapping at the document root")
|
|
return
|
|
}
|
|
// The mapping spans from its first key.
|
|
#expect(mappingRange.start.line == 1)
|
|
#expect(mappingRange.start.column == 1)
|
|
|
|
// name: web -> key at 1:1, value spanning columns 7..10 (end exclusive, one past 'web')
|
|
let name = pairs[0]
|
|
#expect(name.keyNode.stringValue == "name")
|
|
#expect(name.keyRange.start == .init(line: 1, column: 1))
|
|
#expect(name.value.range.start == .init(line: 1, column: 7))
|
|
#expect(name.value.range.end == .init(line: 1, column: 10))
|
|
|
|
// port: 8080 -> key at 2:1, value at 2:7
|
|
let port = pairs[1]
|
|
#expect(port.keyRange.start == .init(line: 2, column: 1))
|
|
#expect(port.value.range.start == .init(line: 2, column: 7))
|
|
|
|
// items: a sequence whose items sit at 4:5 and 5:5
|
|
guard case let .sequence(items, _) = pairs[2].value else {
|
|
Issue.record("expected a sequence for 'items'")
|
|
return
|
|
}
|
|
#expect(items.count == 2)
|
|
#expect(items[0].range.start == .init(line: 4, column: 5))
|
|
#expect(items[1].range.start == .init(line: 5, column: 5))
|
|
#expect(items[0].value == .string("first"))
|
|
}
|
|
|
|
/// Every case in the official suite that parses cleanly must have a located projection
|
|
/// identical to its plain parse, so the located tree shape never drifts from the value tree.
|
|
/// Opt-in like the plain conformance harness: set `PUREYAML_RUN_YAML_TEST_SUITE=1` and point
|
|
/// `PUREYAML_YAML_TEST_SUITE_DIR` at a checkout of the suite's case directories.
|
|
@Test("Every passing suite case projects to the same value as a plain parse")
|
|
func test_locatedMatchesPlainParseAcrossSuite() throws {
|
|
guard ProcessInfo.processInfo.environment["PUREYAML_RUN_YAML_TEST_SUITE"] == "1" else {
|
|
return
|
|
}
|
|
let manager = FileManager.default
|
|
let root = ProcessInfo.processInfo.environment["PUREYAML_YAML_TEST_SUITE_DIR"]
|
|
?? manager.currentDirectoryPath + "/.build/yaml-test-suite"
|
|
let names = try #require(try? manager.contentsOfDirectory(atPath: root), "suite corpus not found at \(root)")
|
|
var compared = 0
|
|
for name in names.sorted() {
|
|
let dir = root + "/" + name
|
|
guard manager.fileExists(atPath: dir + "/in.yaml"),
|
|
!manager.fileExists(atPath: dir + "/error"),
|
|
let yaml = try? String(contentsOfFile: dir + "/in.yaml", encoding: .utf8)
|
|
else {
|
|
continue
|
|
}
|
|
// Only compare cases that parse cleanly; the located projection must match exactly.
|
|
guard let plain = try? PureYAML.parseStream(yaml).map(\.value) else {
|
|
continue
|
|
}
|
|
let located = try PureYAML.parseStreamLocated(yaml).map(\.value)
|
|
#expect(located == plain, "\(name): located projection diverged from plain parse")
|
|
compared += 1
|
|
}
|
|
#expect(compared > 0)
|
|
}
|
|
}
|