|
| 1 | +import * as YAML from "yaml"; |
| 2 | +import { parse } from "./parse.ts"; |
| 3 | +import { type BlockValue, type Mapping } from "./types.ts"; |
| 4 | + |
| 5 | +const blockStyles = ["|", ">", "|+", "|-", ">+", ">-"]; |
| 6 | +const trailingSpace = ["", " ", " ", " \t"]; |
| 7 | +const newlines = ["", "\n", "\n\n", "\n\n\n"]; |
| 8 | + |
| 9 | +const snippets = blockStyles.flatMap(blockStyle => |
| 10 | + trailingSpace.flatMap(space => |
| 11 | + newlines.flatMap(lines => [ |
| 12 | + `value: ${blockStyle}\n x\n${space}${lines}`, |
| 13 | + `value: ${blockStyle}\n${space}${lines}`, |
| 14 | + ]), |
| 15 | + ), |
| 16 | +); |
| 17 | + |
| 18 | +const parseBlock = (code: string) => { |
| 19 | + const root = parse(code); |
| 20 | + const document = root.children[0]; |
| 21 | + const mapping = document.children[1].children[0] as Mapping; |
| 22 | + const mappingItem = mapping.children[0]; |
| 23 | + const mappingValue = mappingItem.children[1]; |
| 24 | + const block = mappingValue.children[0] as BlockValue; |
| 25 | + return { document, block }; |
| 26 | +}; |
| 27 | + |
| 28 | +describe("Block values", () => { |
| 29 | + for (const code of snippets) { |
| 30 | + test(JSON.stringify(code), () => { |
| 31 | + const codeWithoutEndMark = code; |
| 32 | + const codeWithEndMark = `${code}\n...`; |
| 33 | + |
| 34 | + const result: Map<boolean, { value: string; block: BlockValue }> = |
| 35 | + new Map(); |
| 36 | + |
| 37 | + for (const { code, documentEndMarker } of [ |
| 38 | + { code: codeWithoutEndMark, documentEndMarker: false }, |
| 39 | + { code: codeWithEndMark, documentEndMarker: true }, |
| 40 | + ]) { |
| 41 | + const { document, block } = parseBlock(code); |
| 42 | + expect(document.documentEndMarker).toBe(documentEndMarker); |
| 43 | + expect(block.type).toBeOneOf(["blockLiteral", "blockFolded"]); |
| 44 | + const expectedValue: string = YAML.parse(code).value; |
| 45 | + expect(block.value).toBe(expectedValue); |
| 46 | + result.set(documentEndMarker, { |
| 47 | + value: expectedValue, |
| 48 | + block, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + const blockWithoutEndMark = result.get(false)!.block; |
| 53 | + const blockWithEndMark = result.get(true)!.block; |
| 54 | + const { chomping } = blockWithoutEndMark; |
| 55 | + |
| 56 | + // I don't know what the value should be |
| 57 | + if (chomping === "keep") { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + expect(blockWithoutEndMark.value).toBe(blockWithEndMark.value); |
| 62 | + }); |
| 63 | + } |
| 64 | +}); |
0 commit comments