@testable import PureYAML import Testing /// Verifies the ported indentation engine reproduces libyaml's `roll_indent` / /// `unroll_indent` behavior, in particular the rule that fixes the block-mapping /// bug: a block collection opens only when content is strictly more indented than /// the current level. @Suite("LibYAML Indent Engine") struct LibYAMLIndentEngineTests { private let mark = PureYAML.Parsing.Mark.start @Test("a same-column key after an empty value is a sibling, not nested") func test_sameColumnKeyIsSibling() { // `- a:` then ` b: c`: the `-` opens a sequence at column 0; the key `a` // opens a mapping at column 2; `a` has no value; `b` arrives at column 2. var engine = PureYAML.Parsing.LibYAML.IndentEngine() engine.rollIndent(column: 0, number: -1, type: .blockSequenceStart, mark: mark) engine.rollIndent(column: 2, number: -1, type: .blockMappingStart, mark: mark) engine.unrollIndent(column: 2, mark: mark) engine.rollIndent(column: 2, number: -1, type: .blockMappingStart, mark: mark) engine.unrollIndent(column: -1, mark: mark) // One sequence, one mapping (b is a sibling of a, not nested under it). #expect(engine.tokens.map(\.type) == [ .blockSequenceStart, .blockMappingStart, .blockEnd, .blockEnd, ]) } @Test("a more-indented value opens a nested mapping") func test_moreIndentedValueNests() { // `a:` then ` b: c`: the value is more indented, so it nests. var engine = PureYAML.Parsing.LibYAML.IndentEngine() engine.rollIndent(column: 0, number: -1, type: .blockMappingStart, mark: mark) engine.unrollIndent(column: 2, mark: mark) engine.rollIndent(column: 2, number: -1, type: .blockMappingStart, mark: mark) engine.unrollIndent(column: -1, mark: mark) #expect(engine.tokens.map(\.type) == [ .blockMappingStart, .blockMappingStart, .blockEnd, .blockEnd, ]) } @Test("flow level makes indentation inert") func test_flowLevelIsInert() { var engine = PureYAML.Parsing.LibYAML.IndentEngine() engine.flowLevel = 1 engine.rollIndent(column: 4, number: -1, type: .blockMappingStart, mark: mark) engine.unrollIndent(column: 0, mark: mark) #expect(engine.tokens.isEmpty) #expect(engine.indent == -1) } }