@testable import PureYAML import Testing /// A flow collection followed by a `: ` indicator on the same line is an implicit block mapping key, /// e.g. `[a, b]: value` or `{a: 1}: value`. The scanner detects this by looking ahead from the flow /// opener to the matching close and emits the same implicit-key marker a scalar key gets, so the flow /// node is parsed as the key through the ordinary mapping path. A block implicit key must lie on one /// line, so a flow collection spanning a line break before its `:` is not a key and stays an error. @Suite("Flow collection as mapping key") struct FlowCollectionAsMappingKeyTests { @Test("A flow sequence before a colon is a mapping key") func test_flowSequenceKey() throws { #expect(try PureYAML.parse("[a, b]: c\n") == .mapping(.init([ .init(keyNode: .sequence([.string("a"), .string("b")]), value: .string("c")), ]))) } @Test("A flow mapping before a colon is a mapping key") func test_flowMappingKey() throws { #expect(try PureYAML.parse("{a: 1}: b\n") == .mapping(.init([ .init(keyNode: .mapping(.init([.init(key: "a", value: .int(1))])), value: .string("b")), ]))) } @Test("A colon inside a quoted scalar in the flow key does not end the lookahead early") func test_quotedColonInsideKey() throws { #expect(try PureYAML.parse("['x: y']: z\n") == .mapping(.init([ .init(keyNode: .sequence([.string("x: y")]), value: .string("z")), ]))) } @Test("A flow node not followed by a colon stays an ordinary node, not a key") func test_flowNodeWithoutColonIsNotAKey() throws { #expect(try PureYAML.parse("[a, b, c]\n") == .sequence([.string("a"), .string("b"), .string("c")])) #expect(try PureYAML.parse("key: [a, b]\n") == .mapping(.init([ .init(key: "key", value: .sequence([.string("a"), .string("b")])), ]))) } @Test("A flow collection spanning a line break before its colon is not a valid key") func test_multilineFlowKeyRejected() { // The flow sequence runs across two lines, so it cannot be a single-line implicit key. #expect(throws: PureYAML.Parsing.ParseError.self) { try PureYAML.parse("[23\n]: 42\n") } } @Test("A colon without a following space is not a mapping value, so the flow node is not a key") func test_colonWithoutSpaceIsNotAKey() { #expect(throws: PureYAML.Parsing.ParseError.self) { try PureYAML.parse("[a]:b\n") } } }