Path: blob/main/extensions/copilot/src/extension/conversation/vscode-node/test/interactiveEditorSessionProvider.test.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { Diagnostic, Range, TextDocument, workspace } from 'vscode';7import { rangeSpanningDiagnostics } from '../../../../platform/languages/common/languageDiagnosticsService';8import { IParserService } from '../../../../platform/parser/node/parserService';9import { ITestingServicesAccessor } from '../../../../platform/test/node/services';10import { Position } from '../../../../vscodeTypes';11import { findFixRangeOfInterest } from '../../../context/node/resolvers/fixSelection';12import { createExtensionTestingServices } from '../../../test/vscode-node/services';1314suite('findFixRangeOfInterest', function () {15let accessor: ITestingServicesAccessor;16let typeScriptDoc: TextDocument;17let javaDoc: TextDocument;18let pythonDoc: TextDocument;19let cppDoc: TextDocument;2021suiteSetup(async function () {22accessor = createExtensionTestingServices().createTestingAccessor();23await setupTypeScriptDoc();24await setupPythonDoc();25await setupJavaDoc();26await setupCppDoc();27});2829async function setupTypeScriptDoc() {30const typeScriptContent = [31/* 0 */ 'class Person {',32/* 1 */ ' readonly myName = "Paul";',33/* 2 */ ' readonly myFriendName = "Marc";',34/* 3 */ ' readonly threshold = 3;',35/* 4 */ '',36/* 5 */ ' sayMyName() {',37/* 6 */ ' return this.myName;',38/* 7 */ ' }',39/* 8 */ ' sayHi() {',40/* 9 */ ' return "hi";',41/* 10 */ ' }',42/* 11 */ ' speak() {',43/* 12 */ ' const salute = () => {',44/* 13 */ ' for (let i = 0; i < 5; i++) {',45/* 14 */ ' if (i > this.threshold) {',46/* 15 */ '',47/* 16 */ ' const x = 3;',48/* 17 */ ' const x = 10;',49/* 18 */ ' |r|eturn this.sayHi() + this.sayMyName();',50/* 19 */ '',51/* 20 */ ' }',52/* 21 */ ' }',53/* 22 */ ' };',54/* 23 */ ' salute();',55/* 24 */ ' }',56/* 25 */ '}',57].join('\n');5859typeScriptDoc = await workspace.openTextDocument({60language: 'typescript',61content: typeScriptContent,62});63}6465test('typescript - max number of lines 4', async function () {66await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 4, [16, 18]);67});6869test('typescript - max number of lines 10', async function () {70await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 10, [13, 21]);71});7273test('typescript - max number of lines 15', async function () {74await assertfindFixRangeOfInterestAsync(accessor, typeScriptDoc, 15, [11, 24]);75});7677async function setupPythonDoc() {78const pythonContent = [79/* 0 */ 'participant = ["Monalisa", "Akbar Hossain",',80/* 1 */ ' "Jakir Hasan", "Zahadur Rahman", "Zenifer Lopez"]',81/* 2 */ '',82/* 3 */ 'def selected_person(part):',83/* 4 */ ' selected = ["Akbar Hossain", "Zillur Rahman", "Monalisa"]',84/* 5 */ ' if (part in selected):',85/* 6 */ ' |r|eturn True',86/* 7 */ ' return False',87/* 8 */ '',88/* 9 */ 'selectedList = filter(selected_person, participant)',89/* 10 */ '',90/* 11 */ 'print("The selected candidates are:")',91/* 12 */ 'for candidate in selectedList:',92/* 13 */ ' print(candidate)',93].join('\n');9495pythonDoc = await workspace.openTextDocument({96language: 'python',97content: pythonContent,98});99}100101test('python - max number of lines 5', async function () {102await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 5, [3, 7]);103});104105test('python - max number of lines 11', async function () {106await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 11, [0, 9]);107});108109test('python - max number of lines 16', async function () {110await assertfindFixRangeOfInterestAsync(accessor, pythonDoc, 16, [0, 13]);111});112113async function setupJavaDoc() {114const javaContent = [115/* 0 */ 'public class Main {',116/* 1 */ ' public static void main(String[] args) {',117/* 2 */ ' final int myNum = 15;',118/* 3 */ ' for (int i = 0; i <= 10; i = i + 2) {',119/* 4 */ ' |S|ystem.out.println(i);',120/* 5 */ ' }',121/* 6 */ ' myNum = 20; // will generate an error',122/* 7 */ ' System.out.println(myNum);',123/* 8 */ ' }',124/* 9 */ '}',125].join('\n');126127javaDoc = await workspace.openTextDocument({128language: 'java',129content: javaContent,130});131}132133test('java - max number of lines 3', async function () {134await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 3, [3, 5]);135});136137test('java - max number of lines 5', async function () {138await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 5, [2, 6]);139});140141test('java - max number of lines 9', async function () {142await assertfindFixRangeOfInterestAsync(accessor, javaDoc, 9, [1, 8]);143});144145async function setupCppDoc() {146const cppContent = [147/* 0 */ 'bool isArmstrong(int x)',148/* 1 */ '{',149/* 2 */ ' |/|/ Calling order function',150/* 3 */ ' int n = order(x);',151/* 4 */ ' int temp = x, sum = 0;',152/* 5 */ '',153/* 6 */ ' while (temp) {',154/* 7 */ ' int r = temp % 10;',155/* 8 */ ' sum += power(r, n);',156/* 9 */ ' temp = temp / 10;',157/* 10 */ ' }',158/* 11 */ '',159/* 12 */ ' // If satisfies Armstrong',160/* 13 */ ' // condition',161/* 14 */ ' return (sum == x);',162/* 15 */ '}',163].join('\n');164165cppDoc = await workspace.openTextDocument({166language: 'cpp',167content: cppContent,168});169}170171test('cpp - max length 4', async function () {172await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 4, [2, 4]);173});174175test('cpp - max length 11', async function () {176await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 11, [2, 10]);177});178179test('cpp - max length 12', async function () {180await assertfindFixRangeOfInterestAsync(accessor, cppDoc, 12, [2, 10]);181});182});183184async function assertfindFixRangeOfInterestAsync(accessor: ITestingServicesAccessor, _document: TextDocument, maximumNumberOfLines: number, expectedLineRange: [number, number]) {185let startPosition: Position | undefined;186let endPosition: Position | undefined;187const documentLineCount = _document.lineCount;188for (let index = 0; index < documentLineCount; index++) {189const line = _document.lineAt(index);190if (!line.text.includes('|')) {191continue;192}193const firstPipeIndex = line.text.indexOf('|');194const position = new Position(index, firstPipeIndex);195if (!startPosition) {196startPosition = position;197const secondPipeIndex = line.text.indexOf('|', firstPipeIndex + 1) - 1;198endPosition = secondPipeIndex !== -1 ? new Position(index, secondPipeIndex) : undefined;199continue;200}201if (!endPosition) {202endPosition = position;203continue;204}205throw new Error('More than two | in the document');206}207if (!startPosition || !endPosition) {208throw new Error('Less than two | in the document');209}210const document = await workspace.openTextDocument({211language: _document.languageId,212content: _document.getText().replace(/\|/g, ''),213});214const treeSitterAST = accessor.get(IParserService).getTreeSitterAST(document);215assert(treeSitterAST);216const diagnostics: Diagnostic[] = [new Diagnostic(new Range(startPosition, endPosition), 'placeholder')];217const diagnosticsRange = rangeSpanningDiagnostics(diagnostics);218const rangeOfInterest = await findFixRangeOfInterest(treeSitterAST, diagnosticsRange, maximumNumberOfLines);219assert.deepStrictEqual(rangeOfInterest!.start.line, expectedLineRange[0]);220assert.deepStrictEqual(rangeOfInterest!.end.line, expectedLineRange[1]);221}222223224