Repository navigation
Expand file tree
/
Copy pathquickjsTest.ts
More file actions
107 lines (88 loc) · 2.47 KB
/
Copy pathquickjsTest.ts
File metadata and controls
107 lines (88 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { exit } from "std";
import { LATEST_VERSION } from "@cursorless/lib-common";
import type { ActionDescriptor, CommandLatest } from "@cursorless/lib-common";
import { activate } from "@cursorless/lib-talonjs-core";
import { talonMock } from "./talonMock";
let hasFailed = false;
async function runTests() {
try {
await activate(talonMock, "test");
console.log();
console.log("Running quickjs tests");
await test("testTake", testTake);
await test("testChuck", testChuck);
} catch {
hasFailed = true;
}
exit(hasFailed ? 1 : 0);
}
async function testTake() {
const testHelpers = talonMock.getTestHelpers();
const initialText = "Hello, world!";
testHelpers.setEditorState({
text: initialText,
selections: [{ anchor: 0, active: 0 }],
});
await runAction({
name: "setSelection",
target: {
type: "primitive",
modifiers: [{ type: "containingScope", scopeType: { type: "token" } }],
},
});
const { text, selections } = testHelpers.getFinalEditorState();
assert.equal(text, initialText);
assert.equal(selections.length, 1);
assert.equal(selections[0].anchor, 0);
assert.equal(selections[0].active, 5);
}
async function testChuck() {
const testHelpers = talonMock.getTestHelpers();
testHelpers.setEditorState({
text: "Hello, world!",
selections: [{ anchor: 0, active: 0 }],
});
await runAction({
name: "remove",
target: {
type: "primitive",
modifiers: [{ type: "containingScope", scopeType: { type: "token" } }],
},
});
const { text, selections } = testHelpers.getFinalEditorState();
assert.equal(text, ", world!");
assert.equal(selections.length, 1);
assert.equal(selections[0].anchor, 0);
assert.equal(selections[0].active, 0);
}
function runAction(action: ActionDescriptor) {
const command: CommandLatest = {
version: LATEST_VERSION,
usePrePhraseSnapshot: false,
action,
};
return talonMock
.getTestHelpers()
.contextActions.private_cursorless_talonjs_run_and_wait(
"cursorless.command",
command,
);
}
const assert = {
equal: (actual: unknown, expected: unknown) => {
if (actual !== expected) {
throw new Error(`Actual '${actual}' Expected '${expected}'`);
}
},
};
async function test(name: string, fn: () => Promise<void>) {
try {
await fn();
console.log(` * ${name}`);
} catch (error) {
console.error(` x ${name}`);
console.error(error);
hasFailed = true;
}
}
await runTests();