|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
1 | 3 |
|
2 | | -// Note: This example test is leveraging the Mocha test framework. |
3 | | -// Please refer to their documentation on https://mochajs.org/ for help. |
| 4 | +'use strict'; |
4 | 5 |
|
5 | | -import * as assert from 'assert'; |
| 6 | +import { expect } from 'chai'; |
6 | 7 | import * as path from 'path'; |
7 | | -import * as vscode from 'vscode'; |
| 8 | +import { CancellationTokenSource, Position, TextDocument, workspace } from 'vscode'; |
| 9 | +import { EXTENSION_ROOT_DIR } from '../../client/constants'; |
| 10 | +import { OnEnterFormatter } from '../../client/typeFormatters/onEnterFormatter'; |
8 | 11 | import { closeActiveWindows, initialize } from '../initialize'; |
9 | 12 |
|
10 | | -const formatFilesPath = path.join(__dirname, '..', '..', '..', 'src', 'test', 'pythonFiles', 'formatting'); |
| 13 | +const formatFilesPath = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'pythonFiles', 'formatting'); |
11 | 14 | const unformattedFile = path.join(formatFilesPath, 'fileToFormatOnEnter.py'); |
12 | 15 |
|
13 | 16 | suite('Formatting - OnEnter provider', () => { |
14 | | - let document: vscode.TextDocument; |
15 | | - let editor: vscode.TextEditor; |
16 | | - |
17 | | - suiteSetup(initialize); |
18 | | - setup(async () => { |
19 | | - document = await vscode.workspace.openTextDocument(unformattedFile); |
20 | | - editor = await vscode.window.showTextDocument(document); |
| 17 | + let document: TextDocument; |
| 18 | + let formatter: OnEnterFormatter; |
| 19 | + suiteSetup(async () => { |
| 20 | + await initialize(); |
| 21 | + document = await workspace.openTextDocument(unformattedFile); |
| 22 | + formatter = new OnEnterFormatter(); |
21 | 23 | }); |
22 | 24 | suiteTeardown(closeActiveWindows); |
23 | | - teardown(closeActiveWindows); |
24 | 25 |
|
25 | | - test('Simple statement', async () => { |
26 | | - const text = await formatAtPosition(1, 0); |
27 | | - assert.equal(text, 'x = 1', 'Line was not formatted'); |
28 | | - }); |
| 26 | + test('Simple statement', () => testFormattingAtPosition(1, 0, 'x = 1')); |
29 | 27 |
|
30 | | - test('No formatting inside strings', async () => { |
31 | | - let text = await formatAtPosition(2, 0); |
32 | | - assert.equal(text, '"""x=1', 'Text inside string was formatted'); |
33 | | - text = await formatAtPosition(3, 0); |
34 | | - assert.equal(text, '"""', 'Text inside string was formatted'); |
35 | | - }); |
| 28 | + test('No formatting inside strings (2)', () => doesNotFormat(2, 0)); |
36 | 29 |
|
37 | | - test('Whitespace before comment', async () => { |
38 | | - const text = await formatAtPosition(4, 0); |
39 | | - assert.equal(text, ' # comment', 'Whitespace before comment was not preserved'); |
40 | | - }); |
| 30 | + test('No formatting inside strings (3)', () => doesNotFormat(3, 0)); |
41 | 31 |
|
42 | | - test('No formatting of comment', async () => { |
43 | | - const text = await formatAtPosition(5, 0); |
44 | | - assert.equal(text, '# x=1', 'Text inside comment was formatted'); |
45 | | - }); |
| 32 | + test('Whitespace before comment', () => doesNotFormat(4, 0)); |
46 | 33 |
|
47 | | - test('Formatting line ending in comment', async () => { |
48 | | - const text = await formatAtPosition(6, 0); |
49 | | - assert.equal(text, 'x + 1 # ', 'Line ending in comment was not formatted'); |
50 | | - }); |
| 34 | + test('No formatting of comment', () => doesNotFormat(5, 0)); |
51 | 35 |
|
52 | | - test('Formatting line with @', async () => { |
53 | | - const text = await formatAtPosition(7, 0); |
54 | | - assert.equal(text, '@x', 'Line with @ was reformatted'); |
55 | | - }); |
| 36 | + test('Formatting line ending in comment', () => testFormattingAtPosition(6, 0, 'x + 1 # ')); |
56 | 37 |
|
57 | | - test('Formatting line with @', async () => { |
58 | | - const text = await formatAtPosition(8, 0); |
59 | | - assert.equal(text, 'x.y', 'Line ending with period was reformatted'); |
60 | | - }); |
| 38 | + test('Formatting line with @', () => doesNotFormat(7, 0)); |
61 | 39 |
|
62 | | - test('Formatting line with unknown neighboring tokens', async () => { |
63 | | - const text = await formatAtPosition(9, 0); |
64 | | - assert.equal(text, 'if x <= 1:', 'Line with unknown neighboring tokens was not formatted'); |
65 | | - }); |
| 40 | + test('Formatting line with @', () => doesNotFormat(8, 0)); |
66 | 41 |
|
67 | | - test('Formatting line with unknown neighboring tokens', async () => { |
68 | | - const text = await formatAtPosition(10, 0); |
69 | | - assert.equal(text, 'if 1 <= x:', 'Line with unknown neighboring tokens was not formatted'); |
70 | | - }); |
| 42 | + test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(9, 0, 'if x <= 1:')); |
71 | 43 |
|
72 | | - test('Formatting method definition with arguments', async () => { |
73 | | - const text = await formatAtPosition(11, 0); |
74 | | - assert.equal(text, 'def __init__(self, age=23)', 'Method definition with arguments was not formatted'); |
75 | | - }); |
| 44 | + test('Formatting line with unknown neighboring tokens', () => testFormattingAtPosition(10, 0, 'if 1 <= x:')); |
76 | 45 |
|
77 | | - test('Formatting space after open brace', async () => { |
78 | | - const text = await formatAtPosition(12, 0); |
79 | | - assert.equal(text, 'while (1)', 'Space after open brace was not formatted'); |
80 | | - }); |
| 46 | + test('Formatting method definition with arguments', () => testFormattingAtPosition(11, 0, 'def __init__(self, age=23)')); |
81 | 47 |
|
82 | | - test('Formatting line ending in string', async () => { |
83 | | - const text = await formatAtPosition(13, 0); |
84 | | - assert.equal(text, 'x + """', 'Line ending in multiline string was not formatted'); |
85 | | - }); |
| 48 | + test('Formatting space after open brace', () => testFormattingAtPosition(12, 0, 'while (1)')); |
| 49 | + |
| 50 | + test('Formatting line ending in string', () => testFormattingAtPosition(13, 0, 'x + """')); |
86 | 51 |
|
87 | | - async function formatAtPosition(line: number, character: number): Promise<string> { |
88 | | - const edits = await vscode.commands.executeCommand<vscode.TextEdit[]>('vscode.executeFormatOnTypeProvider', |
89 | | - document.uri, new vscode.Position(line, character), '\n', { insertSpaces: true, tabSize: 2 }); |
90 | | - if (edits) { |
91 | | - await editor.edit(builder => edits.forEach(e => builder.replace(e.range, e.newText))); |
92 | | - } |
93 | | - return document.lineAt(line - 1).text; |
| 52 | + function testFormattingAtPosition(line: number, character: number, expectedFormattedString?: string): void { |
| 53 | + const token = new CancellationTokenSource().token; |
| 54 | + const edits = formatter.provideOnTypeFormattingEdits(document, new Position(line, character), '\n', { insertSpaces: true, tabSize: 2 }, token); |
| 55 | + expect(edits).to.be.lengthOf(1); |
| 56 | + expect(edits[0].newText).to.be.equal(expectedFormattedString); |
| 57 | + } |
| 58 | + function doesNotFormat(line: number, character: number): void { |
| 59 | + const token = new CancellationTokenSource().token; |
| 60 | + const edits = formatter.provideOnTypeFormattingEdits(document, new Position(line, character), '\n', { insertSpaces: true, tabSize: 2 }, token); |
| 61 | + expect(edits).to.be.lengthOf(0); |
94 | 62 | } |
95 | 63 | }); |
0 commit comments