Skip to content

Commit 1c994c6

Browse files
Added migration tests
1 parent 2a92a04 commit 1c994c6

File tree

2 files changed

+347
-33
lines changed

2 files changed

+347
-33
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
import type { SnippetMap } from "@cursorless/common";
2+
import assert from "node:assert";
3+
import type { SnippetFile } from "talon-snippets";
4+
import { migrateLegacySnippet, type SpokenForms } from "./migrateSnippets";
5+
6+
interface Fixture {
7+
name: string;
8+
input: SnippetMap;
9+
output: SnippetFile;
10+
}
11+
12+
const spokenForms: SpokenForms = {
13+
insertion: {
14+
mySnippet: "snip",
15+
myPythonSnippet: "snip py",
16+
},
17+
insertionWithPhrase: {
18+
myPhraseSnippet: "phrase",
19+
},
20+
wrapper: {
21+
"myWrapperSnippet.foo": "foo",
22+
},
23+
};
24+
25+
const fixtures: Fixture[] = [
26+
{
27+
name: "Empty map",
28+
input: {},
29+
output: {
30+
snippets: [],
31+
},
32+
},
33+
{
34+
name: "Empty definitions",
35+
input: {
36+
mySnippet: {
37+
definitions: [],
38+
},
39+
},
40+
output: {
41+
header: {
42+
name: "mySnippet",
43+
phrases: ["snip"],
44+
description: undefined,
45+
insertionScopes: undefined,
46+
variables: [],
47+
},
48+
snippets: [],
49+
},
50+
},
51+
{
52+
name: "Basic",
53+
input: {
54+
mySnippet: {
55+
description: "Example description",
56+
definitions: [
57+
{
58+
scope: { langIds: ["plaintext"] },
59+
body: ["Hello, $0, world!"],
60+
},
61+
],
62+
},
63+
},
64+
output: {
65+
header: {
66+
name: "mySnippet",
67+
description: "Example description",
68+
phrases: ["snip"],
69+
insertionScopes: undefined,
70+
variables: [],
71+
},
72+
snippets: [
73+
{
74+
name: undefined,
75+
description: undefined,
76+
phrases: undefined,
77+
languages: ["plaintext"],
78+
insertionScopes: undefined,
79+
variables: [],
80+
body: ["Hello, $0, world!"],
81+
},
82+
],
83+
},
84+
},
85+
{
86+
name: "Insertion phrase",
87+
input: {
88+
myPhraseSnippet: {
89+
description: "Example description",
90+
definitions: [
91+
{
92+
scope: { langIds: ["plaintext"] },
93+
body: ["Hello, $foo, world!"],
94+
variables: {
95+
foo: { formatter: "snakeCase" },
96+
},
97+
},
98+
],
99+
},
100+
},
101+
output: {
102+
header: {
103+
name: "myPhraseSnippet",
104+
description: "Example description",
105+
phrases: ["phrase"],
106+
insertionScopes: undefined,
107+
variables: [],
108+
},
109+
snippets: [
110+
{
111+
name: undefined,
112+
description: undefined,
113+
phrases: undefined,
114+
languages: ["plaintext"],
115+
insertionScopes: undefined,
116+
variables: [
117+
{
118+
name: "foo",
119+
insertionFormatters: ["SNAKE_CASE"],
120+
wrapperPhrases: undefined,
121+
},
122+
],
123+
body: ["Hello, $foo, world!"],
124+
},
125+
],
126+
},
127+
},
128+
{
129+
name: "Wrapper phrase",
130+
input: {
131+
myWrapperSnippet: {
132+
definitions: [
133+
{
134+
scope: { langIds: ["plaintext"] },
135+
body: ["Hello, $foo, world!"],
136+
},
137+
],
138+
},
139+
},
140+
output: {
141+
header: {
142+
name: "myWrapperSnippet",
143+
description: undefined,
144+
phrases: undefined,
145+
insertionScopes: undefined,
146+
variables: [
147+
{
148+
name: "foo",
149+
wrapperPhrases: ["foo"],
150+
},
151+
],
152+
},
153+
snippets: [
154+
{
155+
name: undefined,
156+
description: undefined,
157+
phrases: undefined,
158+
languages: ["plaintext"],
159+
insertionScopes: undefined,
160+
variables: [],
161+
body: ["Hello, $foo, world!"],
162+
},
163+
],
164+
},
165+
},
166+
{
167+
name: "Multiple definitions",
168+
input: {
169+
mySnippet: {
170+
definitions: [
171+
{
172+
scope: { langIds: ["plaintext"] },
173+
body: ["Hello, $0 plain world!"],
174+
},
175+
{
176+
scope: { langIds: ["python"] },
177+
body: ["Hello, $0 python world!"],
178+
},
179+
],
180+
},
181+
},
182+
output: {
183+
header: {
184+
name: "mySnippet",
185+
description: undefined,
186+
phrases: ["snip"],
187+
insertionScopes: undefined,
188+
variables: [],
189+
},
190+
snippets: [
191+
{
192+
name: undefined,
193+
description: undefined,
194+
phrases: undefined,
195+
languages: ["plaintext"],
196+
insertionScopes: undefined,
197+
variables: [],
198+
body: ["Hello, $0 plain world!"],
199+
},
200+
{
201+
name: undefined,
202+
description: undefined,
203+
phrases: undefined,
204+
languages: ["python"],
205+
insertionScopes: undefined,
206+
variables: [],
207+
body: ["Hello, $0 python world!"],
208+
},
209+
],
210+
},
211+
},
212+
{
213+
name: "Multiple snippets",
214+
input: {
215+
mySnippet: {
216+
definitions: [
217+
{
218+
scope: { langIds: ["plaintext"] },
219+
body: ["Hello, $0 plain world!"],
220+
},
221+
],
222+
},
223+
myPythonSnippet: {
224+
definitions: [
225+
{
226+
scope: { langIds: ["python"] },
227+
body: ["Hello, $0 python world!"],
228+
},
229+
],
230+
},
231+
},
232+
output: {
233+
snippets: [
234+
{
235+
name: "mySnippet",
236+
description: undefined,
237+
phrases: ["snip"],
238+
languages: ["plaintext"],
239+
insertionScopes: undefined,
240+
variables: [],
241+
body: ["Hello, $0 plain world!"],
242+
},
243+
{
244+
name: "myPythonSnippet",
245+
description: undefined,
246+
phrases: ["snip py"],
247+
languages: ["python"],
248+
insertionScopes: undefined,
249+
variables: [],
250+
body: ["Hello, $0 python world!"],
251+
},
252+
],
253+
},
254+
},
255+
];
256+
257+
suite("Migrate snippets", async function () {
258+
fixtures.forEach((fixture) => {
259+
test(fixture.name, () => runTest(fixture.input, fixture.output));
260+
});
261+
});
262+
263+
function runTest(input: SnippetMap, expectedOutput: SnippetFile) {
264+
const [actualOutput] = migrateLegacySnippet(spokenForms, input);
265+
266+
assert.deepStrictEqual(actualOutput, expectedOutput);
267+
}

0 commit comments

Comments
 (0)