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+ // Place this right on top
7+ import { initialize , IS_TRAVIS , PYTHON_PATH , closeActiveWindows } from './initialize' ;
8+ // The module 'assert' provides assertion methods from node
9+ import * as assert from 'assert' ;
10+
11+ // You can import and use all API from the 'vscode' module
12+ // as well as import your extension to test it
13+ import * as vscode from 'vscode' ;
14+ import * as path from 'path' ;
15+ import * as settings from '../client/common/configSettings' ;
16+ import * as fs from 'fs-extra' ;
17+ import { BlockFormatProviders } from '../client/typeFormatters/blockFormatProvider' ;
18+ let pythonSettings = settings . PythonSettings . getInstance ( ) ;
19+ let srcPythoFilesPath = path . join ( __dirname , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'typeFormatFiles' ) ;
20+ let outPythoFilesPath = path . join ( __dirname , '..' , 'pythonFiles' , 'typeFormatFiles' ) ;
21+
22+ const tryBlock2OutFilePath = path . join ( outPythoFilesPath , 'tryBlocks2.py' ) ;
23+
24+ suite ( 'Formatting' , ( ) => {
25+ let provider : BlockFormatProviders ;
26+ const formatOptions2 : vscode . FormattingOptions = {
27+ insertSpaces : true , tabSize : 2
28+ } ;
29+
30+ suiteSetup ( done => {
31+ initialize ( ) . then ( ( ) => {
32+ provider = new BlockFormatProviders ( ) ;
33+ pythonSettings . pythonPath = PYTHON_PATH ;
34+ fs . ensureDirSync ( path . dirname ( outPythoFilesPath ) ) ;
35+
36+ [ 'tryBlocks2.py' , 'tryBlocks4.py' , 'tryBlocksTab.py' ] . forEach ( file => {
37+ const targetFile = path . join ( outPythoFilesPath , file ) ;
38+ if ( fs . existsSync ( targetFile ) ) { fs . unlinkSync ( targetFile ) ; }
39+ fs . copySync ( path . join ( srcPythoFilesPath , file ) , targetFile ) ;
40+ } ) ;
41+ } ) . then ( done ) . catch ( done ) ;
42+ } ) ;
43+ suiteTeardown ( done => {
44+ closeActiveWindows ( ) . then ( done , done ) ;
45+ } ) ;
46+ teardown ( done => {
47+ closeActiveWindows ( ) . then ( done , done ) ;
48+ } ) ;
49+
50+ function testFormatting ( fileToFormat : string , position : vscode . Position , expectedEdits : vscode . TextEdit [ ] ) : PromiseLike < void > {
51+ let textEditor : vscode . TextEditor ;
52+ let textDocument : vscode . TextDocument ;
53+ return vscode . workspace . openTextDocument ( fileToFormat ) . then ( document => {
54+ textDocument = document ;
55+ return vscode . window . showTextDocument ( textDocument ) ;
56+ } ) . then ( editor => {
57+ return provider . provideOnTypeFormattingEdits ( textDocument , position , ':' , formatOptions2 , null ) ;
58+ } ) . then ( edits => {
59+ assert . equal ( edits . length , expectedEdits . length , 'Number of edits not the same' ) ;
60+ edits . forEach ( ( edit , index ) => {
61+ const expectedEdit = expectedEdits [ index ] ;
62+ assert . equal ( edit . newText , expectedEdit . newText , `newText for edit is not the same for index = ${ index } ` ) ;
63+ assert . ok ( edit . range . isEqual ( expectedEdit . range ) , `range for edit is not the same for index = ${ index } , provided ${ edit . range + '' } , expected ${ expectedEdit . range + '' } ` ) ;
64+ } ) ;
65+ } , reason => {
66+ assert . fail ( reason , undefined , 'Type Formatting failed' , '' ) ;
67+ } ) ;
68+ }
69+ test ( '1. except off by tab' , done => {
70+ const lineNumber = 6 ;
71+ const pos = new vscode . Position ( lineNumber , 22 ) ;
72+ const expectedEdits = [
73+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
74+ ] ;
75+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
76+ } ) ;
77+ test ( '2. except off by one should not be formatted' , done => {
78+ const lineNumber = 15 ;
79+ const pos = new vscode . Position ( lineNumber , 21 ) ;
80+ const expectedEdits = [ ] ;
81+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
82+ } ) ;
83+ test ( '3. except off by tab inside a for loop' , done => {
84+ const lineNumber = 35 ;
85+ const pos = new vscode . Position ( lineNumber , 13 ) ;
86+ const expectedEdits = [
87+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
88+ ] ;
89+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
90+ } ) ;
91+ test ( '4. except off by one inside a for loop should not be formatted' , done => {
92+ const lineNumber = 47 ;
93+ const pos = new vscode . Position ( lineNumber , 12 ) ;
94+ const expectedEdits = [
95+ ] ;
96+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
97+ } ) ;
98+ test ( '5. except IOError: off by tab inside a for loop' , done => {
99+ const lineNumber = 54 ;
100+ const pos = new vscode . Position ( lineNumber , 19 ) ;
101+ const expectedEdits = [
102+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
103+ ] ;
104+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
105+ } ) ;
106+ test ( '6. else: off by tab inside a for loop' , done => {
107+ const lineNumber = 76 ;
108+ const pos = new vscode . Position ( lineNumber , 9 ) ;
109+ const expectedEdits = [
110+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
111+ ] ;
112+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
113+ } ) ;
114+ test ( '7. except ValueError:: off by tab inside a function' , done => {
115+ const lineNumber = 143 ;
116+ const pos = new vscode . Position ( lineNumber , 22 ) ;
117+ const expectedEdits = [
118+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
119+ ] ;
120+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
121+ } ) ;
122+ test ( '8. except ValueError as err: off by one inside a function should not be formatted' , done => {
123+ const lineNumber = 157 ;
124+ const pos = new vscode . Position ( lineNumber , 25 ) ;
125+ const expectedEdits = [
126+ ] ;
127+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
128+ } ) ;
129+ test ( '9. else: off by tab inside function' , done => {
130+ const lineNumber = 172 ;
131+ const pos = new vscode . Position ( lineNumber , 11 ) ;
132+ const expectedEdits = [
133+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
134+ ] ;
135+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
136+ } ) ;
137+ test ( '10. finally: off by tab inside function' , done => {
138+ const lineNumber = 195 ;
139+ const pos = new vscode . Position ( lineNumber , 12 ) ;
140+ const expectedEdits = [
141+ vscode . TextEdit . delete ( new vscode . Range ( lineNumber , 0 , lineNumber , 2 ) )
142+ ] ;
143+ testFormatting ( tryBlock2OutFilePath , pos , expectedEdits ) . then ( done , done ) ;
144+ } ) ;
145+ } ) ;
0 commit comments