Debian Codegraph @

frontend/test/parser.test.ts

raw
1 import { join } from "node:path";
2
3 import type { Tree } from "@lezer/common";
4 import { TreeFragment } from "@lezer/common";
5 import { test } from "uvu";
6 import assert from "uvu/assert";
7 import { Language as TSLanguage, Parser as TSParser } from "web-tree-sitter";
8
9 import {
10 input_edit_for_fragments,
11 LezerTSParser,
12 } from "../src/codemirror/tree-sitter-parser";
13 import { is_non_empty } from "../src/lib/array";
14
15 async function load(): Promise<TSParser> {
16 await TSParser.init();
17 const path = join(
18 __dirname,
19 "..",
20 "src",
21 "codemirror",
22 "tree-sitter-beancount.wasm",
23 );
24 const lang = await TSLanguage.load(path);
25 const parser = new TSParser();
26 parser.setLanguage(lang);
27 return parser;
28 }
29 // eslint-disable-next-line @typescript-eslint/no-base-to-string
30 const tree_string = (t: Tree) => t.toString();
31
32 test("deduce an deletion at the end", () => {
33 const edit = input_edit_for_fragments(
34 [new TreeFragment(0, 315, { length: 733 } as Tree, 0, false, true)],
35 315,
36 );
37 assert.ok(edit);
38 assert.equal(edit.startIndex, 314);
39 assert.equal(edit.oldEndIndex, 733);
40 assert.equal(edit.newEndIndex, 315);
41 });
42
43 test("deduce an deletion at the start", () => {
44 const edit = input_edit_for_fragments(
45 [new TreeFragment(0, 418, { length: 733 } as Tree, 315, true, false)],
46 315,
47 );
48 assert.ok(edit);
49 assert.equal(edit.startIndex, 0);
50 assert.equal(edit.oldEndIndex, 316);
51 assert.equal(edit.newEndIndex, 1);
52 });
53
54 test("deduce a deletion in the middle", () => {
55 const tree = { length: 733 } as Tree;
56 const edit = input_edit_for_fragments(
57 [
58 new TreeFragment(0, 164, tree, 0, false, true),
59 new TreeFragment(164, 422, tree, 311, true, false),
60 ],
61 422,
62 );
63 assert.ok(edit);
64 assert.equal(edit.startIndex, 163);
65 assert.equal(edit.oldEndIndex, 476);
66 assert.equal(edit.newEndIndex, 165);
67 });
68
69 test("deduce an insertion in the middle", () => {
70 const tree = { length: 422 } as Tree;
71 const edit = input_edit_for_fragments(
72 [
73 new TreeFragment(0, 164, tree, 0, false, true),
74 new TreeFragment(475, 733, tree, -311, true, false),
75 ],
76 733,
77 );
78 assert.ok(edit);
79 assert.equal(edit.startIndex, 163);
80 assert.equal(edit.oldEndIndex, 165);
81 assert.equal(edit.newEndIndex, 476);
82 });
83
84 test("parse that reuses a single fragment", async () => {
85 const ts_parser = await load();
86 const parser = new LezerTSParser(ts_parser, [], "beancount_file");
87 const line = "2012-12-12 price USD 1 EUR\n";
88 const tree = parser.parse(line + line);
89 assert.equal(tree.length, line.length * 2);
90 assert.snapshot(
91 tree_string(tree),
92 "beancount_file(price(date,PRICE,currency,amount(number,currency)),price(date,PRICE,currency,amount(number,currency)))",
93 );
94 let fragments = TreeFragment.addTree(tree);
95 fragments = TreeFragment.applyChanges(fragments, [
96 { fromA: 0, toA: line.length, fromB: 0, toB: 0 },
97 ]);
98 assert.equal(fragments.length, 1);
99 assert.ok(is_non_empty(fragments));
100 const edit = input_edit_for_fragments(fragments, line.length);
101 assert.equal(edit?.startIndex, 0);
102 assert.equal(edit?.oldEndIndex, line.length + 1);
103 assert.equal(edit?.newEndIndex, 1);
104 const new_tree = parser.parse(line, fragments);
105 assert.snapshot(
106 tree_string(new_tree),
107 "beancount_file(price(date,PRICE,currency,amount(number,currency)))",
108 );
109 });
110
111 test("parse a single price directive", async () => {
112 const ts_parser = await load();
113 const parser = new LezerTSParser(ts_parser, [], "beancount_file");
114 const line = "2012-12-12 price USD 1 EUR\n";
115 const tree = parser.parse(line);
116 assert.snapshot(
117 tree_string(tree),
118 "beancount_file(price(date,PRICE,currency,amount(number,currency)))",
119 );
120
121 const partial_parse = parser.startParse(line);
122 // Advance directly returns the tree.
123 assert.ok(partial_parse.advance());
124 });
125
126 test.run();