4
4
*--------------------------------------------------------------------------------------------*/
5
5
import * as assert from 'assert' ;
6
6
import * as words from '../utils/strings' ;
7
+ import * as fs from 'fs' ;
8
+ import * as path from 'path' ;
7
9
8
- suite ( 'HTML Words' , ( ) => {
10
+ suite ( 'HTML Language Configuration' , ( ) => {
11
+ const config = JSON . parse ( ( fs . readFileSync ( path . join ( __dirname , '../../../../html/language-configuration.json' ) ) . toString ( ) ) ) ;
9
12
10
- let wordRegex = / ( - ? \d * \. \d \w * ) | ( [ ^ \` \~ \! \@ \# \% \^ \& \* \( \) \- \= \+ \[ \{ \] \} \\ \| \; \: \' \" \, \. \< \> \/ \? \s ] + ) / g;
13
+ function createRegex ( str : string | { pattern : string , flags : string } ) : RegExp {
14
+ if ( typeof str === 'string' ) {
15
+ return new RegExp ( str , 'g' ) ;
16
+ }
17
+ return new RegExp ( str . pattern , str . flags ) ;
18
+ }
19
+
20
+ const wordRegex = createRegex ( config . wordPattern ) ;
11
21
12
22
function assertWord ( value : string , expected : string ) : void {
13
- let offset = value . indexOf ( '|' ) ;
14
- value = value . substr ( 0 , offset ) + value . substr ( offset + 1 ) ;
23
+ const offset = value . indexOf ( '|' ) ;
24
+ value = value . substr ( 0 , offset ) + value . substring ( offset + 1 ) ;
15
25
16
- let actualRange = words . getWordAtText ( value , offset , wordRegex ) ;
26
+ const actualRange = words . getWordAtText ( value , offset , wordRegex ) ;
17
27
assert ( actualRange . start <= offset ) ;
18
28
assert ( actualRange . start + actualRange . length >= offset ) ;
19
29
assert . strictEqual ( value . substr ( actualRange . start , actualRange . length ) , expected ) ;
20
30
}
21
31
22
-
23
- test ( 'Basic' , function ( ) : any {
32
+ test ( 'Words Basic' , function ( ) : any {
24
33
assertWord ( '|var x1 = new F<A>(a, b);' , 'var' ) ;
25
34
assertWord ( 'v|ar x1 = new F<A>(a, b);' , 'var' ) ;
26
35
assertWord ( 'var| x1 = new F<A>(a, b);' , 'var' ) ;
@@ -35,10 +44,28 @@ suite('HTML Words', () => {
35
44
assertWord ( 'var x1 = | new F<A>(a, b)|;|' , '' ) ;
36
45
} ) ;
37
46
38
- test ( 'Multiline' , function ( ) : any {
47
+ test ( 'Words Multiline' , function ( ) : any {
39
48
assertWord ( 'console.log("hello");\n|var x1 = new F<A>(a, b);' , 'var' ) ;
40
49
assertWord ( 'console.log("hello");\n|\nvar x1 = new F<A>(a, b);' , '' ) ;
41
50
assertWord ( 'console.log("hello");\n\r |var x1 = new F<A>(a, b);' , 'var' ) ;
42
51
} ) ;
43
52
53
+ const onEnterBeforeRules : RegExp [ ] = config . onEnterRules . map ( ( r : any ) => createRegex ( r . beforeText ) ) ;
54
+
55
+ function assertBeforeRule ( text : string , expectedMatch : boolean ) : void {
56
+ for ( const reg of onEnterBeforeRules ) {
57
+ const start = new Date ( ) . getTime ( ) ;
58
+ assert . strictEqual ( reg . test ( text ) , expectedMatch ) ;
59
+ const totalTime = new Date ( ) . getTime ( ) - start ;
60
+ assert . ok ( totalTime < 200 , `Evaluation of ${ reg . source } on ${ text } took ${ totalTime } ms]` ) ;
61
+ }
62
+ }
63
+
64
+ test ( 'OnEnter Before' , function ( ) : any {
65
+ assertBeforeRule ( '<button attr1=val1 attr2=val2' , false ) ;
66
+ assertBeforeRule ( '<button attr1=val1 attr2=val2>' , true ) ;
67
+ assertBeforeRule ( '<button attr1=\'val1\' attr2="val2">' , true ) ;
68
+ assertBeforeRule ( '<button attr1=val1 attr2=val2></button>' , false ) ;
69
+ } ) ;
70
+
44
71
} ) ;
0 commit comments