Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a79f86

Browse files
committed
unit testing of formatters
1 parent 10096ec commit 0a79f86

15 files changed

+614
-0
lines changed

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
],
3737
"sourceMaps": true,
3838
"outDir": "${workspaceRoot}/out/client"
39+
},
40+
{
41+
"name": "Launch Tests",
42+
"type": "extensionHost",
43+
"request": "launch",
44+
"runtimeExecutable": "${execPath}",
45+
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
46+
"stopOnEntry": false,
47+
"sourceMaps": true,
48+
"outDir": "${workspaceRoot}/out/test",
49+
"preLaunchTask": "npm"
3950
}
4051
]
4152
}

src/test/extension.common.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
6+
// The module 'assert' provides assertion methods from node
7+
import * as assert from "assert";
8+
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 {sendCommand} from "../client/common/childProc";
13+
14+
// Defines a Mocha test suite to group tests of similar kind together
15+
suite("ChildProc", () => {
16+
17+
test("Standard Response", () => {
18+
sendCommand("python -c \"print(1)\"", __dirname, false).then(data => {
19+
assert.ok(data === "1\n");
20+
});
21+
});
22+
test("Error Response", () => {
23+
sendCommand("python -c \"print1234(1)\"", __dirname, false).then(data => {
24+
assert.ok(false);
25+
}).catch(() => {
26+
assert.ok(true);
27+
});
28+
});
29+
});

src/test/extension.format.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
6+
// The module 'assert' provides assertion methods from node
7+
import * as assert from "assert";
8+
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+
18+
let pythonSettings = settings.PythonSettings.getInstance();
19+
let ch = vscode.window.createOutputChannel("Tests");
20+
let pythoFilesPath = path.join(__dirname, "..", "..", "src", "test", "pythonFiles", "formatting");
21+
22+
function closeActiveEditor() {
23+
if (vscode.window.activeTextEditor) {
24+
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
25+
}
26+
}
27+
suite("Formatting", () => {
28+
test("AutoPep8", done => {
29+
let fileToFormat = path.join(pythoFilesPath, "beforeAutoPep8.py");
30+
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
31+
vscode.window.showTextDocument(textDocument);
32+
let formatter = new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath);
33+
return formatter.formatDocument(textDocument, null, null).then(edits => {
34+
let activeEditor = vscode.window.activeTextEditor;
35+
activeEditor.edit(editBuilder => {
36+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
37+
}).then(edited => {
38+
let formattedFile = path.join(pythoFilesPath, "afterAutoPep8.py");
39+
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
40+
if (error) {
41+
return assert.fail(error, "", "Failed to read formatted file");
42+
}
43+
assert.equal(activeEditor.document.getText(), data, "Formatted text is not the same");
44+
});
45+
});
46+
}, error => {
47+
assert.fail(error, "", "Error in Formatting, " + error);
48+
});
49+
}, error => {
50+
assert.fail(error, "", "Error in Opening Document, " + error);
51+
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
52+
});
53+
54+
test("Yapf", done => {
55+
let fileToFormat = path.join(pythoFilesPath, "beforeYapf.py");
56+
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
57+
vscode.window.showTextDocument(textDocument);
58+
let formatter = new YapfFormatter(ch, pythonSettings, pythoFilesPath);
59+
return formatter.formatDocument(textDocument, null, null).then(edits => {
60+
let activeEditor = vscode.window.activeTextEditor;
61+
activeEditor.edit(editBuilder => {
62+
edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
63+
}).then(edited => {
64+
let formattedFile = path.join(pythoFilesPath, "afterYapf.py");
65+
let formattedContents = fs.readFile(formattedFile, "utf-8", (error, data) => {
66+
if (error) {
67+
return assert.fail(error, "", "Failed to read formatted file");
68+
}
69+
assert.equal(activeEditor.document.getText(), data, "Formatted text is not the same");
70+
});
71+
});
72+
}, error => {
73+
assert.fail(error, "", "Error in Formatting, " + error);
74+
});
75+
}, error => {
76+
assert.fail(error, "", "Error in Opening Document, " + error);
77+
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
78+
});
79+
80+
test("Yapf autoformat on save", done => {
81+
let formattedFile = path.join(pythoFilesPath, "afterYapfFormatOnSave.py");
82+
let fileToFormat = path.join(pythoFilesPath, "beforeYapfFormatOnSave.py");
83+
let fileToCopyFrom = path.join(pythoFilesPath, "beforeYapfFormatOnSaveOriginal.py");
84+
let formattedFileContents = fs.readFileSync(formattedFile, "utf-8");
85+
if (fs.existsSync(fileToFormat)) { fs.unlinkSync(fileToFormat); }
86+
fs.copySync(fileToCopyFrom, fileToFormat);
87+
const FORMAT_ON_SAVE = pythonSettings.formatting.formatOnSave;
88+
pythonSettings.formatting.formatOnSave = true;
89+
pythonSettings.formatting.provider = "yapf";
90+
91+
vscode.workspace.openTextDocument(fileToFormat).then(textDocument => {
92+
return vscode.window.showTextDocument(textDocument).then(textEditor => {
93+
return textEditor.edit(editBuilder => {
94+
editBuilder.insert(new vscode.Position(0, 0), "#");
95+
}).then(edited => {
96+
return textDocument.save().then(saved => {
97+
return new Promise<any>((resolve, reject) => {
98+
setTimeout(() => {
99+
assert.equal(textDocument.getText(), formattedFileContents, "Formatted contents are not the same");
100+
resolve();
101+
}, 1000);
102+
});
103+
}, error => {
104+
assert.fail(error, "", "Error in Saving Document, " + error);
105+
});
106+
}, error => {
107+
assert.fail(error, "", "Error in Editing Document, " + error);
108+
});
109+
}, error => {
110+
assert.fail(error, "", "Error in Showing Document, " + error);
111+
});
112+
}, error => {
113+
assert.fail(error, "", "Error in Opening Document, " + error);
114+
}).then(() => done(), done).then(() => closeActiveEditor(), closeActiveEditor);
115+
});
116+
});

