Skip to content

Commit 5d8f81a

Browse files
committed
more tests for #432
1 parent e31c639 commit 5d8f81a

4 files changed

Lines changed: 87 additions & 3 deletions

File tree

src/test/extension.onTypeFormat.test.ts

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
// Place this right on top
7-
import { initialize, IS_TRAVIS, PYTHON_PATH, closeActiveWindows } from './initialize';
7+
import { initialize, PYTHON_PATH, closeActiveWindows } from './initialize';
88
// The module 'assert' provides assertion methods from node
99
import * as assert from 'assert';
1010

@@ -17,7 +17,7 @@ import * as fs from 'fs-extra';
1717
import { BlockFormatProviders } from '../client/typeFormatters/blockFormatProvider';
1818
let pythonSettings = settings.PythonSettings.getInstance();
1919
let srcPythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'typeFormatFiles');
20-
let outPythoFilesPath = path.join(__dirname, '..', 'pythonFiles', 'typeFormatFiles');
20+
let outPythoFilesPath = path.join(__dirname, 'pythonFiles', 'typeFormatFiles');
2121

2222
const tryBlock2OutFilePath = path.join(outPythoFilesPath, 'tryBlocks2.py');
2323
const tryBlock4OutFilePath = path.join(outPythoFilesPath, 'tryBlocks4.py');
@@ -27,6 +27,10 @@ const elseBlock2OutFilePath = path.join(outPythoFilesPath, 'elseBlocks2.py');
2727
const elseBlock4OutFilePath = path.join(outPythoFilesPath, 'elseBlocks4.py');
2828
const elseBlockTabOutFilePath = path.join(outPythoFilesPath, 'elseBlocksTab.py');
2929

30+
const elseBlockFirstLine2OutFilePath = path.join(outPythoFilesPath, 'elseBlocksFirstLine2.py');
31+
const elseBlockFirstLine4OutFilePath = path.join(outPythoFilesPath, 'elseBlocksFirstLine4.py');
32+
const elseBlockFirstLineTabOutFilePath = path.join(outPythoFilesPath, 'elseBlocksFirstLineTab.py');
33+
3034
const provider = new BlockFormatProviders();
3135

3236
function testFormatting(fileToFormat: string, position: vscode.Position, expectedEdits: vscode.TextEdit[], formatOptions: vscode.FormattingOptions): PromiseLike<void> {
@@ -50,6 +54,75 @@ function testFormatting(fileToFormat: string, position: vscode.Position, expecte
5054
});
5155
}
5256

57+
58+
suite('Else block with if in first line of file', () => {
59+
suiteSetup(done => {
60+
initialize().then(() => {
61+
pythonSettings.pythonPath = PYTHON_PATH;
62+
fs.ensureDirSync(path.dirname(outPythoFilesPath));
63+
64+
['elseBlocksFirstLine2.py', 'elseBlocksFirstLine4.py', 'elseBlocksFirstLineTab.py'].forEach(file => {
65+
const targetFile = path.join(outPythoFilesPath, file);
66+
if (fs.existsSync(targetFile)) { fs.unlinkSync(targetFile); }
67+
fs.copySync(path.join(srcPythoFilesPath, file), targetFile);
68+
});
69+
}).then(done).catch(done);
70+
});
71+
suiteTeardown(done => {
72+
closeActiveWindows().then(done, done);
73+
});
74+
teardown(done => {
75+
closeActiveWindows().then(done, done);
76+
});
77+
78+
interface TestCase {
79+
title: string;
80+
line: number;
81+
column: number;
82+
expectedEdits: vscode.TextEdit[];
83+
formatOptions: vscode.FormattingOptions;
84+
filePath: string;
85+
}
86+
const TAB = ' ';
87+
const testCases: TestCase[] = [
88+
{
89+
title: 'else block with 2 spaces',
90+
line: 3, column: 7,
91+
expectedEdits: [
92+
vscode.TextEdit.delete(new vscode.Range(3, 0, 3, 2))
93+
],
94+
formatOptions: { insertSpaces: true, tabSize: 2 },
95+
filePath: elseBlockFirstLine2OutFilePath
96+
},
97+
{
98+
title: 'else block with 4 spaces',
99+
line: 3, column: 9,
100+
expectedEdits: [
101+
vscode.TextEdit.delete(new vscode.Range(3, 0, 3, 4))
102+
],
103+
formatOptions: { insertSpaces: true, tabSize: 4 },
104+
filePath: elseBlockFirstLine4OutFilePath
105+
},
106+
{
107+
title: 'else block with Tab',
108+
line: 3, column: 6,
109+
expectedEdits: [
110+
vscode.TextEdit.delete(new vscode.Range(3, 0, 3, 1)),
111+
vscode.TextEdit.insert(new vscode.Position(3, 0), '')
112+
],
113+
formatOptions: { insertSpaces: false, tabSize: 4 },
114+
filePath: elseBlockFirstLineTabOutFilePath
115+
}
116+
];
117+
118+
testCases.forEach((testCase, index) => {
119+
test(`${index + 1}. ${testCase.title}`, done => {
120+
const pos = new vscode.Position(testCase.line, testCase.column);
121+
testFormatting(testCase.filePath, pos, testCase.expectedEdits, testCase.formatOptions).then(done, done);
122+
});
123+
});
124+
});
125+
53126
suite('Try blocks with indentation of 2 spaces', () => {
54127
suiteSetup(done => {
55128
initialize().then(() => {
@@ -650,7 +723,6 @@ suite('Else blocks with indentation of Tab', () => {
650723
closeActiveWindows().then(done, done);
651724
});
652725

653-
const TAB = ' ';
654726
interface TestCase {
655727
title: string;
656728
line: number;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if True == True:
2+
a = 2
3+
b = 3
4+
else:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if True == True:
2+
a = 2
3+
b = 3
4+
else:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if True == True:
2+
a = 2
3+
b = 3
4+
else:

0 commit comments

Comments
 (0)