1
- import { selectionTestGrep } from '../../src/utils/regexp' ;
1
+ import { convertOneGroup , selectionTestGrep , simplifyParentheses } from '../../src/utils/regexp' ;
2
2
import expect from 'expect' ;
3
3
4
4
describe ( 'suite' , ( ) => {
@@ -68,7 +68,7 @@ describe('suite', () => {
68
68
{
69
69
desc : 'parenthesis several more complex - many parent parenthesis' ,
70
70
GREP : '((((((.*)&!((my test)|(his test))&(.*))))))' ,
71
- regExpected : / (? = .* . * ) + (? = .* ^ (? ! .* ( m y t e s t | h i s t e s t ) .* ) ) + (? = . * . * ) + .* / i,
71
+ regExpected : / (? = .* ) + (? = .* ^ (? ! .* ( m y t e s t | h i s t e s t ) .* ) ) + .* / i,
72
72
cases : [
73
73
{ expectMatch : true , testLine : 'test her' } ,
74
74
{ expectMatch : true , testLine : 'her test' } ,
@@ -376,6 +376,23 @@ describe('suite', () => {
376
376
{ expectMatch : false , testLine : '@otherTest' } ,
377
377
] ,
378
378
} ,
379
+ {
380
+ desc : 'simplify regexp' ,
381
+ GREP : '(.*)&((((.*))))' ,
382
+ regExpected : / .* / i,
383
+ cases : [ { expectMatch : true , testLine : '@test sdd @test2 dsd' } ] ,
384
+ } ,
385
+ {
386
+ desc : 'simplify regexp' ,
387
+ GREP : '((((((((((((((@V1&!@V2)&(.*)))&(.*)))&(.*)))&(@V1&!@V2)))&(.*)))&(.*))))' , // '(@V1&!@V2)&(@V1&!@V2)',
388
+ regExpected : / (? = .* (? = .* @ V 1 ) + ^ (? ! .* @ V 2 .* ) + ) + (? = .* (? = .* @ V 1 ) + ^ (? ! .* @ V 2 .* ) + ) + .* / i,
389
+ cases : [
390
+ { expectMatch : false , testLine : '@V1 sdd @V2 dsd' } ,
391
+ { expectMatch : false , testLine : '@V2 sdd @V1 dsd' } ,
392
+ { expectMatch : true , testLine : '@V1 sdd dsd' } ,
393
+ { expectMatch : false , testLine : '@V3 sdd dsd' } ,
394
+ ] ,
395
+ } ,
379
396
] )
380
397
. each ( t => t . cases )
381
398
// .only(t => t.id === '1')
@@ -395,4 +412,94 @@ describe('suite', () => {
395
412
}
396
413
} ) ;
397
414
} ) ;
415
+
416
+ it ( 'should simplify expression' , ( ) => {
417
+ const grep = '(.*)&((((.*))))' ;
418
+ const regActual = selectionTestGrep ( grep ) ;
419
+
420
+ expect ( regActual ) . toEqual ( / .* / i) ;
421
+ } ) ;
422
+
423
+ describe ( 'simplifyParentheses' , ( ) => {
424
+ it ( 'simplifyParentheses 1' , ( ) => {
425
+ const grep = '(.*)&((((.*))))' ;
426
+ expect ( simplifyParentheses ( grep ) ) . toEqual ( '(.*)' ) ;
427
+ } ) ;
428
+
429
+ it ( 'simplifyParentheses 2' , ( ) => {
430
+ const grep = '(.*)&((((.*))))&((((something))))' ;
431
+ expect ( simplifyParentheses ( grep ) ) . toEqual ( '(.*)&(something)' ) ;
432
+ } ) ;
433
+
434
+ it ( 'simplifyParentheses 3' , ( ) => {
435
+ const grep = '(.*)&((((.*))))&((((something))))' ;
436
+ expect ( simplifyParentheses ( grep ) ) . toEqual ( '(.*)&(something)' ) ;
437
+ } ) ;
438
+
439
+ it ( 'simplifyParentheses 4' , ( ) => {
440
+ const grep = '(.*)&((((.*))))&(((((A)&(B|(V&D))))))&((((something))))' ;
441
+ expect ( simplifyParentheses ( grep ) ) . toEqual ( '(.*)&(((((A)&(B|(V&D))))))&(something)' ) ;
442
+ } ) ;
443
+
444
+ it ( 'simplifyParentheses incorrect' , ( ) => {
445
+ const grep = '(.*)&((((.*))))&((((something)))))' ;
446
+ expect ( simplifyParentheses ( grep ) ) . toEqual ( '(.*)&(something))' ) ;
447
+ } ) ;
448
+ } ) ;
449
+
450
+ describe ( 'simplify expression' , ( ) => {
451
+ it ( 'should simplify expression 1' , ( ) => {
452
+ const grep = '(.*)&((((.*))))' ;
453
+ const regActual = selectionTestGrep ( grep ) ;
454
+
455
+ expect ( regActual ) . toEqual ( / .* / i) ;
456
+ } ) ;
457
+
458
+ it ( 'should simplify expression 2' , ( ) => {
459
+ const grep = '(A)&((((C))))' ;
460
+ const regActual = selectionTestGrep ( grep ) ;
461
+
462
+ expect ( regActual ) . toEqual ( / (? = .* A ) + (? = .* C ) + .* / i) ;
463
+ } ) ;
464
+
465
+ it ( 'should simplify expression - groups inside parenthesis' , ( ) => {
466
+ const grep = '(.*)&((((.*))))&(((((A)&(B|(V&D))))))&((((something))))' ;
467
+ const regActual = selectionTestGrep ( grep ) ;
468
+
469
+ expect ( regActual ) . toEqual ( / (? = .* ) + (? = .* (? = .* A ) + (? = .* ( B | (? = .* V ) + (? = .* D ) + ) ) + ) + (? = .* s o m e t h i n g ) + .* / i) ;
470
+ expect ( regActual . test ( 'A' ) ) . toEqual ( false ) ;
471
+ expect ( regActual . test ( 'A gogo B else something' ) ) . toEqual ( true ) ;
472
+ expect ( regActual . test ( 'B gogo A else something' ) ) . toEqual ( true ) ;
473
+ expect ( regActual . test ( 'V gogo A else something D' ) ) . toEqual ( true ) ;
474
+ expect ( regActual . test ( 'V gogo A else something' ) ) . toEqual ( false ) ;
475
+ expect ( regActual . test ( 'X' ) ) . toEqual ( false ) ;
476
+ } ) ;
477
+
478
+ it ( 'should simplify expression 4' , ( ) => {
479
+ const grep = '(.*)&((((.*))))&((((something))))' ;
480
+ const regActual = selectionTestGrep ( grep ) ;
481
+
482
+ expect ( regActual ) . toEqual ( / (? = .* ) + (? = .* s o m e t h i n g ) + .* / i) ;
483
+ } ) ;
484
+ } ) ;
485
+
486
+ describe ( 'convertOneGroup' , ( ) => {
487
+ it ( 'convertOneGroup 1' , ( ) => {
488
+ const grep = '.*' ;
489
+ const regActual = convertOneGroup ( grep , false ) ;
490
+ expect ( regActual ) . toEqual ( '.*' ) ;
491
+ } ) ;
492
+
493
+ it ( 'convertOneGroup 2 - or' , ( ) => {
494
+ const grep = 'A|C' ;
495
+ const regActual = convertOneGroup ( grep , false ) ;
496
+ expect ( regActual ) . toEqual ( '(A|C)' ) ;
497
+ } ) ;
498
+
499
+ it ( 'convertOneGroup 3 - and' , ( ) => {
500
+ const grep = 'A&C' ;
501
+ const regActual = convertOneGroup ( grep , false ) ;
502
+ expect ( regActual ) . toEqual ( '(?=.*A)+(?=.*C)+' ) ;
503
+ } ) ;
504
+ } ) ;
398
505
} ) ;
0 commit comments