Skip to content

Commit cfc67fc

Browse files
committed
fixed unit tests
1 parent 39d4708 commit cfc67fc

5 files changed

Lines changed: 861 additions & 829 deletions

File tree

src/test/extension.format.test.ts

Lines changed: 92 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,101 @@
11

2-
// // Note: This example test is leveraging the Mocha test framework.
3-
// // Please refer to their documentation on https://mochajs.org/ for help.
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
44

55

6-
// // The module 'assert' provides assertion methods from node
7-
// import * as assert from 'assert';
6+
// The module 'assert' provides assertion methods from node
7+
import * as assert from 'assert';
88

9-
// // You can import and use all API from the 'vscode' module
10-
// // as well as import your extension to test it
11-
// import * as vscode from 'vscode';
12-
// import {AutoPep8Formatter} from '../client/formatters/autoPep8Formatter';
13-
// import {YapfFormatter} from '../client/formatters/yapfFormatter';
14-
// import * as path from 'path';
15-
// import * as settings from '../client/common/configSettings';
16-
// import * as fs from 'fs-extra';
17-
// import {initialize} from './initialize';
18-
// import {execPythonFile} from '../client/common/utils';
9+
// You can import and use all API from the 'vscode' module
10+
// as well as import your extension to test it
11+
import * as vscode from 'vscode';
12+
import {AutoPep8Formatter} from '../client/formatters/autoPep8Formatter';
13+
import {YapfFormatter} from '../client/formatters/yapfFormatter';
14+
import * as path from 'path';
15+
import * as settings from '../client/common/configSettings';
16+
import * as fs from 'fs-extra';
17+
import {initialize} from './initialize';
18+
import {execPythonFile} from '../client/common/utils';
1919

20-
// let pythonSettings = settings.PythonSettings.getInstance();
21-
// let ch = vscode.window.createOutputChannel('Tests');
22-
// let pythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
23-
// const originalUnformattedFile = path.join(pythoFilesPath, 'fileToFormat.py');
20+
let pythonSettings = settings.PythonSettings.getInstance();
21+
let ch = vscode.window.createOutputChannel('Tests');
22+
let pythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
23+
const originalUnformattedFile = path.join(pythoFilesPath, 'fileToFormat.py');
2424

25-
// const autoPep8FileToFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'autoPep8FileToFormat.py');
26-
// const autoPep8FileToAutoFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'autoPep8FileToAutoFormat.py');
27-
// const yapfFileToFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'yapfFileToFormat.py');
28-
// const yapfFileToAutoFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'yapfFileToAutoFormat.py');
25+
const autoPep8FileToFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'autoPep8FileToFormat.py');
26+
const autoPep8FileToAutoFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'autoPep8FileToAutoFormat.py');
27+
const yapfFileToFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'yapfFileToFormat.py');
28+
const yapfFileToAutoFormat = path.join(__dirname, 'pythonFiles', 'formatting', 'yapfFileToAutoFormat.py');
2929

30-
// let formattedYapf = '';
31-
// let formattedAutoPep8 = '';
30+
let formattedYapf = '';
31+
let formattedAutoPep8 = '';
3232

33-
// suiteSetup(done => {
34-
// initialize().then(() => {
35-
// [autoPep8FileToFormat, autoPep8FileToAutoFormat, yapfFileToFormat, yapfFileToAutoFormat].forEach(file => {
36-
// if (fs.existsSync(file)) { fs.unlinkSync(file); }
37-
// fs.copySync(originalUnformattedFile, file);
38-
// });
33+
suiteSetup(done => {
34+
initialize().then(() => {
35+
[autoPep8FileToFormat, autoPep8FileToAutoFormat, yapfFileToFormat, yapfFileToAutoFormat].forEach(file => {
36+
if (fs.existsSync(file)) { fs.unlinkSync(file); }
37+
fs.copySync(originalUnformattedFile, file);
38+
});
3939

40-
// fs.ensureDirSync(path.dirname(autoPep8FileToFormat));
41-
// let yapf = execPythonFile('yapf', [originalUnformattedFile], pythoFilesPath, false);
42-
// let autoPep8 = execPythonFile('autopep8', [originalUnformattedFile], pythoFilesPath, false);
43-
// return Promise.all<string>([yapf, autoPep8]).then(formattedResults => {
44-
// formattedYapf = formattedResults[0];
45-
// formattedAutoPep8 = formattedResults[1];
46-
// }).then(() => {
47-
// done();
48-
// });
49-
// }, done);
50-
// });
40+
fs.ensureDirSync(path.dirname(autoPep8FileToFormat));
41+
let yapf = execPythonFile('yapf', [originalUnformattedFile], pythoFilesPath, false);
42+
let autoPep8 = execPythonFile('autopep8', [originalUnformattedFile], pythoFilesPath, false);
43+
return Promise.all<string>([yapf, autoPep8]).then(formattedResults => {
44+
formattedYapf = formattedResults[0];
45+
formattedAutoPep8 = formattedResults[1];
46+
}).then(() => {
47+
done();
48+
});
49+
}, done);
50+
});
5151

52-
// suiteTeardown(() => {
53-
// if (vscode.window.activeTextEditor) {
54-
// return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
55-
// }
56-
// });
52+
suiteTeardown(() => {
53+
if (vscode.window.activeTextEditor) {
54+
return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
55+
}
56+
});
5757

