PureYAML/Tests/AnchorOnNullValueTests.swift
Mihaela M e3caa5be0d fix(parsing): resolve an alias to an anchor on a null value
An anchor on an empty mapping value (`a: &anchor`) failed to register, so a
later `*anchor` errored as an undefined alias. Two bugs compounded:

  - The parser dispatched the anchor-only value position to a nested block
    mapping, absorbing the sibling key as the anchored node's child instead of
    emitting the empty anchored node. It now emits an empty anchored node when
    only a sibling mapping key follows, since the indented and inline-nested
    cases are already handled by earlier branches.
  - The stream composer registered an anchor by its raw scalar text, so an
    anchor on an empty scalar came back as "" and a typed scalar (`&a 42`) as
    text. It now registers the composed value, fixing a latent typed-anchor bug.

Fixes yaml-test-suite 6KGN; raises parse-level conformance to 306/333 (91.9%)
and value-level to 199/199 (100.0%), with the should-error rate unchanged.
2026-06-29 02:56:17 +02:00

27 lines
1.3 KiB
Swift

@testable import PureYAML
import Testing
@Suite("Anchor on a null or typed value resolves through its alias")
struct AnchorOnNullValueTests {
/// An anchor on a `key:` with no value anchors that key's implicit null node, so a later alias
/// resolves to null rather than failing as undefined (yaml-test-suite 6KGN). The empty value is
/// followed by a sibling mapping key, which must not be absorbed as a nested mapping.
@Test("An anchor on an empty mapping value registers a null node for its alias")
func test_anchorOnEmptyValueResolvesToNull() throws {
let value = try PureYAML.parse("a: &anchor\nb: *anchor\n")
let mapping = try requireMapping(value)
#expect(mapping?["a"] == .null)
#expect(mapping?["b"] == .null)
}
/// The anchored node is resolved before registration, so a typed scalar anchors its typed value
/// (here an integer), not its raw text: the alias must come back as the same `.int(42)`.
@Test("An anchor on a typed scalar registers the resolved value, not its raw text")
func test_anchorOnTypedScalarResolvesToTypedValue() throws {
let value = try PureYAML.parse("a: &a 42\nb: *a\n")
let mapping = try requireMapping(value)
#expect(mapping?["a"] == .int(42))
#expect(mapping?["b"] == .int(42))
}
}