Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/renameSuggestions/test/node/renameSuggestionsProvider.spec.tsx
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 { expect, suite, test } from 'vitest';
8
import { RenameSuggestionsProvider } from '../../node/renameSuggestionsProvider';
9
10
suite('processReply', () => {
11
test('should handle JSON array of strings', () => {
12
const input = `["Hello", "world", "howAreYou?"]`;
13
const output = RenameSuggestionsProvider.parseResponse(input);
14
expect(output).toMatchInlineSnapshot(`
15
{
16
"redundantCharCount": 0,
17
"replyFormat": "jsonStringArray",
18
"symbolNames": [
19
"Hello",
20
"world",
21
"howAreYou?",
22
],
23
}
24
`);
25
});
26
27
test('should handle ordered list with .', () => {
28
const input = `
29
1. Hello
30
2. world
31
3. howAreYou?
32
`;
33
const output = RenameSuggestionsProvider.parseResponse(input);
34
expect(output).toMatchInlineSnapshot(`
35
{
36
"redundantCharCount": 21,
37
"replyFormat": "list",
38
"symbolNames": [
39
"Hello",
40
"world",
41
"howAreYou?",
42
],
43
}
44
`);
45
});
46
47
test('should handle ordered list with )', () => {
48
const input = `
49
1) Hello
50
2) world
51
3) howAreYou?
52
`;
53
const output = RenameSuggestionsProvider.parseResponse(input);
54
expect(output).toMatchInlineSnapshot(`
55
{
56
"redundantCharCount": 21,
57
"replyFormat": "list",
58
"symbolNames": [
59
"Hello",
60
"world",
61
"howAreYou?",
62
],
63
}
64
`);
65
});
66
67
test('should handle unordered list with *', () => {
68
const input = `
69
* Hello
70
* world
71
* howAreYou?
72
`;
73
const output = RenameSuggestionsProvider.parseResponse(input);
74
expect(output).toMatchInlineSnapshot(`
75
{
76
"redundantCharCount": 18,
77
"replyFormat": "list",
78
"symbolNames": [
79
"Hello",
80
"world",
81
"howAreYou?",
82
],
83
}
84
`);
85
});
86
87
test('should handle unordered list with -', () => {
88
const input = `
89
- Hello
90
- world
91
- howAreYou?
92
`;
93
const output = RenameSuggestionsProvider.parseResponse(input);
94
expect(output).toMatchInlineSnapshot(`
95
{
96
"redundantCharCount": 18,
97
"replyFormat": "list",
98
"symbolNames": [
99
"Hello",
100
"world",
101
"howAreYou?",
102
],
103
}
104
`);
105
});
106
107
test('should handle invalid input', () => {
108
const input = 'Hello world howAreYou?';
109
const output = RenameSuggestionsProvider.parseResponse(input);
110
expect(output).toMatchInlineSnapshot(`
111
{
112
"redundantCharCount": 22,
113
"replyFormat": "unknown",
114
"symbolNames": [],
115
}
116
`);
117
});
118
119
test('should handle empty input', () => {
120
const input = '';
121
const output = RenameSuggestionsProvider.parseResponse(input);
122
expect(output).toMatchInlineSnapshot(`
123
{
124
"redundantCharCount": 0,
125
"replyFormat": "unknown",
126
"symbolNames": [],
127
}
128
`);
129
});
130
131
test('should handle free response', () => {
132
const input = outdent`
133
\`\`\`
134
// FILEPATH: /Users/foo/bar.ts
135
136
private static _determinePrefix(name: string): string | undefined {
137
const prefix = name.match(/^([\\.\$\\_]+)/)?.[0];
138
return prefix;
139
}
140
\`\`\`
141
142
The purpose of the \`_determinePrefix\` function is to extract the prefix from a given name string. It uses a regular expression to match any characters at the beginning of the string that are either a dot (.), dollar sign ($), or underscore (_), and returns that matched prefix.
143
144
Here are some new names that reflect the purpose of the \`_determinePrefix\` function:
145
146
\`\`\`json
147
[
148
"_extractPrefix",
149
"_findPrefix",
150
"_getPrefix",
151
"_identifyPrefix",
152
"_parsePrefix"
153
]
154
\`\`\`
155
`;
156
const output = RenameSuggestionsProvider.parseResponse(input);
157
expect(output).toMatchInlineSnapshot(`
158
{
159
"redundantCharCount": 552,
160
"replyFormat": "multiJsonStringArray",
161
"symbolNames": [
162
"_extractPrefix",
163
"_findPrefix",
164
"_getPrefix",
165
"_identifyPrefix",
166
"_parsePrefix",
167
],
168
}
169
`);
170
});
171
172
// TODO@ulugbekna: handle this case
173
test('JSON object', () => {
174
const input = outdent`
175
{
176
"1": "fooBar",
177
"2": "bazQux",
178
"3": "steamBear",
179
}
180
`;
181
const output = RenameSuggestionsProvider.parseResponse(input);
182
expect(output).toMatchInlineSnapshot(`
183
{
184
"redundantCharCount": 54,
185
"replyFormat": "unknown",
186
"symbolNames": [],
187
}
188
`);
189
});
190
191
test('handle free form + ordered list', () => {
192
const input = outdent`
193
Based on the name and the role it plays in the code, \`Baz\` is likely a class responsible for initializing or setting up the baz service.
194
195
Here are a few alternative names that could reflect the purpose and functionality of \`Baz\`:
196
197
1. \`FooBar\`
198
2. \`FooBarBaz\`
199
3. \`BarBazBear\`
200
`;
201
const output = RenameSuggestionsProvider.parseResponse(input);
202
expect(output).toMatchInlineSnapshot(`
203
{
204
"redundantCharCount": 248,
205
"replyFormat": "list",
206
"symbolNames": [
207
"FooBar",
208
"FooBarBaz",
209
"BarBazBear",
210
],
211
}
212
`);
213
});
214
});
215
216
suite('preprocessSymbolNames', () => {
217
test('to camelCase', () => {
218
const result = RenameSuggestionsProvider.preprocessSymbolNames({
219
currentSymbolName: 'camelCase',
220
newSymbolNames: ['snake_case', 'kebab-case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],
221
languageId: 'javascript',
222
});
223
expect(result).toEqual(['snakeCase', 'kebabCase', 'pascalCase', 'upperSnakeCase', 'lowerKebabCase']);
224
});
225
226
test('to snake_case', () => {
227
const result = RenameSuggestionsProvider.preprocessSymbolNames({
228
currentSymbolName: 'snake_case',
229
newSymbolNames: ['camelCase', 'kebab-case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],
230
languageId: 'python',
231
});
232
expect(result).toEqual(['camel_case', 'kebab_case', 'pascal_case', 'upper_snake_case', 'lower_kebab_case']);
233
});
234
235
test('to PascalCase', () => {
236
const result = RenameSuggestionsProvider.preprocessSymbolNames({
237
currentSymbolName: 'PascalCase',
238
newSymbolNames: ['camelCase', 'kebab-case', 'snake_case', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],
239
languageId: 'javascript',
240
});
241
expect(result).toEqual(['CamelCase', 'KebabCase', 'SnakeCase', 'UpperSnakeCase', 'LowerKebabCase']);
242
});
243
244
test('to kebab-case', () => {
245
const result = RenameSuggestionsProvider.preprocessSymbolNames({
246
currentSymbolName: 'kebab-case',
247
newSymbolNames: ['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],
248
languageId: 'javascript',
249
});
250
expect(result).toEqual(['camel-case', 'snake-case', 'pascal-case', 'upper-snake-case', 'lower-kebab-case']);
251
});
252
253
test('as is', () => {
254
const result = RenameSuggestionsProvider.preprocessSymbolNames({
255
currentSymbolName: 'Unknown_Format',
256
newSymbolNames: ['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],
257
languageId: 'javascript',
258
});
259
expect(result).toEqual(['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case']);
260
});
261
});
262
263