55import * as assert from 'assert' ;
66import * as fs from 'fs' ;
77import * as path from 'path' ;
8+ import * as TypeMoq from 'typemoq' ;
9+ import { TextDocument , TextLine } from 'vscode' ;
810import '../../client/common/extensions' ;
911import { LineFormatter } from '../../client/formatters/lineFormatter' ;
1012
@@ -17,124 +19,142 @@ suite('Formatting - line formatter', () => {
1719 const formatter = new LineFormatter ( ) ;
1820
1921 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' ) ;
2223 } ) ;
2324 test ( 'Braces spacing' , ( ) => {
24- const actual = formatter . formatLine ( 'foo =(0 ,)' ) ;
25- assert . equal ( actual , 'foo = (0,)' ) ;
25+ testFormatLine ( 'foo =(0 ,)' , 'foo = (0,)' ) ;
2626 } ) ;
2727 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)') ;
3030 } ) ;
3131 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' ) ;
3434 } ) ;
3535 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]' ) ;
3837 } ) ;
3938 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})' ) ;
4241 } ) ;
4342 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:]' ) ;
4645 } ) ;
4746 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]' ) ;
5049 } ) ;
5150 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)]' ) ;
5453 } ) ;
5554 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)):' ) ;
5857 } ) ;
5958 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}' ) ;
6260 } ) ;
6361 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' ) ;
6663 } ) ;
6764 test ( 'Single comment' , ( ) => {
68- const actual = formatter . formatLine ( '# comment' ) ;
69- assert . equal ( actual , '# comment' ) ;
65+ testFormatLine ( '# comment' , '# comment' ) ;
7066 } ) ;
7167 test ( 'Comment with leading whitespace' , ( ) => {
72- const actual = formatter . formatLine ( ' # comment' ) ;
73- assert . equal ( actual , ' # comment' ) ;
68+ testFormatLine ( ' # comment' , ' # comment' ) ;
7469 } ) ;
7570 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)' ) ;
7872 } ) ;
7973 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\",' ) ;
8275 } ) ;
8376 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)' ) ;
8678 } ) ;
8779 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)' ) ;
9081 } ) ;
9182 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,' ) ;
9484 } ) ;
9585 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)' ) ;
9887 } ) ;
9988 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)' ) ;
10290 } ) ;
10391 test ( 'Dot operator' , ( ) => {
104- const actual = formatter . formatLine ( 'x.y' ) ;
105- assert . equal ( actual , 'x.y' ) ;
92+ testFormatLine ( 'x.y' , 'x.y' ) ;
10693 } ) ;
10794 test ( 'Unknown tokens no space' , ( ) => {
108- const actual = formatter . formatLine ( 'abc\\n\\' ) ;
109- assert . equal ( actual , 'abc\\n\\' ) ;
95+ testFormatLine ( 'abc\\n\\' , 'abc\\n\\' ) ;
11096 } ) ;
11197 test ( 'Unknown tokens with space' , ( ) => {
112- const actual = formatter . formatLine ( 'abc \\n \\' ) ;
113- assert . equal ( actual , 'abc \\n \\' ) ;
98+ testFormatLine ( 'abc \\n \\' , 'abc \\n \\' ) ;
11499 } ) ;
115100 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' ) ;
118102 } ) ;
119103 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' ) ;
122105 } ) ;
123106 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' ) ;
126108 } ) ;
127109 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)' ) ;
130114 } ) ;
131115 test ( 'Grammar file' , ( ) => {
132116 const content = fs . readFileSync ( grammarFile ) . toString ( 'utf8' ) ;
133117 const lines = content . splitLines ( { trim : false , removeEmptyEntries : false } ) ;
118+ let prevLine = '' ;
134119 for ( let i = 0 ; i < lines . length ; i += 1 ) {
135120 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 ;
138124 }
139125 } ) ;
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+ }
140160} ) ;
0 commit comments