58-
// suite('Formatting', () => {
59-
// teardown(() => {
60-
// if (vscode.window.activeTextEditor) {
61-
// return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
62-
// }
63-
// });
64-
// function testFormatting(formatter: AutoPep8Formatter | YapfFormatter, formattedContents: string, fileToFormat: string): PromiseLike<void> {
65-
// let textEditor: vscode.TextEditor;
66-
// let textDocument: vscode.TextDocument;
67-
// return vscode.workspace.openTextDocument(fileToFormat).then(document => {
68-
// textDocument = document;
69-
// return vscode.window.showTextDocument(textDocument);
70-
// }).then(editor => {
71-
// textEditor = editor;
72-
// return formatter.formatDocument(textDocument, null, null);
73-
// }).then(edits => {
74-
// return textEditor.edit(editBuilder => {
75-
// edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
76-
// });
77-
// }).then(edited => {
78-
// assert.equal(textEditor.document.getText(), formattedContents, 'Formatted text is not the same');
79-
// });
80-
// }
81-
// test('AutoPep8', done => {
82-
// testFormatting(new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath), formattedAutoPep8, autoPep8FileToFormat).then(done, done);
83-
// });
58+
suite('Formatting', () => {
59+
teardown(() => {
60+
if (vscode.window.activeTextEditor) {
61+
return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
62+
}
63+
});
64+
function testFormatting(formatter: AutoPep8Formatter | YapfFormatter, formattedContents: string, fileToFormat: string): PromiseLike<void> {
65+
let textEditor: vscode.TextEditor;
66+
let textDocument: vscode.TextDocument;
67+
return vscode.workspace.openTextDocument(fileToFormat).then(document => {
68+
textDocument = document;
69+
return vscode.window.showTextDocument(textDocument);
70+
}).then(editor => {
71+
textEditor = editor;
72+
return formatter.formatDocument(textDocument, null, null);
73+
}).then(edits => {
74+
return textEditor.edit(editBuilder => {
75+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
76+
});
77+
}).then(edited => {
78+
assert.equal(textEditor.document.getText(), formattedContents, 'Formatted text is not the same');
79+
});
80+
}
81+
test('AutoPep8', done => {
82+
testFormatting(new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath), formattedAutoPep8, autoPep8FileToFormat).then(done, done);
83+
});
8484

85-
// test('Yapf', done => {
86-
// testFormatting(new YapfFormatter(ch, pythonSettings, pythoFilesPath), formattedYapf, yapfFileToFormat).then(done, done);
87-
// });
85+
test('Yapf', done => {
86+
testFormatting(new YapfFormatter(ch, pythonSettings, pythoFilesPath), formattedYapf, yapfFileToFormat).then(done, done);
87+
});
8888

89+
<<<<<<< ef3136a0e5a3831092a37477366f922f1aa4c93c
8990
<<<<<<< 6caa8b5a628a42bb147a96b8c158bf8029b0847a
9091
function testAutoFormatting(formatter: string, formattedContents: string, fileToFormat: string): PromiseLike<void> {
9192
let textDocument: vscode.TextDocument;
9293
vscode.workspace.getConfiguration('editor').update('formatOnSave', true);
94+
=======
95+
function testAutoFormatting(formatter: string, formattedContents: string, fileToFormat: string): PromiseLike<void> {
96+
let textDocument: vscode.TextDocument;
97+
pythonSettings.formatting.formatOnSave = true;
98+
>>>>>>> fixed unit tests
9399
pythonSettings.formatting.provider = formatter;
94100
return vscode.workspace.openTextDocument(fileToFormat).then(document => {
95101
textDocument = document;
@@ -104,7 +110,11 @@
104110
return new Promise<any>((resolve, reject) => {
105111
setTimeout(() => {
106112
resolve();
113+
<<<<<<< ef3136a0e5a3831092a37477366f922f1aa4c93c
107114
}, 4000);
115+
=======
116+
}, 2000);
117+
>>>>>>> fixed unit tests
108118
});
109119
}).then(() => {
110120
assert.equal(textDocument.getText(), formattedContents, 'Formatted contents are not the same');
@@ -113,6 +123,7 @@
113123
test('AutoPep8 autoformat on save', done => {
114124
testAutoFormatting('autopep8', '#\n' + formattedAutoPep8, autoPep8FileToAutoFormat).then(done, done);
115125
});
126+
<<<<<<< ef3136a0e5a3831092a37477366f922f1aa4c93c
116127
=======
117128
// function testAutoFormatting(formatter: string, formattedContents: string, fileToFormat: string): PromiseLike<void> {
118129
// let textDocument: vscode.TextDocument;
@@ -141,8 +152,10 @@
141152
// testAutoFormatting('autopep8', '#\n' + formattedAutoPep8, autoPep8FileToAutoFormat).then(done, done);
142153
// });
143154
>>>>>>> unittest changes
155+
=======
156+
>>>>>>> fixed unit tests
144157

145-
// test('Yapf autoformat on save', done => {
146-
// testAutoFormatting('yapf', '#\n' + formattedYapf, yapfFileToAutoFormat).then(done, done);
147-
// });
148-
// });
158+
test('Yapf autoformat on save', done => {
159+
testAutoFormatting('yapf', '#\n' + formattedYapf, yapfFileToAutoFormat).then(done, done);
160+
});
161+
});

0 commit comments

Comments
 (0)