1
1
interface Classification {
2
+ position : number ;
2
3
length : number ;
3
4
class : ts . TokenClass ;
4
5
}
@@ -8,17 +9,10 @@ interface ClassiferResult {
8
9
finalEndOfLineState : ts . EndOfLineState ;
9
10
}
10
11
11
- var TokenClassNames = { } ;
12
- TokenClassNames [ ts . TokenClass . Punctuation ] = "Punctuation" ;
13
- TokenClassNames [ ts . TokenClass . Keyword ] = "Keyword" ;
14
- TokenClassNames [ ts . TokenClass . Operator ] = "Operator" ;
15
- TokenClassNames [ ts . TokenClass . Comment ] = "Comment" ;
16
- TokenClassNames [ ts . TokenClass . Whitespace ] = "Whitespace" ;
17
- TokenClassNames [ ts . TokenClass . Identifier ] = "Identifier" ;
18
- TokenClassNames [ ts . TokenClass . NumberLiteral ] = "NumberLiteral" ;
19
- TokenClassNames [ ts . TokenClass . StringLiteral ] = "StringLiteral" ;
20
- TokenClassNames [ ts . TokenClass . RegExpLiteral ] = "RegExpLiteral" ;
21
-
12
+ interface ClassificationEntry {
13
+ value : any ;
14
+ class : ts . TokenClass ;
15
+ }
22
16
23
17
describe ( 'Colorization' , function ( ) {
24
18
var mytypescriptLS = new Harness . LanguageService . TypeScriptLS ( ) ;
@@ -28,20 +22,21 @@ describe('Colorization', function () {
28
22
var classResult = myclassifier . getClassificationsForLine ( code , initialEndOfLineState ) . split ( '\n' ) ;
29
23
var tuples : Classification [ ] = [ ] ;
30
24
var i = 0 ;
31
- var computedLength = 0 ;
25
+ var position = 0 ;
32
26
33
27
for ( ; i < classResult . length - 1 ; i += 2 ) {
34
28
var t = tuples [ i / 2 ] = {
29
+ position : position ,
35
30
length : parseInt ( classResult [ i ] ) ,
36
31
class : parseInt ( classResult [ i + 1 ] )
37
32
} ;
38
33
39
34
assert . isTrue ( t . length > 0 , "Result length should be greater than 0, got :" + t . length ) ;
40
- computedLength += t . length ;
35
+ position += t . length ;
41
36
}
42
37
var finalEndOfLineState = classResult [ classResult . length - 1 ] ;
43
38
44
- assert . equal ( computedLength , code . length , "Expected accumilative length of all entries to match the length of the source. expected: " + code . length + ", but got: " + computedLength ) ;
39
+ assert . equal ( position , code . length , "Expected accumilative length of all entries to match the length of the source. expected: " + code . length + ", but got: " + position ) ;
45
40
46
41
return {
47
42
tuples : tuples ,
@@ -52,182 +47,132 @@ describe('Colorization', function () {
52
47
function verifyClassification ( classification : Classification , expectedLength : number , expectedClass : number ) {
53
48
assert . isNotNull ( classification ) ;
54
49
assert . equal ( classification . length , expectedLength , "Classification length does not match expected. Expected: " + expectedLength + ", Actual: " + classification . length ) ;
55
- assert . equal ( classification . class , expectedClass , "Classification class does not match expected. Expected: " + TokenClassNames [ expectedClass ] + ", Actual: " + TokenClassNames [ classification . class ] ) ;
50
+ assert . equal ( classification . class , expectedClass , "Classification class does not match expected. Expected: " + ts . TokenClass [ expectedClass ] + ", Actual: " + ts . TokenClass [ classification . class ] ) ;
56
51
}
57
52
58
- describe ( "test cases for colorization" , function ( ) {
59
- var results = getClassifications ( 'var x:string = "foo"; //Hello' ) ;
60
-
61
- it ( "checks for a keyword" , function ( ) {
62
- verifyClassification ( results . tuples [ 0 ] , 3 , ts . TokenClass . Keyword ) ;
63
- } ) ;
64
-
65
- it ( "checks for a whitespace" , function ( ) {
66
- verifyClassification ( results . tuples [ 1 ] , 1 , ts . TokenClass . Whitespace ) ;
67
- } ) ;
68
-
69
- it ( "checks for a identifier" , function ( ) {
70
- verifyClassification ( results . tuples [ 2 ] , 1 , ts . TokenClass . Identifier ) ;
71
- } ) ;
72
-
73
- it ( "checks for an punctuation" , function ( ) {
74
- verifyClassification ( results . tuples [ 3 ] , 1 , ts . TokenClass . Punctuation ) ;
75
- } ) ;
76
-
77
- it ( "checks for a operator" , function ( ) {
78
- verifyClassification ( results . tuples [ 6 ] , 1 , ts . TokenClass . Operator ) ;
79
- } ) ;
80
-
81
- it ( "checks for a string literal" , function ( ) {
82
- verifyClassification ( results . tuples [ 8 ] , 5 , ts . TokenClass . StringLiteral ) ;
83
- } ) ;
84
-
85
- it ( "checks for a comment" , function ( ) {
86
- verifyClassification ( results . tuples [ 11 ] , 7 , ts . TokenClass . Comment ) ;
87
- } ) ;
88
- } ) ;
89
-
90
- describe ( "test comment colorization after a divide operator" , function ( ) {
91
- var results = getClassifications ( '1 / 1 // comment' ) ;
92
-
93
- it ( "checks for a number literal" , function ( ) {
94
- verifyClassification ( results . tuples [ 0 ] , 1 , ts . TokenClass . NumberLiteral ) ;
95
- } ) ;
96
-
97
- it ( "checks for a whitespace" , function ( ) {
98
- verifyClassification ( results . tuples [ 1 ] , 1 , ts . TokenClass . Whitespace ) ;
99
- } ) ;
100
-
101
- it ( "checks for a operator" , function ( ) {
102
- verifyClassification ( results . tuples [ 2 ] , 1 , ts . TokenClass . Operator ) ;
103
- } ) ;
104
-
105
- it ( "checks for a whitespace" , function ( ) {
106
- verifyClassification ( results . tuples [ 3 ] , 1 , ts . TokenClass . Whitespace ) ;
107
- } ) ;
108
-
109
- it ( "checks for a number literal" , function ( ) {
110
- verifyClassification ( results . tuples [ 4 ] , 1 , ts . TokenClass . NumberLiteral ) ;
111
- } ) ;
112
-
113
- it ( "checks for a whitespace" , function ( ) {
114
- verifyClassification ( results . tuples [ 5 ] , 1 , ts . TokenClass . Whitespace ) ;
115
- } ) ;
116
-
117
- it ( "checks for a comment" , function ( ) {
118
- verifyClassification ( results . tuples [ 6 ] , 10 , ts . TokenClass . Comment ) ;
119
- } ) ;
120
- } ) ;
121
-
122
- describe ( "test literal colorization after a divide operator" , function ( ) {
123
- var results = getClassifications ( '1 / 2, 1 / 2' ) ;
124
-
125
- it ( "checks for a number literal" , function ( ) {
126
- verifyClassification ( results . tuples [ 0 ] , 1 , ts . TokenClass . NumberLiteral ) ;
127
- } ) ;
128
-
129
- it ( "checks for a whitespace" , function ( ) {
130
- verifyClassification ( results . tuples [ 1 ] , 1 , ts . TokenClass . Whitespace ) ;
131
- } ) ;
132
-
133
- it ( "checks for a operator" , function ( ) {
134
- verifyClassification ( results . tuples [ 2 ] , 1 , ts . TokenClass . Operator ) ;
135
- } ) ;
136
-
137
- it ( "checks for a whitespace" , function ( ) {
138
- verifyClassification ( results . tuples [ 3 ] , 1 , ts . TokenClass . Whitespace ) ;
139
- } ) ;
140
-
141
- it ( "checks for a number literal" , function ( ) {
142
- verifyClassification ( results . tuples [ 4 ] , 1 , ts . TokenClass . NumberLiteral ) ;
143
- } ) ;
144
-
145
- it ( "checks for a operator" , function ( ) {
146
- verifyClassification ( results . tuples [ 5 ] , 1 , ts . TokenClass . Operator ) ;
147
- } ) ;
148
-
149
- it ( "checks for a whitespace" , function ( ) {
150
- verifyClassification ( results . tuples [ 6 ] , 1 , ts . TokenClass . Whitespace ) ;
151
- } ) ;
152
-
153
- it ( "checks for a number literal" , function ( ) {
154
- verifyClassification ( results . tuples [ 7 ] , 1 , ts . TokenClass . NumberLiteral ) ;
155
- } ) ;
53
+ function getEntryAtPosistion ( result : ClassiferResult , position : number ) {
54
+ for ( var i = 0 , n = result . tuples . length ; i < n ; i ++ ) {
55
+ if ( result . tuples [ i ] . position === position ) return result . tuples [ i ] ;
56
+ }
57
+ return undefined ;
58
+ }
156
59
157
- it ( "checks for a whitespace" , function ( ) {
158
- verifyClassification ( results . tuples [ 8 ] , 1 , ts . TokenClass . Whitespace ) ;
159
- } ) ;
60
+ function punctuation ( text : string ) { return { value : text , class : ts . TokenClass . Punctuation } ; }
61
+ function keyword ( text : string ) { return { value : text , class : ts . TokenClass . Keyword } ; }
62
+ function operator ( text : string ) { return { value : text , class : ts . TokenClass . Operator } ; }
63
+ function comment ( text : string ) { return { value : text , class : ts . TokenClass . Comment } ; }
64
+ function whitespace ( text : string ) { return { value : text , class : ts . TokenClass . Whitespace } ; }
65
+ function identifier ( text : string ) { return { value : text , class : ts . TokenClass . Identifier } ; }
66
+ function numberLiteral ( text : string ) { return { value : text , class : ts . TokenClass . NumberLiteral } ; }
67
+ function stringLiteral ( text : string ) { return { value : text , class : ts . TokenClass . StringLiteral } ; }
68
+ function regExpLiteral ( text : string ) { return { value : text , class : ts . TokenClass . RegExpLiteral } ; }
69
+ function finalEndOfLineState ( value : number ) { return { value : value , class : undefined } ; }
70
+
71
+ function test ( text : string , initialEndOfLineState : ts . EndOfLineState , ...expectedEntries : ClassificationEntry [ ] ) : void {
72
+ var result = getClassifications ( text , initialEndOfLineState ) ;
73
+
74
+ for ( var i = 0 , n = expectedEntries . length ; i < n ; i ++ ) {
75
+ var expectedEntry = expectedEntries [ i ] ;
76
+
77
+ if ( expectedEntry . class === undefined ) {
78
+ assert . equal ( result . finalEndOfLineState , expectedEntry . value , "final endOfLineState does not match expected." ) ;
79
+ }
80
+ else {
81
+ var actualEntryPosition = text . indexOf ( expectedEntry . value ) ;
82
+ assert ( actualEntryPosition >= 0 , "token: '" + expectedEntry . value + "' does not exit in text: '" + text + "'." ) ;
83
+
84
+ var actualEntry = getEntryAtPosistion ( result , actualEntryPosition ) ;
85
+
86
+ assert ( actualEntry , "Could not find classification entry for '" + expectedEntry . value + "' at position: " + actualEntryPosition ) ;
87
+ assert . equal ( actualEntry . length , expectedEntry . value . length , "Classification class does not match expected." ) ;
88
+ assert . equal ( actualEntry . class , expectedEntry . class , "Classification class does not match expected." ) ;
89
+ }
90
+ }
91
+ }
160
92
161
- it ( "checks for a operator" , function ( ) {
162
- verifyClassification ( results . tuples [ 9 ] , 1 , ts . TokenClass . Operator ) ;
93
+ describe ( "test getClassifications" , function ( ) {
94
+ it ( "Returns correct token classes" , function ( ) {
95
+ test ( "var x: string = \"foo\"; //Hello" ,
96
+ ts . EndOfLineState . Start ,
97
+ keyword ( "var" ) ,
98
+ whitespace ( " " ) ,
99
+ identifier ( "x" ) ,
100
+ punctuation ( ":" ) ,
101
+ keyword ( "string" ) ,
102
+ operator ( "=" ) ,
103
+ stringLiteral ( "\"foo\"" ) ,
104
+ comment ( "//Hello" ) ,
105
+ punctuation ( ";" ) ) ;
163
106
} ) ;
164
107
165
- it ( "checks for a whitespace" , function ( ) {
166
- verifyClassification ( results . tuples [ 10 ] , 1 , ts . TokenClass . Whitespace ) ;
108
+ it ( "classifies correctelly a comment after a divide operator" , function ( ) {
109
+ test ( "1 / 2 // comment" ,
110
+ ts . EndOfLineState . Start ,
111
+ numberLiteral ( "1" ) ,
112
+ whitespace ( " " ) ,
113
+ operator ( "/" ) ,
114
+ numberLiteral ( "2" ) ,
115
+ comment ( "// comment" ) ) ;
167
116
} ) ;
168
117
169
- it ( "checks for a number literal" , function ( ) {
170
- verifyClassification ( results . tuples [ 11 ] , 1 , ts . TokenClass . NumberLiteral ) ;
118
+ it ( "classifies correctelly a literal after a divide operator" , function ( ) {
119
+ test ( "1 / 2, 3 / 4" ,
120
+ ts . EndOfLineState . Start ,
121
+ numberLiteral ( "1" ) ,
122
+ whitespace ( " " ) ,
123
+ operator ( "/" ) ,
124
+ numberLiteral ( "2" ) ,
125
+ numberLiteral ( "3" ) ,
126
+ numberLiteral ( "4" ) ,
127
+ operator ( "," ) ) ;
171
128
} ) ;
172
129
173
- } ) ;
174
-
175
- describe ( "test cases for colorizing multi-line string" , function ( ) {
176
- it ( "classifies first line correctelly" , function ( ) {
177
- var results = getClassifications ( "'line1\\" , ts . EndOfLineState . Start ) ;
178
-
179
- assert . equal ( results . tuples . length , 1 ) ;
180
- verifyClassification ( results . tuples [ 0 ] , 7 , ts . TokenClass . StringLiteral ) ;
181
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . InSingleQuoteStringLiteral ) ;
130
+ it ( "classifies correctelly an unterminated multi-line string" , function ( ) {
131
+ test ( "'line1\\" ,
132
+ ts . EndOfLineState . Start ,
133
+ stringLiteral ( "'line1\\" ) ,
134
+ finalEndOfLineState ( ts . EndOfLineState . InSingleQuoteStringLiteral ) ) ;
182
135
} ) ;
183
136
184
- it ( "classifies second line correctelly" , function ( ) {
185
- var results = getClassifications ( "\\" , ts . EndOfLineState . InDoubleQuoteStringLiteral ) ;
186
-
187
- assert . equal ( results . tuples . length , 1 ) ;
188
- verifyClassification ( results . tuples [ 0 ] , 1 , ts . TokenClass . StringLiteral ) ;
189
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . InDoubleQuoteStringLiteral ) ;
137
+ it ( "classifies correctelly the second line of an unterminated multi-line string" , function ( ) {
138
+ test ( "\\" ,
139
+ ts . EndOfLineState . InDoubleQuoteStringLiteral ,
140
+ stringLiteral ( "\\" ) ,
141
+ finalEndOfLineState ( ts . EndOfLineState . InDoubleQuoteStringLiteral ) ) ;
190
142
} ) ;
191
143
192
- it ( "classifies third line correctelly" , function ( ) {
193
- var results = getClassifications ( "'" , ts . EndOfLineState . InSingleQuoteStringLiteral ) ;
194
-
195
- assert . equal ( results . tuples . length , 1 ) ;
196
- verifyClassification ( results . tuples [ 0 ] , 1 , ts . TokenClass . StringLiteral ) ;
197
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . Start ) ;
144
+ it ( "classifies correctelly the last line of a multi-line string" , function ( ) {
145
+ test ( "'" ,
146
+ ts . EndOfLineState . InSingleQuoteStringLiteral ,
147
+ stringLiteral ( "'" ) ,
148
+ finalEndOfLineState ( ts . EndOfLineState . Start ) ) ;
198
149
} ) ;
199
- } ) ;
200
150
201
- describe ( "test cases for colorizing unterminted multi-line comment" , function ( ) {
202
- it ( "unterminated multi-line comment correctelly" , function ( ) {
203
- var results = getClassifications ( "/*" , ts . EndOfLineState . Start ) ;
204
-
205
- assert . equal ( results . tuples . length , 1 ) ;
206
- verifyClassification ( results . tuples [ 0 ] , 2 , ts . TokenClass . Comment ) ;
207
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . InMultiLineCommentTrivia ) ;
151
+ it ( "classifies correctelly an unterminated multiline comment" , function ( ) {
152
+ test ( "/*" ,
153
+ ts . EndOfLineState . Start ,
154
+ comment ( "/*" ) ,
155
+ finalEndOfLineState ( ts . EndOfLineState . InMultiLineCommentTrivia ) ) ;
208
156
} ) ;
209
157
210
- it ( "unterminated multi-line comment with trailing space correctelly" , function ( ) {
211
- var results = getClassifications ( "/* " , ts . EndOfLineState . Start ) ;
212
-
213
- assert . equal ( results . tuples . length , 1 ) ;
214
- verifyClassification ( results . tuples [ 0 ] , 3 , ts . TokenClass . Comment ) ;
215
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . InMultiLineCommentTrivia ) ;
158
+ it ( "classifies correctelly an unterminated multiline comment with trailing space" , function ( ) {
159
+ test ( "/* " ,
160
+ ts . EndOfLineState . Start ,
161
+ comment ( "/* " ) ,
162
+ finalEndOfLineState ( ts . EndOfLineState . InMultiLineCommentTrivia ) ) ;
216
163
} ) ;
217
- } ) ;
218
164
219
- describe ( "test cases for colorizing keywords ", function ( ) {
220
- it ( "classifies keyword after a dot" , function ( ) {
221
- var results = getClassifications ( "a.var" , ts . EndOfLineState . Start ) ;
222
- verifyClassification ( results . tuples [ 2 ] , 3 , ts . TokenClass . Identifier ) ;
165
+ it ( "classifies correctelly a keyword after a dot ", function ( ) {
166
+ test ( "a.var" ,
167
+ ts . EndOfLineState . Start ,
168
+ identifier ( "var" ) ) ;
223
169
} ) ;
224
170
225
171
it ( "classifies keyword after a dot on previous line" , function ( ) {
226
- var results = getClassifications ( "var" , ts . EndOfLineState . EndingWithDotToken ) ;
227
-
228
- assert . equal ( results . tuples . length , 1 ) ;
229
- verifyClassification ( results . tuples [ 0 ] , 3 , ts . TokenClass . Identifier ) ;
230
- assert . equal ( results . finalEndOfLineState , ts . EndOfLineState . Start ) ;
172
+ test ( "var" ,
173
+ ts . EndOfLineState . EndingWithDotToken ,
174
+ identifier ( "var" ) ,
175
+ finalEndOfLineState ( ts . EndOfLineState . Start ) ) ;
231
176
} ) ;
232
177
} ) ;
233
178
} ) ;
0 commit comments