Skip to content

Commit

Permalink
Fix rename refactor unit tests (#1955)
Browse files Browse the repository at this point in the history
Fixes #1953
  • Loading branch information
DonJayamanne authored Jun 14, 2018
1 parent f64f043 commit 3c882b5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions news/3 Code Health/1953.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix rename refactoring unit tests.
6 changes: 3 additions & 3 deletions src/client/common/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare interface String {
* By default lines are trimmed and empty lines are removed.
* @param {SplitLinesOptions=} splitOptions - Options used for splitting the string.
*/
splitLines(splitOptions?: { trim: boolean, removeEmptyEntries?: boolean }): string[];
splitLines(splitOptions?: { trim: boolean; removeEmptyEntries?: boolean }): string[];
/**
* Appropriately formats a string so it can be used as an argument for a command in a shell.
* E.g. if an argument contains a space, then it will be enclosed within double quotes.
Expand All @@ -33,10 +33,10 @@ declare interface String {
* By default lines are trimmed and empty lines are removed.
* @param {SplitLinesOptions=} splitOptions - Options used for splitting the string.
*/
String.prototype.splitLines = function (this: string, splitOptions: { trim: boolean, removeEmptyEntries: boolean } = { removeEmptyEntries: true, trim: true }): string[] {
String.prototype.splitLines = function (this: string, splitOptions: { trim: boolean; removeEmptyEntries: boolean } = { removeEmptyEntries: true, trim: true }): string[] {
let lines = this.split(/\r?\n/g);
if (splitOptions && splitOptions.trim) {
lines = lines.filter(line => line.trim());
lines = lines.map(line => line.trim());
}
if (splitOptions && splitOptions.removeEmptyEntries) {
lines = lines.filter(line => line.length > 0);
Expand Down
11 changes: 7 additions & 4 deletions src/test/refactor/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as path from 'path';
import * as typeMoq from 'typemoq';
import { Range, TextEditorCursorStyle, TextEditorLineNumbersStyle, TextEditorOptions, window, workspace } from 'vscode';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import '../../client/common/extensions';
import { BufferDecoder } from '../../client/common/process/decoder';
import { ProcessService } from '../../client/common/process/proc';
import { PythonExecutionFactory } from '../../client/common/process/pythonExecutionFactory';
Expand Down Expand Up @@ -47,26 +48,28 @@ suite('Refactor Rename', () => {

test('Rename function in source without a trailing empty line', async () => {
const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'without empty line.py');
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}`;
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}`
.splitLines({ removeEmptyEntries: false, trim: false });

const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object);
const textDocument = await workspace.openTextDocument(sourceFile);
await window.showTextDocument(textDocument);

const response = await proxy.rename<RenameResponse>(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options);
expect(response.results).to.be.lengthOf(1);
expect(response.results[0].diff).to.be.equal(expectedDiff);
expect(response.results[0].diff.splitLines({ removeEmptyEntries: false, trim: false })).to.be.deep.equal(expectedDiff);
});
test('Rename function in source with a trailing empty line', async () => {
const sourceFile = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'refactoring', 'source folder', 'with empty line.py');
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}`;
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}`
.splitLines({ removeEmptyEntries: false, trim: false });

const proxy = new RefactorProxy(EXTENSION_ROOT_DIR, pythonSettings.object, path.dirname(sourceFile), serviceContainer.object);
const textDocument = await workspace.openTextDocument(sourceFile);
await window.showTextDocument(textDocument);

const response = await proxy.rename<RenameResponse>(textDocument, 'three', sourceFile, new Range(7, 20, 7, 23), options);
expect(response.results).to.be.lengthOf(1);
expect(response.results[0].diff).to.be.equal(expectedDiff);
expect(response.results[0].diff.splitLines({ removeEmptyEntries: false, trim: false })).to.be.deep.equal(expectedDiff);
});
});

0 comments on commit 3c882b5

Please sign in to comment.