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.js.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 - js', () => {
13
afterAll(() => _dispose());
14
15
function run(annotatedSrc: string) {
16
return srcWithAnnotatedTestableNode(
17
WASMLanguage.JavaScript,
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('does not capture private method', async () => {
76
const result = await run(
77
outdent`
78
class Foo {
79
private <<#method>>(a: number, b: number): number {
80
return a + b;
81
}
82
}
83
`,
84
);
85
expect(result).toMatchInlineSnapshot(`"testable node NOT found"`);
86
});
87
88
test('static method', async () => {
89
const result = await run(
90
outdent`
91
class Foo {
92
static <<method>>(a: number, b: number): number {
93
return a + b;
94
}
95
}
96
`,
97
);
98
expect(result).toMatchInlineSnapshot(`
99
"class Foo {
100
<NODE(method_definition)>static <IDENT>method</IDENT>(a: number, b: number): number {
101
return a + b;
102
}</NODE(method_definition)>
103
}"
104
`);
105
});
106
107
test('private static method', async () => {
108
const result = await run(
109
outdent`
110
class Foo {
111
static <<#method>>(a: number, b: number): number {
112
return a + b;
113
}
114
}
115
`,
116
);
117
expect(result).toMatchInlineSnapshot(`"testable node NOT found"`);
118
});
119
120
test('public static method', async () => {
121
const result = await run(
122
outdent`
123
class Foo {
124
public static <<method>>(a: number, b: number): number {
125
return a + b;
126
}
127
}
128
`,
129
);
130
expect(result).toMatchInlineSnapshot(`
131
"class Foo {
132
public <NODE(method_definition)>static <IDENT>method</IDENT>(a: number, b: number): number {
133
return a + b;
134
}</NODE(method_definition)>
135
}"
136
`);
137
});
138
});
139
140