import { describe, it, expect, test } from "vitest"; import { tokenize } from "@twinkleplop/core"; import { verify } from "@twinkleplop/core/compile"; import { grammar, raw_grammar } from "./index.js"; import fs from "node:fs"; import path from "node:path"; const test_path = path.join(import.meta.dirname, "..", "test"); const test_files = fs.readdirSync(test_path); const input_files = test_files .filter((file) => file.endsWith(".diff")) .map((file) => [file, fs.readFileSync(path.join(test_path, file), "utf-8")]); const output_modules = import.meta.glob("../test/*.js", { eager: true, }) as Record; const output_files = Object.entries(output_modules) .filter((module) => !module[0].includes("index.js")) .map((module) => [path.basename(module[0]), module[1].test]); input_files.sort((a, b) => a[0].localeCompare(b[0])); output_files .sort((a, b) => a[0].localeCompare(b[0])) .filter((file) => !file[0].includes(".output.js")); function get_tokens(input: string) { const result = tokenize(input, grammar); const tokens = []; for (let i = 0; i < result.tokens.length / 3; i++) { const type = result.token_types[result.tokens[i * 3]]; const start = result.tokens[i * 3 + 1]; const end = result.tokens[i * 3 + 2]; const match = input.substring(start, end); tokens.push({ type, start, end, match }); } return tokens; } describe("Diff Grammar", () => { test("verify", () => { const issues = verify(raw_grammar); expect(issues).toEqual([]); }); for (let i = 0; i < input_files.length; i++) { const test_name = input_files[i][0].replace(".diff", ""); it(`should tokenize ${test_name}`, () => { const tokens = get_tokens(input_files[i][1]); expect(tokens).toEqual(output_files[i][1]); }); } }); describe("stack balance", () => { // the state stack has 256 slots, a pop past it loses multi char patterns such as the hunk marker it("tokenizes the tail after 300 of every line form", () => { const unit = [ "diff --git a/f b/f", "index 1234567..89abcde 100644", "--- a/f", "+++ b/f", "@@ -1,2 +1,2 @@ fn main", "-a", "+b", "@@ -9,2 +9,2 @@", " c", "--- a/g\t2024-01-01 00:00:00", "+++ b/g\t2024-01-01 00:00:00", "@@ -1 +1 @@", "-x", "+y", "*** a/h\t2024-01-01", "--- b/h\t2024-01-01", "***************", "*** 1,2 ****", "! old", "--- 1,2 ----", "! new", "1c1", "< p", "---", "> q", "", ].join("\n"); const input = `${unit.repeat(300)}diff --git a/z b/z\n@@ -1 +1 @@\n+z\n`; const tail = get_tokens(input) .slice(-12) .map((t) => [t.type, t.match]); expect(tail).toEqual([ ["keyword", "diff "], ["keyword", "--git"], ["string", "a/z"], ["string", "b/z"], ["label", "@@ "], ["punctuation", "-"], ["number", "1"], ["punctuation", "+"], ["number", "1"], ["label", "@@"], ["inserted_marker", "+"], ["inserted", "z"], ]); }); });