The conformance number was measured against a lossy oracle: the JSON projection (in.json) discards anchors, tags, and scalar styles, and exists for only about half the cases, so "passing" often meant only "parse did not throw". Measured against the official suite's own test.event event streams, the true exact conformance is 254 of 308 valid cases (40 emit a wrong event stream, 14 false-reject), well below the JSON-proxy figure. Add a canonical-event gate (Tests/CanonicalEventConformanceTests.swift) that serializes the parser's events into the yaml-test-suite yeast format and compares them exactly to each case's test.event, checking anchors, tags, scalar styles, flow vs block, and explicit document markers, for every valid case. The gate records the gap set and fails on any regression or unrecorded fix. All 402 test.event files are vendored alongside their inputs. To make the comparison exact, the parser Event model now records whether a document boundary was explicit (documentStart/documentEnd carry an `explicit` flag), distinguishing `---`/`...` from an implicit boundary. This resolves 89 cases that were previously correct but unverifiable without normalization. No parser behavior changed; this is measurement and model fidelity. The 40 wrong-event and 14 false-reject cases are the real remaining gaps, to be fixed against this gate.
145 lines
6.2 KiB
Swift
145 lines
6.2 KiB
Swift
import Foundation
|
|
@testable import PureYAML
|
|
import Testing
|
|
|
|
/// Authoritative conformance gate: serialize PureYAML's parse events into the
|
|
/// official YAML test suite "event" (yeast) format and compare EXACTLY to each
|
|
/// case's vendored `test.event`. Unlike the JSON-projection oracle, this checks
|
|
/// anchors, tags, scalar styles, flow vs block, and explicit document markers,
|
|
/// for every valid case (including those with no `in.json`). No normalization.
|
|
@Suite("Canonical Event Conformance")
|
|
struct CanonicalEventConformanceTests {
|
|
/// Valid suite cases whose event stream PureYAML does NOT yet emit exactly.
|
|
/// This is the authoritative gap set: a fix removes an id (and the test fails
|
|
/// until it is removed), a regression adds one (and the test fails). Of these,
|
|
/// 14 currently throw on valid input and 26 emit a wrong event stream.
|
|
static let knownEventGaps: Set<String> = [
|
|
// false-rejects (parseEvents throws on valid input)
|
|
"57H4", "58MP", "5T43", "6BFJ", "8XYN", "FH7J", "LX3P", "M5C3", "M5DY",
|
|
"Q9WF", "SKE5", "UKK6/00", "V9D5", "W5VH",
|
|
// wrong event stream (parses, but not the canonical events)
|
|
"2SXE", "4ABK", "4FJ6", "4MUZ/02", "652Z", "6CK3", "7BMT", "7FWL", "7ZZ5",
|
|
"8G76", "98YD", "9KAX", "AVM7", "CFD4", "CT4Q", "DBG4", "DC7X", "DK95/05",
|
|
"HM87/00", "HM87/01", "HS5T", "HWV9", "JEF9/02", "LP6E", "M2N8/00",
|
|
"M2N8/01", "M7A3", "NB6Z", "NKF9", "PW8X", "QF4Y", "QT73", "U3XV", "UGM3",
|
|
"UV7Q", "VJP3/01", "Y2GN", "Y79Y/002", "ZF4X", "ZK9H",
|
|
]
|
|
|
|
@Test("Every valid case emits canonical events, except the recorded gaps")
|
|
func test_canonicalEventConformance() throws {
|
|
var observedGaps: Set<String> = []
|
|
for testCase in CanonicalEventCorpus.validCases() {
|
|
guard let yaml = testCase.input, let expected = testCase.expectedEvents else { continue }
|
|
let events: [PureYAML.Parsing.Event]
|
|
do {
|
|
events = try PureYAML.Parsing.Parser().parseEvents(yaml)
|
|
} catch {
|
|
observedGaps.insert(testCase.id)
|
|
continue
|
|
}
|
|
let actual = events.map(CanonicalEventCorpus.serialize).joined(separator: "\n")
|
|
if actual != expected.trimmingTrailingNewline {
|
|
observedGaps.insert(testCase.id)
|
|
}
|
|
}
|
|
let regressions = observedGaps.subtracting(Self.knownEventGaps).sorted()
|
|
let fixed = Self.knownEventGaps.subtracting(observedGaps).sorted()
|
|
#expect(regressions.isEmpty, "Canonical-event REGRESSIONS (now wrong, were correct): \(regressions.joined(separator: " "))")
|
|
#expect(fixed.isEmpty, "Canonical-event cases now FIXED; remove from knownEventGaps: \(fixed.joined(separator: " "))")
|
|
}
|
|
}
|
|
|
|
enum CanonicalEventCorpus {
|
|
struct Case { let id: String }
|
|
|
|
static func validCases() -> [Case] {
|
|
// Every vendored case that is NOT an error case (no `error` marker).
|
|
guard let base = Bundle.module.url(forResource: "yaml-test-suite", withExtension: nil, subdirectory: "Fixtures") else { return [] }
|
|
let fileManager = FileManager.default
|
|
guard let walker = fileManager.enumerator(at: base, includingPropertiesForKeys: nil) else { return [] }
|
|
var out: [Case] = []
|
|
for case let url as URL in walker where url.lastPathComponent == "in.yaml" {
|
|
let dir = url.deletingLastPathComponent()
|
|
let id = dir.path.replacingOccurrences(of: base.path + "/", with: "")
|
|
if !fileManager.fileExists(atPath: dir.appendingPathComponent("error").path) {
|
|
out.append(Case(id: id))
|
|
}
|
|
}
|
|
return out.sorted { $0.id < $1.id }
|
|
}
|
|
|
|
static func url(_ id: String, _ name: String, _ ext: String?) -> URL? {
|
|
Bundle.module.url(forResource: name, withExtension: ext, subdirectory: "Fixtures/yaml-test-suite/" + id)
|
|
}
|
|
|
|
static func serialize(_ event: PureYAML.Parsing.Event) -> String {
|
|
switch event {
|
|
case .streamStart: "+STR"
|
|
case .streamEnd: "-STR"
|
|
case let .documentStart(explicit, _): explicit ? "+DOC ---" : "+DOC"
|
|
case let .documentEnd(explicit, _): explicit ? "-DOC ..." : "-DOC"
|
|
case let .sequenceStart(anchor, tag, style, _): "+SEQ" + (style == .flow ? " []" : "") + props(anchor, tag)
|
|
case .sequenceEnd: "-SEQ"
|
|
case let .mappingStart(anchor, tag, style, _): "+MAP" + (style == .flow ? " {}" : "") + props(anchor, tag)
|
|
case .mappingEnd: "-MAP"
|
|
case let .scalar(value, anchor, tag, style, _): "=VAL" + props(anchor, tag) + " " + styleChar(style) + escape(value)
|
|
case let .alias(anchor, _): "=ALI *" + anchor
|
|
}
|
|
}
|
|
|
|
static func props(_ anchor: String?, _ tag: String?) -> String {
|
|
var result = ""
|
|
if let anchor { result += " &" + anchor }
|
|
if let tag { result += " <" + tag + ">" }
|
|
return result
|
|
}
|
|
|
|
static func styleChar(_ style: PureYAML.Parsing.ScalarStyle) -> String {
|
|
switch style {
|
|
case .plain: ":"
|
|
case .singleQuoted: "'"
|
|
case .doubleQuoted: "\""
|
|
case .literal: "|"
|
|
case .folded: ">"
|
|
}
|
|
}
|
|
|
|
static func escape(_ value: String) -> String {
|
|
var out = ""
|
|
for scalar in value.unicodeScalars {
|
|
switch scalar {
|
|
case "\\": out += "\\\\"
|
|
case "\t": out += "\\t"
|
|
case "\n": out += "\\n"
|
|
case "\r": out += "\\r"
|
|
case "\u{08}": out += "\\b"
|
|
case "\u{00}": out += "\\0"
|
|
default: out.unicodeScalars.append(scalar)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
}
|
|
|
|
extension CanonicalEventCorpus.Case {
|
|
var input: String? {
|
|
guard let url = CanonicalEventCorpus.url(id, "in", "yaml") else { return nil }
|
|
return try? String(contentsOf: url, encoding: .utf8)
|
|
}
|
|
|
|
var expectedEvents: String? {
|
|
guard let url = CanonicalEventCorpus.url(id, "test", "event") else { return nil }
|
|
return try? String(contentsOf: url, encoding: .utf8)
|
|
}
|
|
}
|
|
|
|
private extension String {
|
|
var trimmingTrailingNewline: String {
|
|
var copy = self
|
|
while copy.hasSuffix("\n") {
|
|
copy.removeLast()
|
|
}
|
|
return copy
|
|
}
|
|
}
|