forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.onEnterFormat.test.ts
More file actions
76 lines (58 loc) · 2.97 KB
/
extension.onEnterFormat.test.ts
File metadata and controls
76 lines (58 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as path from 'path';
import { CancellationTokenSource, Position, TextDocument, workspace } from 'vscode';
import { EXTENSION_ROOT_DIR } from '../../client/constants';
import { OnEnterFormatter } from '../../client/typeFormatters/onEnterFormatter';
import { closeActiveWindows, initialize } from '../initialize';
const formatFilesPath = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'formatting');
const unformattedFile = path.join(formatFilesPath, 'fileToFormatOnEnter.py');
suite('Formatting - OnEnter provider', () => {
let document: TextDocument;
let formatter: OnEnterFormatter;
suiteSetup(async () => {
await initialize();
document = await workspace.openTextDocument(unformattedFile);
formatter = new OnEnterFormatter();
});
suiteTeardown(closeActiveWindows);
test('Simple statement', () => testFormattingAtPosition(1, 0, 'x = 1'));
test('No formatting inside strings (2)', () => doesNotFormat(2, 0));
test('No formatting inside strings (3)', () => doesNotFormat(3, 0));
test('Whitespace before comment', () => doesNotFormat(4, 0));
test('No formatting of comment', () => doesNotFormat(5, 0));
test('Formatting line ending in comment', () => testFormattingAtPosition(6, 0, 'x + 1 # '));
test('Formatting line with @', () => doesNotFormat(7, 0));
test('Formatting line with @', () => doesNotFormat(8, 0));
test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(9, 0, 'if x <= 1:'));
test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(10, 0, 'if 1 <= x:'));
test('Formatting method definition with arguments', () =>
testFormattingAtPosition(11, 0, 'def __init__(self, age=23)'));
test('Formatting space after open brace', () => testFormattingAtPosition(12, 0, 'while (1)'));
test('Formatting line ending in string', () => testFormattingAtPosition(13, 0, 'x + """'));
function testFormattingAtPosition(line: number, character: number, expectedFormattedString?: string): void {
const token = new CancellationTokenSource().token;
const edits = formatter.provideOnTypeFormattingEdits(
document,
new Position(line, character),
'\n',
{ insertSpaces: true, tabSize: 2 },
token
);
expect(edits).to.be.lengthOf(1);
expect(edits[0].newText).to.be.equal(expectedFormattedString);
}
function doesNotFormat(line: number, character: number): void {
const token = new CancellationTokenSource().token;
const edits = formatter.provideOnTypeFormattingEdits(
document,
new Position(line, character),
'\n',
{ insertSpaces: true, tabSize: 2 },
token
);
expect(edits).to.be.lengthOf(0);
}
});