5
5
import * as assert from 'assert' ;
6
6
import * as fs from 'fs' ;
7
7
import * as path from 'path' ;
8
+ import * as TypeMoq from 'typemoq' ;
9
+ import { TextDocument , TextLine } from 'vscode' ;
8
10
import '../../client/common/extensions' ;
9
11
import { LineFormatter } from '../../client/formatters/lineFormatter' ;
10
12
@@ -17,124 +19,142 @@ suite('Formatting - line formatter', () => {
17
19
const formatter = new LineFormatter ( ) ;
18
20
19
21
test ( 'Operator spacing' , ( ) => {
20
- const actual = formatter . formatLine ( '( x +1 )*y/ 3' ) ;
21
- assert . equal ( actual , '(x + 1) * y / 3' ) ;
22
+ testFormatLine ( '( x +1 )*y/ 3' , '(x + 1) * y / 3' ) ;
22
23
} ) ;
23
24
test ( 'Braces spacing' , ( ) => {
24
- const actual = formatter . formatLine ( 'foo =(0 ,)' ) ;
25
- assert . equal ( actual , 'foo = (0,)' ) ;
25
+ testFormatLine ( 'foo =(0 ,)' , 'foo = (0,)' ) ;
26
26
} ) ;
27
27
test ( 'Function arguments' , ( ) => {
28
- const actual = formatter . formatLine ( ' foo (0 , x= 1, (3+7) , y , z )') ;
29
- assert . equal ( actual , ' foo(0, x=1, (3 + 7), y, z)') ;
28
+ testFormatLine ( 'z= foo (0 , x= 1, (3+7) , y , z )',
29
+ 'z = foo(0, x=1, (3 + 7), y, z)') ;
30
30
} ) ;
31
31
test ( 'Colon regular' , ( ) => {
32
- const actual = formatter . formatLine ( 'if x == 4 : print x,y; x,y= y, x' ) ;
33
- assert . equal ( actual , 'if x == 4: print x, y; x, y = y, x' ) ;
32
+ testFormatLine ( 'if x == 4 : print x,y; x,y= y, x' ,
33
+ 'if x == 4: print x, y; x, y = y, x' ) ;
34
34
} ) ;
35
35
test ( 'Colon slices' , ( ) => {
36
- const actual = formatter . formatLine ( 'x[1: 30]' ) ;
37
- assert . equal ( actual , 'x[1:30]' ) ;
36
+ testFormatLine ( 'x[1: 30]' , 'x[1:30]' ) ;
38
37
} ) ;
39
38
test ( 'Colon slices in arguments' , ( ) => {
40
- const actual = formatter . formatLine ( 'spam ( ham[ 1 :3], {eggs : 2})' ) ;
41
- assert . equal ( actual , 'spam(ham[1:3], {eggs: 2})' ) ;
39
+ testFormatLine ( 'spam ( ham[ 1 :3], {eggs : 2})' ,
40
+ 'spam(ham[1:3], {eggs: 2})' ) ;
42
41
} ) ;
43
42
test ( 'Colon slices with double colon' , ( ) => {
44
- const actual = formatter . formatLine ( 'ham [1:9 ], ham[ 1: 9: 3], ham[: 9 :3], ham[1: :3], ham [ 1: 9:]' ) ;
45
- assert . equal ( actual , 'ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]' ) ;
43
+ testFormatLine ( 'ham [1:9 ], ham[ 1: 9: 3], ham[: 9 :3], ham[1: :3], ham [ 1: 9:]' ,
44
+ 'ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]' ) ;
46
45
} ) ;
47
46
test ( 'Colon slices with operators' , ( ) => {
48
- const actual = formatter . formatLine ( 'ham [lower+ offset :upper+offset]' ) ;
49
- assert . equal ( actual , 'ham[lower + offset:upper + offset]' ) ;
47
+ testFormatLine ( 'ham [lower+ offset :upper+offset]' ,
48
+ 'ham[lower + offset:upper + offset]' ) ;
50
49
} ) ;
51
50
test ( 'Colon slices with functions' , ( ) => {
52
- const actual = formatter . formatLine ( 'ham[ : upper_fn ( x) : step_fn(x )], ham[ :: step_fn(x)]' ) ;
53
- assert . equal ( actual , 'ham[:upper_fn(x):step_fn(x)], ham[::step_fn(x)]' ) ;
51
+ testFormatLine ( 'ham[ : upper_fn ( x) : step_fn(x )], ham[ :: step_fn(x)]' ,
52
+ 'ham[:upper_fn(x):step_fn(x)], ham[::step_fn(x)]' ) ;
54
53
} ) ;
55
54
test ( 'Colon in for loop' , ( ) => {
56
- const actual = formatter . formatLine ( 'for index in range( len(fruits) ): ' ) ;
57
- assert . equal ( actual , 'for index in range(len(fruits)):' ) ;
55
+ testFormatLine ( 'for index in range( len(fruits) ): ' ,
56
+ 'for index in range(len(fruits)):' ) ;
58
57
} ) ;
59
58
test ( 'Nested braces' , ( ) => {
60
- const actual = formatter . formatLine ( '[ 1 :[2: (x,),y]]{1}' ) ;
61
- assert . equal ( actual , '[1:[2:(x,), y]]{1}' ) ;
59
+ testFormatLine ( '[ 1 :[2: (x,),y]]{1}' , '[1:[2:(x,), y]]{1}' ) ;
62
60
} ) ;
63
61
test ( 'Trailing comment' , ( ) => {
64
- const actual = formatter . formatLine ( 'x=1 # comment' ) ;
65
- assert . equal ( actual , 'x = 1 # comment' ) ;
62
+ testFormatLine ( 'x=1 # comment' , 'x = 1 # comment' ) ;
66
63
} ) ;
67
64
test ( 'Single comment' , ( ) => {
68
- const actual = formatter . formatLine ( '# comment' ) ;
69
- assert . equal ( actual , '# comment' ) ;
65
+ testFormatLine ( '# comment' , '# comment' ) ;
70
66
} ) ;
71
67
test ( 'Comment with leading whitespace' , ( ) => {
72
- const actual = formatter . formatLine ( ' # comment' ) ;
73
- assert . equal ( actual , ' # comment' ) ;
68
+ testFormatLine ( ' # comment' , ' # comment' ) ;
74
69
} ) ;
75
70
test ( 'Equals in first argument' , ( ) => {
76
- const actual = formatter . formatLine ( 'foo(x =0)' ) ;
77
- assert . equal ( actual , 'foo(x=0)' ) ;
71
+ testFormatLine ( 'foo(x =0)' , 'foo(x=0)' ) ;
78
72
} ) ;
79
73
test ( 'Equals in second argument' , ( ) => {
80
- const actual = formatter . formatLine ( 'foo(x,y= \"a\",' ) ;
81
- assert . equal ( actual , 'foo(x, y=\"a\",' ) ;
74
+ testFormatLine ( 'foo(x,y= \"a\",' , 'foo(x, y=\"a\",' ) ;
82
75
} ) ;
83
76
test ( 'Equals in multiline arguments' , ( ) => {
84
- const actual = formatter . formatLine ( 'x = 1,y =-2)' ) ;
85
- assert . equal ( actual , 'x=1, y=-2)' ) ;
77
+ testFormatLine2 ( 'foo(a,' , 'x = 1,y =-2)' , 'x=1, y=-2)' ) ;
86
78
} ) ;
87
79
test ( 'Equals in multiline arguments starting comma' , ( ) => {
88
- const actual = formatter . formatLine ( ',x = 1,y =m)' ) ;
89
- assert . equal ( actual , ', x=1, y=m)' ) ;
80
+ testFormatLine ( ',x = 1,y =m)' , ', x=1, y=m)' ) ;
90
81
} ) ;
91
82
test ( 'Equals in multiline arguments ending comma' , ( ) => {
92
- const actual = formatter . formatLine ( 'x = 1,y =m,' ) ;
93
- assert . equal ( actual , 'x=1, y=m,' ) ;
83
+ testFormatLine ( 'x = 1,y =m,' , 'x=1, y=m,' ) ;
94
84
} ) ;
95
85
test ( 'Operators without following space' , ( ) => {
96
- const actual = formatter . formatLine ( 'foo( *a, ** b, ! c)' ) ;
97
- assert . equal ( actual , 'foo(*a, **b, !c)' ) ;
86
+ testFormatLine ( 'foo( *a, ** b, ! c)' , 'foo(*a, **b, !c)' ) ;
98
87
} ) ;
99
88
test ( 'Brace after keyword' , ( ) => {
100
- const actual = formatter . formatLine ( 'for x in(1,2,3)' ) ;
101
- assert . equal ( actual , 'for x in (1, 2, 3)' ) ;
89
+ testFormatLine ( 'for x in(1,2,3)' , 'for x in (1, 2, 3)' ) ;
102
90
} ) ;
103
91
test ( 'Dot operator' , ( ) => {
104
- const actual = formatter . formatLine ( 'x.y' ) ;
105
- assert . equal ( actual , 'x.y' ) ;
92
+ testFormatLine ( 'x.y' , 'x.y' ) ;
106
93
} ) ;
107
94
test ( 'Unknown tokens no space' , ( ) => {
108
- const actual = formatter . formatLine ( 'abc\\n\\' ) ;
109
- assert . equal ( actual , 'abc\\n\\' ) ;
95
+ testFormatLine ( 'abc\\n\\' , 'abc\\n\\' ) ;
110
96
} ) ;
111
97
test ( 'Unknown tokens with space' , ( ) => {
112
- const actual = formatter . formatLine ( 'abc \\n \\' ) ;
113
- assert . equal ( actual , 'abc \\n \\' ) ;
98
+ testFormatLine ( 'abc \\n \\' , 'abc \\n \\' ) ;
114
99
} ) ;
115
100
test ( 'Double asterisk' , ( ) => {
116
- const actual = formatter . formatLine ( 'a**2, ** k' ) ;
117
- assert . equal ( actual , 'a ** 2, **k' ) ;
101
+ testFormatLine ( 'a**2, ** k' , 'a ** 2, **k' ) ;
118
102
} ) ;
119
103
test ( 'Lambda' , ( ) => {
120
- const actual = formatter . formatLine ( 'lambda * args, :0' ) ;
121
- assert . equal ( actual , 'lambda *args,: 0' ) ;
104
+ testFormatLine ( 'lambda * args, :0' , 'lambda *args,: 0' ) ;
122
105
} ) ;
123
106
test ( 'Comma expression' , ( ) => {
124
- const actual = formatter . formatLine ( 'x=1,2,3' ) ;
125
- assert . equal ( actual , 'x = 1, 2, 3' ) ;
107
+ testFormatLine ( 'x=1,2,3' , 'x = 1, 2, 3' ) ;
126
108
} ) ;
127
109
test ( 'is exression' , ( ) => {
128
- const actual = formatter . formatLine ( 'a( (False is 2) is 3)' ) ;
129
- assert . equal ( actual , 'a((False is 2) is 3)' ) ;
110
+ testFormatLine ( 'a( (False is 2) is 3)' , 'a((False is 2) is 3)' ) ;
111
+ } ) ;
112
+ test ( 'Function returning tuple' , ( ) => {
113
+ testFormatLine ( 'x,y=f(a)' , 'x, y = f(a)' ) ;
130
114
} ) ;
131
115
test ( 'Grammar file' , ( ) => {
132
116
const content = fs . readFileSync ( grammarFile ) . toString ( 'utf8' ) ;
133
117
const lines = content . splitLines ( { trim : false , removeEmptyEntries : false } ) ;
118
+ let prevLine = '' ;
134
119
for ( let i = 0 ; i < lines . length ; i += 1 ) {
135
120
const line = lines [ i ] ;
136
- const actual = formatter . formatLine ( line ) ;
137
- assert . equal ( actual , line , `Line ${ i + 1 } changed: '${ line } ' to '${ actual } '` ) ;
121
+ const actual = formatLine2 ( prevLine , line ) ;
122
+ assert . equal ( actual , line , `Line ${ i + 1 } changed: '${ line . trim ( ) } ' to '${ actual . trim ( ) } '` ) ;
123
+ prevLine = line ;
138
124
}
139
125
} ) ;
126
+
127
+ function testFormatLine ( text : string , expected : string ) : void {
128
+ const actual = formatLine ( text ) ;
129
+ assert . equal ( actual , expected ) ;
130
+ }
131
+
132
+ function formatLine ( text : string ) : string {
133
+ const line = TypeMoq . Mock . ofType < TextLine > ( ) ;
134
+ line . setup ( x => x . text ) . returns ( ( ) => text ) ;
135
+
136
+ const document = TypeMoq . Mock . ofType < TextDocument > ( ) ;
137
+ document . setup ( x => x . lineAt ( TypeMoq . It . isAnyNumber ( ) ) ) . returns ( ( ) => line . object ) ;
138
+
139
+ return formatter . formatLine ( document . object , 0 ) ;
140
+ }
141
+
142
+ function formatLine2 ( prevLineText : string , lineText : string ) : string {
143
+ const thisLine = TypeMoq . Mock . ofType < TextLine > ( ) ;
144
+ thisLine . setup ( x => x . text ) . returns ( ( ) => lineText ) ;
145
+
146
+ const prevLine = TypeMoq . Mock . ofType < TextLine > ( ) ;
147
+ prevLine . setup ( x => x . text ) . returns ( ( ) => prevLineText ) ;
148
+
149
+ const document = TypeMoq . Mock . ofType < TextDocument > ( ) ;
150
+ document . setup ( x => x . lineAt ( 0 ) ) . returns ( ( ) => prevLine . object ) ;
151
+ document . setup ( x => x . lineAt ( 1 ) ) . returns ( ( ) => thisLine . object ) ;
152
+
153
+ return formatter . formatLine ( document . object , 1 ) ;
154
+ }
155
+
156
+ function testFormatLine2 ( prevLineText : string , lineText : string , expected : string ) : void {
157
+ const actual = formatLine2 ( prevLineText , lineText ) ;
158
+ assert . equal ( actual , expected ) ;
159
+ }
140
160
} ) ;
0 commit comments