@testable import PureYAML import Testing /// A flow sequence element that begins with `:` is a single-pair mapping with an empty (null) key, /// e.g. `[ : value ]` is `[{null: value}]`. An element that begins with a node followed by `:` already /// becomes a mapping through the scalar key's implicit-key marker (`[ a: b ]`), and a plain element is /// unaffected. @Suite("Flow sequence empty key") struct FlowSequenceEmptyKeyTests { @Test("An element beginning with a colon is a single-pair mapping with an empty key") func test_emptyKeyElement() throws { #expect(try PureYAML.parse("[ : x ]\n") == .sequence([ .mapping(.init([.init(keyNode: .string(""), value: .string("x"))])), ])) } @Test("The empty key works with no space and an empty value") func test_noSpaceAndEmptyValue() throws { #expect(try PureYAML.parse("[: y]\n") == .sequence([ .mapping(.init([.init(keyNode: .string(""), value: .string("y"))])), ])) #expect(try PureYAML.parse("[ : ]\n") == .sequence([ .mapping(.init([.init(keyNode: .string(""), value: .null)])), ])) } @Test("An empty-key element sits alongside ordinary mapping and scalar elements") func test_mixedElements() throws { #expect(try PureYAML.parse("[a: b, : c]\n") == .sequence([ .mapping(.init([.init(key: "a", value: .string("b"))])), .mapping(.init([.init(keyNode: .string(""), value: .string("c"))])), ])) } @Test("Plain elements and scalar-key mapping elements are unchanged") func test_unaffected() throws { #expect(try PureYAML.parse("[a, b]\n") == .sequence([.string("a"), .string("b")])) #expect(try PureYAML.parse("[a: b]\n") == .sequence([ .mapping(.init([.init(key: "a", value: .string("b"))])), ])) } }