|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { expect } from 'chai'; |
| 7 | +import { EOL } from 'os'; |
| 8 | +import * as path from 'path'; |
| 9 | +import * as typeMoq from 'typemoq'; |
| 10 | +import { Range, TextEditorCursorStyle, TextEditorLineNumbersStyle, TextEditorOptions, window, workspace } from 'vscode'; |
| 11 | +import { EXTENSION_ROOT_DIR } from '../../client/common/constants'; |
| 12 | +import { BufferDecoder } from '../../client/common/process/decoder'; |
| 13 | +import { ProcessService } from '../../client/common/process/proc'; |
| 14 | +import { PythonExecutionFactory } from '../../client/common/process/pythonExecutionFactory'; |
| 15 | +import { IProcessServiceFactory, IPythonExecutionFactory } from '../../client/common/process/types'; |
| 16 | +import { IConfigurationService, IPythonSettings } from '../../client/common/types'; |
| 17 | +import { IServiceContainer } from '../../client/ioc/types'; |
| 18 | +import { RefactorProxy } from '../../client/refactor/proxy'; |
| 19 | +import { PYTHON_PATH } from '../common'; |
| 20 | +import { closeActiveWindows, initialize, initializeTest } from './../initialize'; |
| 21 | + |
| 22 | +type RenameResponse = { |
| 23 | + results: [{ diff: string }]; |
| 24 | +}; |
| 25 | + |
| 26 | +suite('Refactor Rename', () => { |
| 27 | + const options: TextEditorOptions = { cursorStyle: TextEditorCursorStyle.Line, insertSpaces: true, lineNumbers: TextEditorLineNumbersStyle.Off, tabSize: 4 }; |
| 28 | + let pythonSettings: typeMoq.IMock<IPythonSettings>; |
| 29 | + let serviceContainer: typeMoq.IMock<IServiceContainer>; |
| 30 | + suiteSetup(initialize); |
| 31 | + setup(async () => { |
| 32 | + pythonSettings = typeMoq.Mock.ofType<IPythonSettings>(); |
| 33 | + pythonSettings.setup(p => p.pythonPath).returns(() => PYTHON_PATH); |
| 34 | + const configService = typeMoq.Mock.ofType<IConfigurationService>(); |
| 35 | + configService.setup(c => c.getSettings(typeMoq.It.isAny())).returns(() => pythonSettings.object); |
| 36 | + const processServiceFactory = typeMoq.Mock.ofType<IProcessServiceFactory>(); |
| 37 | + processServiceFactory.setup(p => p.create(typeMoq.It.isAny())).returns(() => Promise.resolve(new ProcessService(new BufferDecoder()))); |
| 38 | + |
| 39 | + serviceContainer = typeMoq.Mock.ofType<IServiceContainer>(); |
| 40 | + serviceContainer.setup(s => s.get(typeMoq.It.isValue(IConfigurationService), typeMoq.It.isAny())).returns(() => configService.object); |
| 41 | + serviceContainer.setup(s => s.get(typeMoq.It.isValue(IProcessServiceFactory), typeMoq.It.isAny())).returns(() => processServiceFactory.object); |
| 42 | + serviceContainer.setup(s => s.get(typeMoq.It.isValue(IPythonExecutionFactory), typeMoq.It.isAny())).returns(() => new PythonExecutionFactory(serviceContainer.object)); |
| 43 | + await initializeTest(); |
| 44 | + }); |
| 45 | + teardown(closeActiveWindows); |
| 46 | + suiteTeardown(closeActiveWindows); |
| 47 | + |
| 48 | + test('Rename function in source without a trailing empty line', async () => { |
| 49 | + const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'without empty line.py'); |
| 50 | + const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}`; |
| 51 | + |
| 52 | + const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object); |
| 53 | + const textDocument = await workspace.openTextDocument(sourceFile); |
| 54 | + await window.showTextDocument(textDocument); |
| 55 | + |
| 56 | + const response = await proxy.rename<RenameResponse>(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options); |
| 57 | + expect(response.results).to.be.lengthOf(1); |
| 58 | + expect(response.results[0].diff).to.be.equal(expectedDiff); |
| 59 | + }); |
| 60 | + test('Rename function in source with a trailing empty line', async () => { |
| 61 | + const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'with empty line.py'); |
| 62 | + const expectedDiff = `--- a/${path.basename(sourceFile)}${EOL}+++ b/${path.basename(sourceFile)}${EOL}@@ -1,8 +1,8 @@${EOL} import os${EOL} ${EOL}-def one():${EOL}+def three():${EOL} return True${EOL} ${EOL} def two():${EOL}- if one():${EOL}- print(\"A\" + one())${EOL}+ if three():${EOL}+ print(\"A\" + three())${EOL}`; |
| 63 | + |
| 64 | + const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object); |
| 65 | + const textDocument = await workspace.openTextDocument(sourceFile); |
| 66 | + await window.showTextDocument(textDocument); |
| 67 | + |
| 68 | + const response = await proxy.rename<RenameResponse>(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options); |
| 69 | + expect(response.results).to.be.lengthOf(1); |
| 70 | + expect(response.results[0].diff).to.be.equal(expectedDiff); |
| 71 | + }); |
| 72 | +}); |
0 commit comments