Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/conversation/vscode-node/test/interactiveEditorSessionProvider.test.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 assert from 'assert';
7
import { Diagnostic, Range, TextDocument, workspace } from 'vscode';
8
import { rangeSpanningDiagnostics } from '../../../../platform/languages/common/languageDiagnosticsService';
9
import { IParserService } from '../../../../platform/parser/node/parserService';
10
import { ITestingServicesAccessor } from '../../../../platform/test/node/services';
11
import { Position } from '../../../../vscodeTypes';
12
import { findFixRangeOfInterest } from '../../../context/node/resolvers/fixSelection';
13
import { createExtensionTestingServices } from '../../../test/vscode-node/services';
14
15
suite('findFixRangeOfInterest', function () {
16
let accessor: ITestingServicesAccessor;
17
let typeScriptDoc: TextDocument;
18
let javaDoc: TextDocument;
19
let pythonDoc: TextDocument;
20
let cppDoc: TextDocument;
21
22
suiteSetup(async function () {
23
accessor = createExtensionTestingServices().createTestingAccessor();
24
await setupTypeScriptDoc();
25
await setupPythonDoc();
26
await setupJavaDoc();
27
await setupCppDoc();
28
});
29
30
async function setupTypeScriptDoc() {
31
const typeScriptContent = [
32
/* 0 */ 'class Person {',
33
/* 1 */ ' readonly myName = "Paul";',
34
/* 2 */ ' readonly myFriendName = "Marc";',
35
/* 3 */ ' readonly threshold = 3;',
36
/* 4 */ '',
37
/* 5 */ ' sayMyName() {',
38
/* 6 */ ' return this.myName;',
39
/* 7 */ ' }',
40
/* 8 */ ' sayHi() {',
41
/* 9 */ ' return "hi";',
42
/* 10 */ ' }',
43
/* 11 */ ' speak() {',
44
/* 12 */ ' const salute = () => {',
45
/* 13 */ ' for (let i = 0; i < 5; i++) {',
46
/* 14 */ ' if (i > this.threshold) {',
47
/* 15 */ '',
48
/* 16 */ ' const x = 3;',
49
/* 17 */ ' const x = 10;',
50
/* 18 */ ' |r|eturn this.sayHi() + this.sayMyName();',
51
/* 19 */ '',
52
/* 20 */ ' }',
53
/* 21 */ ' }',
54
/* 22 */ ' };',
55
/* 23 */ ' salute();',
56
/* 24 */ ' }',
57
/* 25 */ '}',
58
].join('\n');
59
60
typeScriptDoc = await workspace.openTextDocument({
61
language: 'typescript',
62
content: typeScriptContent,
63
});
64
}
65
66
test('typescript - max number of lines 4', async function () {
67
await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 4, [16, 18]);
68
});
69
70
test('typescript - max number of lines 10', async function () {
71
await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 10, [13, 21]);
72
});
73
74
test('typescript - max number of lines 15', async function () {
75
await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 15, [11, 24]);
76
});
77
78
async function setupPythonDoc() {
79
const pythonContent = [
80
/* 0 */ 'participant = ["Monalisa", "Akbar Hossain",',
81
/* 1 */ ' "Jakir Hasan", "Zahadur Rahman", "Zenifer Lopez"]',
82
/* 2 */ '',
83
/* 3 */ 'def selected_person(part):',
84
/* 4 */ ' selected = ["Akbar Hossain", "Zillur Rahman", "Monalisa"]',
85
/* 5 */ ' if (part in selected):',
86
/* 6 */ ' |r|eturn True',
87
/* 7 */ ' return False',
88
/* 8 */ '',
89
/* 9 */ 'selectedList = filter(selected_person, participant)',
90
/* 10 */ '',
91
/* 11 */ 'print("The selected candidates are:")',
92
/* 12 */ 'for candidate in selectedList:',
93
/* 13 */ ' print(candidate)',
94
].join('\n');
95
96
pythonDoc = await workspace.openTextDocument({
97
language: 'python',
98
content: pythonContent,
99
});
100
}
101
102
test('python - max number of lines 5', async function () {
103
await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 5, [3, 7]);
104
});
105
106
test('python - max number of lines 11', async function () {
107
await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 11, [0, 9]);
108
});
109
110
test('python - max number of lines 16', async function () {
111
await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 16, [0, 13]);
112
});
113
114
async function setupJavaDoc() {
115
const javaContent = [
116
/* 0 */ 'public class Main {',
117
/* 1 */ ' public static void main(String[] args) {',
118
/* 2 */ ' final int myNum = 15;',
119
/* 3 */ ' for (int i = 0; i <= 10; i = i + 2) {',
120
/* 4 */ ' |S|ystem.out.println(i);',
121
/* 5 */ ' }',
122
/* 6 */ ' myNum = 20; // will generate an error',
123
/* 7 */ ' System.out.println(myNum);',
124
/* 8 */ ' }',
125
/* 9 */ '}',
126
].join('\n');
127
128
javaDoc = await workspace.openTextDocument({
129
language: 'java',
130
content: javaContent,
131
});
132
}
133
134
test('java - max number of lines 3', async function () {
135
await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 3, [3, 5]);
136
});
137
138
test('java - max number of lines 5', async function () {
139
await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 5, [2, 6]);
140
});
141
142
test('java - max number of lines 9', async function () {
143
await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 9, [1, 8]);
144
});
145
146
async function setupCppDoc() {
147
const cppContent = [
148
/* 0 */ 'bool isArmstrong(int x)',
149
/* 1 */ '{',
150
/* 2 */ ' |/|/ Calling order function',
151
/* 3 */ ' int n = order(x);',
152
/* 4 */ ' int temp = x, sum = 0;',
153
/* 5 */ '',
154
/* 6 */ ' while (temp) {',
155
/* 7 */ ' int r = temp % 10;',
156
/* 8 */ ' sum += power(r, n);',
157
/* 9 */ ' temp = temp / 10;',
158
/* 10 */ ' }',
159
/* 11 */ '',
160
/* 12 */ ' // If satisfies Armstrong',
161
/* 13 */ ' // condition',
162
/* 14 */ ' return (sum == x);',
163
/* 15 */ '}',
164
].join('\n');
165
166
cppDoc = await workspace.openTextDocument({
167
language: 'cpp',
168
content: cppContent,
169
});
170
}
171
172
test('cpp - max length 4', async function () {
173
await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 4, [2, 4]);
174
});
175
176
test('cpp - max length 11', async function () {
177
await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 11, [2, 10]);
178
});
179
180
test('cpp - max length 12', async function () {
181
await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 12, [2, 10]);
182
});
183
});
184
185
async function assertfindFixRangeOfInterestAsync(accessor: ITestingServicesAccessor, _document: TextDocument, maximumNumberOfLines: number, expectedLineRange: [number, number]) {
186
let startPosition: Position | undefined;
187
let endPosition: Position | undefined;
188
const documentLineCount = _document.lineCount;
189
for (let index = 0; index < documentLineCount; index++) {
190
const line = _document.lineAt(index);
191
if (!line.text.includes('|')) {
192
continue;
193
}
194
const firstPipeIndex = line.text.indexOf('|');
195
const position = new Position(index, firstPipeIndex);
196
if (!startPosition) {
197
startPosition = position;
198
const secondPipeIndex = line.text.indexOf('|', firstPipeIndex + 1) - 1;
199
endPosition = secondPipeIndex !== -1 ? new Position(index, secondPipeIndex) : undefined;
200
continue;
201
}
202
if (!endPosition) {
203
endPosition = position;
204
continue;
205
}
206
throw new Error('More than two | in the document');
207
}
208
if (!startPosition || !endPosition) {
209
throw new Error('Less than two | in the document');
210
}
211
const document = await workspace.openTextDocument({
212
language: _document.languageId,
213
content: _document.getText().replace(/\|/g, ''),
214
});
215
const treeSitterAST = accessor.get(IParserService).getTreeSitterAST(document);
216
assert(treeSitterAST);
217
const diagnostics: Diagnostic[] = [new Diagnostic(new Range(startPosition, endPosition), 'placeholder')];
218
const diagnosticsRange = rangeSpanningDiagnostics(diagnostics);
219
const rangeOfInterest = await findFixRangeOfInterest(treeSitterAST, diagnosticsRange, maximumNumberOfLines);
220
assert.deepStrictEqual(rangeOfInterest!.start.line, expectedLineRange[0]);
221
assert.deepStrictEqual(rangeOfInterest!.end.line, expectedLineRange[1]);
222
}
223
224