@@ -4,13 +4,23 @@ import * as diagnosticUtils from './diagnosticUtils';
4
4
import { Range , DiagnosticSeverity } from 'vscode-languageserver' ;
5
5
import { util } from './util' ;
6
6
import chalk from 'chalk' ;
7
+ import { createSandbox } from 'sinon' ;
8
+ import undent from 'undent' ;
9
+ import type { BsDiagnostic } from './interfaces' ;
10
+ import { stripConsoleColors } from './testHelpers.spec' ;
11
+ const sinon = createSandbox ( ) ;
7
12
8
13
describe ( 'diagnosticUtils' , ( ) => {
9
14
let options : ReturnType < typeof diagnosticUtils . getPrintDiagnosticOptions > ;
10
15
beforeEach ( ( ) => {
16
+ sinon . restore ( ) ;
11
17
options = diagnosticUtils . getPrintDiagnosticOptions ( { } ) ;
12
18
} ) ;
13
19
20
+ afterEach ( ( ) => {
21
+ sinon . restore ( ) ;
22
+ } ) ;
23
+
14
24
describe ( 'printDiagnostic' , ( ) => {
15
25
it ( 'does not crash when range is undefined' , ( ) => {
16
26
//print a diagnostic that doesn't have a range...it should not explode
@@ -25,10 +35,73 @@ describe('diagnosticUtils', () => {
25
35
//print a diagnostic that doesn't have a range...it should not explode
26
36
diagnosticUtils . printDiagnostic ( options , DiagnosticSeverity . Error , undefined , [ ] , {
27
37
message : 'Bad thing happened' ,
28
- range : Range . create ( 0 , 0 , 2 , 2 ) , //important...this needs to be null for the test to pass,
38
+ range : Range . create ( 0 , 0 , 2 , 2 ) ,
29
39
code : 1234
30
40
} as any ) ;
31
41
} ) ;
42
+
43
+ function testPrintDiagnostic ( diagnostic : BsDiagnostic , code : string , expected : string ) {
44
+ let logOutput = '' ;
45
+ sinon . stub ( console , 'log' ) . callsFake ( ( ...args : any [ ] ) => {
46
+ if ( logOutput . length > 0 ) {
47
+ logOutput += '\n' ;
48
+ }
49
+ logOutput += stripConsoleColors ( args . join ( ' ' ) ) ;
50
+ } ) ;
51
+ //print a diagnostic that doesn't have a range...it should not explode
52
+ diagnosticUtils . printDiagnostic ( options , DiagnosticSeverity . Error , undefined , code . split ( / \r ? \n / g) , diagnostic ) ;
53
+
54
+ //remove leading and trailing newlines
55
+ logOutput = logOutput . replace ( / ^ [ \r \n ] * / g, '' ) . replace ( / [ \r \n ] * $ / g, '' ) ;
56
+ expected = undent ( logOutput ) . replace ( / ^ [ \r \n ] * / g, '' ) . replace ( / [ \r \n ] * $ / g, '' ) ;
57
+
58
+ expect ( logOutput ) . to . eql ( expected ) ;
59
+ }
60
+
61
+ it ( 'handles mixed tabs and spaces' , ( ) => {
62
+ testPrintDiagnostic (
63
+ {
64
+ message : 'Bad thing happened' ,
65
+ range : Range . create ( 0 , 5 , 0 , 18 ) ,
66
+ code : 1234
67
+ } as any ,
68
+ `\t \t print "hello"` ,
69
+ `
70
+ <unknown file>:1:6 - error BS1234: Bad thing happened
71
+ 1 print "hello"
72
+ _ ~~~~~~~~~~~~~
73
+ ` ) ;
74
+ } ) ;
75
+
76
+ it ( 'handles only tabs' , ( ) => {
77
+ testPrintDiagnostic (
78
+ {
79
+ message : 'Bad thing happened' ,
80
+ range : Range . create ( 0 , 5 , 0 , 18 ) ,
81
+ code : 1234
82
+ } as any ,
83
+ `\tprint "hello"` ,
84
+ `
85
+ <unknown file>:1:6 - error BS1234: Bad thing happened
86
+ 1 print "hello"
87
+ _ ~~~~~~~~~~~~~
88
+ ` ) ;
89
+ } ) ;
90
+
91
+ it ( 'handles only spaces' , ( ) => {
92
+ testPrintDiagnostic (
93
+ {
94
+ message : 'Bad thing happened' ,
95
+ range : Range . create ( 0 , 5 , 0 , 18 ) ,
96
+ code : 1234
97
+ } as any ,
98
+ ` print "hello"` ,
99
+ `
100
+ <unknown file>:1:6 - error BS1234: Bad thing happened
101
+ 1 print "hello"
102
+ _ ~~~~~~~~~~~~~
103
+ ` ) ;
104
+ } ) ;
32
105
} ) ;
33
106
34
107
describe ( 'getPrintDiagnosticOptions' , ( ) => {
@@ -92,76 +165,75 @@ describe('diagnosticUtils', () => {
92
165
93
166
describe ( 'getDiagnosticSquiggly' , ( ) => {
94
167
it ( 'works for normal cases' , ( ) => {
95
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
96
- range : Range . create ( 0 , 0 , 0 , 4 )
97
- } , 'asdf' ) ) . to . equal ( '~~~~' ) ;
168
+ expect (
169
+ diagnosticUtils . getDiagnosticSquigglyText ( 'asdf' , 0 , 4 )
170
+ ) . to . equal ( '~~~~' ) ;
98
171
} ) ;
99
172
100
173
it ( 'highlights whole line if no range' , ( ) => {
101
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
102
- } , ' asdf ' ) ) . to . equal ( '~~~~~~' ) ;
174
+ expect (
175
+ diagnosticUtils . getDiagnosticSquigglyText ( ' asdf ' , undefined , undefined )
176
+ ) . to . equal ( '~~~~~~' ) ;
103
177
} ) ;
104
178
105
179
it ( 'returns empty string when no line is found' , ( ) => {
106
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
107
- range : Range . create ( 0 , 0 , 0 , 10 )
108
- } , '' ) ) . to . equal ( '' ) ;
180
+ expect ( diagnosticUtils . getDiagnosticSquigglyText ( '' , 0 , 10 ) ) . to . equal ( '' ) ;
109
181
110
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
111
- range : Range . create ( 0 , 0 , 0 , 10 )
112
- } , undefined ) ) . to . equal ( '' ) ;
182
+ expect (
183
+ diagnosticUtils . getDiagnosticSquigglyText ( undefined , 0 , 10 )
184
+ ) . to . equal ( '' ) ;
113
185
} ) ;
114
186
115
187
it ( 'supports diagnostic not at start of line' , ( ) => {
116
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
117
- range : Range . create ( 0 , 2 , 0 , 6 )
118
- } , ' asdf' ) ) . to . equal ( ' ~~~~' ) ;
188
+ expect (
189
+ diagnosticUtils . getDiagnosticSquigglyText ( ' asdf' , 2 , 6 )
190
+ ) . to . equal ( ' ~~~~' ) ;
119
191
} ) ;
120
192
121
193
it ( 'supports diagnostic that does not finish at end of line' , ( ) => {
122
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
123
- range : Range . create ( 0 , 0 , 0 , 4 )
124
- } , 'asdf ' ) ) . to . equal ( '~~~~ ' ) ;
194
+ expect (
195
+ diagnosticUtils . getDiagnosticSquigglyText ( 'asdf ' , 0 , 4 )
196
+ ) . to . equal ( '~~~~ ' ) ;
125
197
} ) ;
126
198
127
199
it ( 'supports diagnostic with space on both sides' , ( ) => {
128
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
129
- range : Range . create ( 0 , 2 , 0 , 6 )
130
- } , ' asdf ' ) ) . to . equal ( ' ~~~~ ' ) ;
200
+ expect (
201
+ diagnosticUtils . getDiagnosticSquigglyText ( ' asdf ' , 2 , 6 )
202
+ ) . to . equal ( ' ~~~~ ' ) ;
131
203
} ) ;
132
204
133
205
it ( 'handles diagnostic that starts and stops on the same position' , ( ) => {
134
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
135
- range : Range . create ( 0 , 2 , 0 , 2 )
136
- } , 'abcde' ) ) . to . equal ( '~~~~~' ) ;
206
+ expect (
207
+ diagnosticUtils . getDiagnosticSquigglyText ( 'abcde' , 2 , 2 )
208
+ ) . to . equal ( '~~~~~' ) ;
137
209
} ) ;
138
210
139
211
it ( 'handles single-character diagnostic' , ( ) => {
140
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
141
- range : Range . create ( 0 , 2 , 0 , 3 )
142
- } , 'abcde' ) ) . to . equal ( ' ~ ' ) ;
212
+ expect (
213
+ diagnosticUtils . getDiagnosticSquigglyText ( 'abcde' , 2 , 3 )
214
+ ) . to . equal ( ' ~ ' ) ;
143
215
} ) ;
144
216
145
217
it ( 'handles diagnostics that are longer than the line' , ( ) => {
146
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
147
- range : Range . create ( 0 , 0 , 0 , 10 )
148
- } , 'abcde' ) ) . to . equal ( '~~~~~' ) ;
218
+ expect (
219
+ diagnosticUtils . getDiagnosticSquigglyText ( 'abcde' , 0 , 10 )
220
+ ) . to . equal ( '~~~~~' ) ;
149
221
150
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
151
- range : Range . create ( 0 , 2 , 0 , 10 )
152
- } , 'abcde' ) ) . to . equal ( ' ~~~' ) ;
222
+ expect (
223
+ diagnosticUtils . getDiagnosticSquigglyText ( 'abcde' , 2 , 10 )
224
+ ) . to . equal ( ' ~~~' ) ;
153
225
} ) ;
154
226
155
227
it ( 'handles Number.MAX_VALUE for end character' , ( ) => {
156
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
157
- range : util . createRange ( 0 , 0 , 0 , Number . MAX_VALUE )
158
- } , 'abcde' ) ) . to . equal ( '~~~~~' ) ;
228
+ expect (
229
+ diagnosticUtils . getDiagnosticSquigglyText ( 'abcde' , 0 , Number . MAX_VALUE )
230
+ ) . to . equal ( '~~~~~' ) ;
159
231
} ) ;
160
232
161
233
it . skip ( 'handles edge cases' , ( ) => {
162
- expect ( diagnosticUtils . getDiagnosticSquigglyText ( < any > {
163
- range : Range . create ( 5 , 16 , 5 , 18 )
164
- } , 'end functionasdf' ) ) . to . equal ( ' ~~~~' ) ;
234
+ expect (
235
+ diagnosticUtils . getDiagnosticSquigglyText ( 'end functionasdf' , 16 , 18 )
236
+ ) . to . equal ( ' ~~~~' ) ;
165
237
} ) ;
166
238
} ) ;
167
239
0 commit comments