Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rename refactor unit tests #1955

Merged
merged 4 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails in Windows tests, perhaps because EOLUsedByRope in Windows is the dreaded \r\n?

After that is fixed I say good!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just rope using different separators. Looks like its inconsistent. I assumed it was using \n throughout, guess I was wrong.

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);
});
});