11import test from 'ava' ;
22import ansiCodes from './fixtures/ansi-codes' ;
3- import m from '.' ;
3+ import ansiRegex from '.' ;
44
55const consumptionChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+1234567890-=[]{};\':"./>?,<\\|' ;
66
77// Testing against codes found at: http://ascii-table.com/ansi-escape-sequences-vt-100.php
88test ( '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 ( ) ) ;
1414} ) ;
1515
1616test ( '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 ( ) ) ;
1818} ) ;
1919
2020test ( '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' ) ;
2323} ) ;
2424
2525test ( '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' ) ;
2828} ) ;
2929
3030test ( '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' ) ;
3333} ) ;
3434
3535test ( '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' ) ;
3838} ) ;
3939
4040test ( '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 ) ;
4242} ) ;
4343
4444test . 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' ) ;
4646} ) ;
4747
4848// Testing against extended codes (excluding codes ending in 0-9)
@@ -61,9 +61,9 @@ for (const codeSet of Object.keys(ansiCodes)) {
6161}
6262
6363const 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' ) ;
6767} ) ;
6868
6969test ( `${ skipText } ${ code } should not overconsume` , t => {
@@ -74,9 +74,9 @@ for (const codeSet of Object.keys(ansiCodes)) {
7474
7575for ( const c of consumptionChars ) {
7676const 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 ) ;
8080}
8181} ) ;
8282}
0 commit comments