Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/getTestableNode.ts.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { outdent } from 'outdent';
7
import { afterAll, expect, suite, test } from 'vitest';
8
import { _dispose } from '../../node/parserWithCaching';
9
import { WASMLanguage } from '../../node/treeSitterLanguages';
10
import { srcWithAnnotatedTestableNode } from './getTestableNode.util';
11
12
suite('getTestableNode - ts', () => {
13
afterAll(() => _dispose());
14
15
function run(annotatedSrc: string) {
16
return srcWithAnnotatedTestableNode(
17
WASMLanguage.TypeScript,
18
annotatedSrc,
19
);
20
}
21
22
test('function declaration', async () => {
23
const result = await run(
24
outdent`
25
function <<add>>(a: number, b: number): number {
26
return a + b;
27
}
28
`,
29
);
30
expect(result).toMatchInlineSnapshot(`
31
"<NODE(function_declaration)>function <IDENT>add</IDENT>(a: number, b: number): number {
32
return a + b;
33
}</NODE(function_declaration)>"
34
`);
35
});
36
37
test('method', async () => {
38
const result = await run(
39
outdent`
40
class Foo {
41
<<method>>(a: number, b: number): number {
42
return a + b;
43
}
44
}
45
`,
46
);
47
expect(result).toMatchInlineSnapshot(`
48
"class Foo {
49
<NODE(method_definition)><IDENT>method</IDENT>(a: number, b: number): number {
50
return a + b;
51
}</NODE(method_definition)>
52
}"
53
`);
54
});
55
56
test('public method', async () => {
57
const result = await run(
58
outdent`
59
class Foo {
60
<<method>>(a: number, b: number): number {
61
return a + b;
62
}
63
}
64
`,
65
);
66
expect(result).toMatchInlineSnapshot(`
67
"class Foo {
68
<NODE(method_definition)><IDENT>method</IDENT>(a: number, b: number): number {
69
return a + b;
70
}</NODE(method_definition)>
71
}"
72
`);
73
});
74
75
test('several public methods', async () => {
76
const result = await run(
77
outdent`
78
class Foo {
79
method(a: number, b: number): number {
80
return a + b;
81
}
82
83
method2(a: number, b: number): number {
84
return a + b;
85
}
86
87
method3(a: number, b: number): number {
88
return a + b;
89
}
90
91
<<method4>>(a: number, b: number): number {
92
return a + b;
93
}
94
}
95
`,
96
);
97
expect(result).toMatchInlineSnapshot(`
98
"class Foo {
99
method(a: number, b: number): number {
100
return a + b;
101
}
102
103
method2(a: number, b: number): number {
104
return a + b;
105
}
106
107
method3(a: number, b: number): number {
108
return a + b;
109
}
110
111
<NODE(method_definition)><IDENT>method4</IDENT>(a: number, b: number): number {
112
return a + b;
113
}</NODE(method_definition)>
114
}"
115
`);
116
});
117
118
test('does not capture private method', async () => {
119
const result = await run(
120
outdent`
121
class Foo {
122
private <<method>>(a: number, b: number): number {
123
return a + b;
124
}
125
}
126
`,
127
);
128
expect(result).toMatchInlineSnapshot(`"testable node NOT found"`);
129
});
130
131
test('static method', async () => {
132
const result = await run(
133
outdent`
134
class Foo {
135
static <<method>>(a: number, b: number): number {
136
return a + b;
137
}
138
}
139
`,
140
);
141
expect(result).toMatchInlineSnapshot(`
142
"class Foo {
143
<NODE(method_definition)>static <IDENT>method</IDENT>(a: number, b: number): number {
144
return a + b;
145
}</NODE(method_definition)>
146
}"
147
`);
148
});
149
150
test('private static method', async () => {
151
const result = await run(
152
outdent`
153
class Foo {
154
private static <<method>>(a: number, b: number): number {
155
return a + b;
156
}
157
}
158
`,
159
);
160
expect(result).toMatchInlineSnapshot(`"testable node NOT found"`);
161
});
162
163
164
test('public static method', async () => {
165
const result = await run(
166
outdent`
167
class Foo {
168
public static <<method>>(a: number, b: number): number {
169
return a + b;
170
}
171
}
172
`,
173
);
174
expect(result).toMatchInlineSnapshot(`
175
"class Foo {
176
<NODE(method_definition)>public static <IDENT>method</IDENT>(a: number, b: number): number {
177
return a + b;
178
}</NODE(method_definition)>
179
}"
180
`);
181
});
182
183
test('class declaration', async () => {
184
const result = await run(
185
outdent`
186
export class <<>>Foo {
187
method(a: number, b: number): number {
188
return a + b;
189
}
190
}
191
`,
192
);
193
expect(result).toMatchInlineSnapshot(`
194
"export <NODE(class_declaration)>class <IDENT>Foo</IDENT> {
195
method(a: number, b: number): number {
196
return a + b;
197
}
198
}</NODE(class_declaration)>"
199
`);
200
});
201
202
test('class declaration with prop and method', async () => {
203
const result = await run(
204
outdent`
205
export class <<>>Foo {
206
bar = 1;
207
208
method(a: number, b: number): number {
209
return a + b;
210
}
211
}
212
`,
213
);
214
expect(result).toMatchInlineSnapshot(`
215
"export <NODE(class_declaration)>class <IDENT>Foo</IDENT> {
216
bar = 1;
217
218
method(a: number, b: number): number {
219
return a + b;
220
}
221
}</NODE(class_declaration)>"
222
`);
223
});
224
225
test('class declaration with prop and static method', async () => {
226
const result = await run(
227
outdent`
228
export class Foo {
229
bar = 1;
230
231
static <<>>method(a: number, b: number): number {
232
return a + b;
233
}
234
}
235
`,
236
);
237
expect(result).toMatchInlineSnapshot(`
238
"export class Foo {
239
bar = 1;
240
241
<NODE(method_definition)>static <IDENT>method</IDENT>(a: number, b: number): number {
242
return a + b;
243
}</NODE(method_definition)>
244
}"
245
`);
246
});
247
});
248
249