src/test/extension.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
6+
// The module 'assert' provides assertion methods from node
7+
import * as assert from "assert";
8+
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 * as myExtension from "../client/extension";
13+
14+
// Defines a Mocha test suite to group tests of similar kind together
15+
suite("Blah blah", () => {
16+
17+
// Defines a Mocha unit test
18+
test("Something Formatting 1", () => {
19+
assert.equal(1, [1, 2, 3].indexOf(5));
20+
assert.equal(-1, [1, 2, 3].indexOf(0));
21+
});
22+
23+
// Defines a Mocha unit test
24+
test("Something Formatting 2", () => {
25+
assert.equal(1, [1, 2, 3].indexOf(5));
26+
assert.equal(-1, [1, 2, 3].indexOf(0));
27+
});
28+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import math
2+
import sys
3+
4+
5+
def example1():
6+
# This is a long comment. This should be wrapped to fit within 72
7+
# characters.
8+
some_tuple = (1, 2, 3, 'a')
9+
some_variable = {'long': 'Long code lines should be wrapped within 79 characters.',
10+
'other': [math.pi, 100, 200, 300, 9876543210, 'This is a long string that goes on'],
11+
'more': {'inner': 'This whole logical line should be wrapped.', some_tuple: [1,
12+
20, 300, 40000, 500000000, 60000000000000000]}}
13+
return (some_tuple, some_variable)
14+
15+
16+
def example2(): return {'has_key() is deprecated': True}.has_key(
17+
{'f': 2}.has_key(''))
18+
19+
20+
class Example3(object):
21+
22+
def __init__(self, bar):
23+
# Comments should have a space after the hash.
24+
if bar:
25+
bar += 1
26+
bar = bar * bar
27+
return bar
28+
else:
29+
some_string = """
30+
Indentation in multiline strings should not be touched.
31+
Only actual code should be reindented.
32+
"""
33+
return (sys.path, some_string)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import math, sys
2+
3+
4+
def example1():
5+
####This is a long comment. This should be wrapped to fit within 72 characters.
6+
some_tuple = (1, 2, 3, 'a')
7+
some_variable = {'long':
8+
'Long code lines should be wrapped within 79 characters.',
9+
'other': [math.pi, 100, 200, 300, 9876543210,
10+
'This is a long string that goes on'],
11+
'more': {'inner':
12+
'This whole logical line should be wrapped.',
13+
some_tuple: [1, 20, 300, 40000, 500000000,
14+
60000000000000000]}}
15+
return (some_tuple, some_variable)
16+
17+
18+
def example2():
19+
return {'has_key() is deprecated': True}.has_key({'f': 2}.has_key(''))
20+
21+
22+
class Example3(object):
23+
def __init__(self, bar):
24+
#Comments should have a space after the hash.
25+
if bar:
26+
bar += 1
27+
bar = bar * bar
28+
return bar
29+
else:
30+
some_string = """
31+
Indentation in multiline strings should not be touched.
32+
Only actual code should be reindented.
33+
"""
34+
35+
return (sys.path, some_string)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
import math, sys
3+
4+
5+
def example1():
6+
####This is a long comment. This should be wrapped to fit within 72 characters.
7+
some_tuple = (1, 2, 3, 'a')
8+
some_variable = {'long':
9+
'Long code lines should be wrapped within 79 characters.',
10+
'other': [math.pi, 100, 200, 300, 9876543210,
11+
'This is a long string that goes on'],
12+
'more': {'inner':
13+
'This whole logical line should be wrapped.',
14+
some_tuple: [1, 20, 300, 40000, 500000000,
15+
60000000000000000]}}
16+
return (some_tuple, some_variable)
17+
18+
19+
def example2():
20+
return {'has_key() is deprecated': True}.has_key({'f': 2}.has_key(''))
21+
22+
23+
class Example3(object):
24+
def __init__(self, bar):
25+
#Comments should have a space after the hash.
26+
if bar:
27+
bar += 1
28+
bar = bar * bar
29+
return bar
30+
else:
31+
some_string = """
32+
Indentation in multiline strings should not be touched.
33+
Only actual code should be reindented.
34+
"""
35+
36+
return (sys.path, some_string)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import math, sys;
2+
3+
def example1():
4+
####This is a long comment. This should be wrapped to fit within 72 characters.
5+
some_tuple=( 1,2, 3,'a' );
6+
some_variable={'long':'Long code lines should be wrapped within 79 characters.',
7+
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
8+
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
9+
20,300,40000,500000000,60000000000000000]}}
10+
return (some_tuple, some_variable)
11+
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
12+
class Example3( object ):
13+
def __init__ ( self, bar ):
14+
#Comments should have a space after the hash.
15+
if bar : bar+=1; bar=bar* bar ; return bar
16+
else:
17+
some_string = """
18+
Indentation in multiline strings should not be touched.
19+
Only actual code should be reindented.
20+
"""
21+
return (sys.path, some_string)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import math, sys;
2+
3+
def example1():
4+
####This is a long comment. This should be wrapped to fit within 72 characters.
5+
some_tuple=( 1,2, 3,'a' );
6+
some_variable={'long':'Long code lines should be wrapped within 79 characters.',
7+
'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
8+
'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
9+
20,300,40000,500000000,60000000000000000]}}
10+
return (some_tuple, some_variable)
11+
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
12+
class Example3( object ):
13+
def __init__ ( self, bar ):
14+
#Comments should have a space after the hash.
15+
if bar : bar+=1; bar=bar* bar ; return bar
16+
else:
17+
some_string = """
18+
Indentation in multiline strings should not be touched.
19+
Only actual code should be reindented.
20+
"""
21+
return (sys.path, some_string)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
import math, sys
3+
4+
5+
def example1():
6+
####This is a long comment. This should be wrapped to fit within 72 characters.
7+
some_tuple = (1, 2, 3, 'a')
8+
some_variable = {'long':
9+
'Long code lines should be wrapped within 79 characters.',
10+
'other': [math.pi, 100, 200, 300, 9876543210,
11+
'This is a long string that goes on'],
12+
'more': {'inner':
13+
'This whole logical line should be wrapped.',
14+
some_tuple: [1, 20, 300, 40000, 500000000,
15+
60000000000000000]}}
16+
return (some_tuple, some_variable)
17+
18+
19+
def example2():
20+
return {'has_key() is deprecated': True}.has_key({'f': 2}.has_key(''))
21+
22+
23+
class Example3(object):
24+
def __init__(self, bar):
25+
#Comments should have a space after the hash.
26+
if bar:
27+
bar += 1
28+
bar = bar * bar
29+
return bar
30+
else:
31+
some_string = """
32+
Indentation in multiline strings should not be touched.
33+
Only actual code should be reindented.
34+
"""
35+
36+
return (sys.path, some_string)

0 commit comments

Comments
 (0)