1
1
import test from 'ava' ;
2
2
import ansiCodes from './fixtures/ansi-codes' ;
3
- import m from '.' ;
3
+ import ansiRegex from '.' ;
4
4
5
5
const consumptionChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+1234567890-=[]{};\':"./>?,<\\|' ;
6
6
7
7
// Testing against codes found at: http://ascii-table.com/ansi-escape-sequences-vt-100.php
8
8
test ( 'match ansi code in a string' , t => {
9
- t . regex ( 'foo\u001B[4mcake\u001B[0m' , m ( ) ) ;
10
- t . regex ( '\u001B[4mcake\u001B[0m' , m ( ) ) ;
11
- t . regex ( 'foo\u001B[4mcake\u001B[0m' , m ( ) ) ;
12
- t . regex ( '\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m' , m ( ) ) ;
13
- t . regex ( 'foo\u001B[mfoo' , m ( ) ) ;
9
+ t . regex ( 'foo\u001B[4mcake\u001B[0m' , ansiRegex ( ) ) ;
10
+ t . regex ( '\u001B[4mcake\u001B[0m' , ansiRegex ( ) ) ;
11
+ t . regex ( 'foo\u001B[4mcake\u001B[0m' , ansiRegex ( ) ) ;
12
+ t . regex ( '\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m' , ansiRegex ( ) ) ;
13
+ t . regex ( 'foo\u001B[mfoo' , ansiRegex ( ) ) ;
14
14
} ) ;
15
15
16
16
test ( 'match ansi code from ls command' , t => {
17
- t . regex ( '\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m' , m ( ) ) ;
17
+ t . regex ( '\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m' , ansiRegex ( ) ) ;
18
18
} ) ;
19
19
20
20
test ( 'match reset;setfg;setbg;italics;strike;underline sequence in a string' , t => {
21
- t . regex ( '\u001B[0;33;49;3;9;4mbar\u001B[0m' , m ( ) ) ;
22
- t . is ( 'foo\u001B[0;33;49;3;9;4mbar' . match ( m ( ) ) [ 0 ] , '\u001B[0;33;49;3;9;4m' ) ;
21
+ t . regex ( '\u001B[0;33;49;3;9;4mbar\u001B[0m' , ansiRegex ( ) ) ;
22
+ t . is ( 'foo\u001B[0;33;49;3;9;4mbar' . match ( ansiRegex ( ) ) [ 0 ] , '\u001B[0;33;49;3;9;4m' ) ;
23
23
} ) ;
24
24
25
25
test ( 'match clear tabs sequence in a string' , t => {
26
- t . regex ( 'foo\u001B[0gbar' , m ( ) ) ;
27
- t . is ( 'foo\u001B[0gbar' . match ( m ( ) ) [ 0 ] , '\u001B[0g' ) ;
26
+ t . regex ( 'foo\u001B[0gbar' , ansiRegex ( ) ) ;
27
+ t . is ( 'foo\u001B[0gbar' . match ( ansiRegex ( ) ) [ 0 ] , '\u001B[0g' ) ;
28
28
} ) ;
29
29
30
30
test ( 'match clear line from cursor right in a string' , t => {
31
- t . regex ( 'foo\u001B[Kbar' , m ( ) ) ;
32
- t . is ( 'foo\u001B[Kbar' . match ( m ( ) ) [ 0 ] , '\u001B[K' ) ;
31
+ t . regex ( 'foo\u001B[Kbar' , ansiRegex ( ) ) ;
32
+ t . is ( 'foo\u001B[Kbar' . match ( ansiRegex ( ) ) [ 0 ] , '\u001B[K' ) ;
33
33
} ) ;
34
34
35
35
test ( 'match clear screen in a string' , t => {
36
- t . regex ( 'foo\u001B[2Jbar' , m ( ) ) ;
37
- t . is ( 'foo\u001B[2Jbar' . match ( m ( ) ) [ 0 ] , '\u001B[2J' ) ;
36
+ t . regex ( 'foo\u001B[2Jbar' , ansiRegex ( ) ) ;
37
+ t . is ( 'foo\u001B[2Jbar' . match ( ansiRegex ( ) ) [ 0 ] , '\u001B[2J' ) ;
38
38
} ) ;
39
39
40
40
test ( 'match only first' , t => {
41
- t . is ( 'foo\u001B[4mcake\u001B[0m' . match ( m ( { onlyFirst : true } ) ) . length , 1 ) ;
41
+ t . is ( 'foo\u001B[4mcake\u001B[0m' . match ( ansiRegex ( { onlyFirst : true } ) ) . length , 1 ) ;
42
42
} ) ;
43
43
44
44
test . failing ( 'match "change icon name and window title" in string' , t => {
45
- t . is ( '\u001B]0;sg@tota:~/git/\u0007\u001B[01;32m[sg@tota\u001B[01;37m misc-tests\u001B[01;32m]$' . match ( m ( ) ) [ 0 ] , '\u001B]0;sg@tota:~/git/\u0007' ) ;
45
+ t . is ( '\u001B]0;sg@tota:~/git/\u0007\u001B[01;32m[sg@tota\u001B[01;37m misc-tests\u001B[01;32m]$' . match ( ansiRegex ( ) ) [ 0 ] , '\u001B]0;sg@tota:~/git/\u0007' ) ;
46
46
} ) ;
47
47
48
48
// Testing against extended codes (excluding codes ending in 0-9)
@@ -61,9 +61,9 @@ for (const codeSet of Object.keys(ansiCodes)) {
61
61
}
62
62
63
63
const string = `hel${ ecode } lo` ;
64
- t . regex ( string , m ( ) ) ;
65
- t . is ( string . match ( m ( ) ) [ 0 ] , ecode ) ;
66
- t . is ( string . replace ( m ( ) , '' ) , 'hello' ) ;
64
+ t . regex ( string , ansiRegex ( ) ) ;
65
+ t . is ( string . match ( ansiRegex ( ) ) [ 0 ] , ecode ) ;
66
+ t . is ( string . replace ( ansiRegex ( ) , '' ) , 'hello' ) ;
67
67
} ) ;
68
68
69
69
test ( `${ skipText } ${ code } should not overconsume` , t => {
@@ -74,9 +74,9 @@ for (const codeSet of Object.keys(ansiCodes)) {
74
74
75
75
for ( const c of consumptionChars ) {
76
76
const string = ecode + c ;
77
- t . regex ( string , m ( ) ) ;
78
- t . is ( string . match ( m ( ) ) [ 0 ] , ecode ) ;
79
- t . is ( string . replace ( m ( ) , '' ) , c ) ;
77
+ t . regex ( string , ansiRegex ( ) ) ;
78
+ t . is ( string . match ( ansiRegex ( ) ) [ 0 ] , ecode ) ;
79
+ t . is ( string . replace ( ansiRegex ( ) , '' ) , c ) ;
80
80
}
81
81
} ) ;
82
82
}
0 commit comments