@@ -543,29 +543,127 @@ describe('helpers', () => {
543543 } ) ;
544544
545545 describe ( 'fromRegExp()' , ( ) => {
546- it ( 'deals with range repeat' , ( ) => {
547- const string = faker . helpers . fromRegExp ( / # { 5 , 10 } / ) ;
548- expect ( string . length ) . toBeLessThanOrEqual ( 10 ) ;
549- expect ( string . length ) . toBeGreaterThanOrEqual ( 5 ) ;
550- expect ( string ) . toMatch ( / ^ # { 5 , 10 } $ / ) ;
546+ describe ( 'single character patterns' , ( ) => {
547+ it ( 'handles case sensitive characters' , ( ) => {
548+ const actual = faker . helpers . fromRegExp ( / w / ) ;
549+ expect ( actual ) . toHaveLength ( 1 ) ;
550+ expect ( actual ) . not . toContain ( 'W' ) ;
551+ expect ( actual ) . toBe ( 'w' ) ;
552+ expect ( actual ) . toMatch ( / ^ w $ / ) ;
553+ } ) ;
554+
555+ it . skip ( 'handles case insensitive characters' , ( ) => {
556+ const set = new Set < string > ( ) ;
557+ for ( let i = 0 ; i < 100 ; i ++ ) {
558+ const actual = faker . helpers . fromRegExp ( / w / i) ;
559+ expect ( actual ) . toHaveLength ( 1 ) ;
560+ expect ( actual ) . toMatch ( / ^ W $ / i) ;
561+ set . add ( actual ) ;
562+ }
563+
564+ expect ( set . size ) . toBe ( 2 ) ;
565+ } ) ;
566+
567+ it ( 'handles case insensitive symbols' , ( ) => {
568+ const actual = faker . helpers . fromRegExp ( / % / i) ;
569+ expect ( actual ) . toHaveLength ( 1 ) ;
570+ expect ( actual ) . toBe ( '%' ) ;
571+ expect ( actual ) . toMatch ( / ^ % $ / i) ;
572+ } ) ;
573+
574+ it . skip ( 'handles the wildcard character' , ( ) => {
575+ const set = new Set < string > ( ) ;
576+ for ( let i = 0 ; i < 100 ; i ++ ) {
577+ const actual = faker . helpers . fromRegExp ( / ./ ) ;
578+ expect ( actual ) . toHaveLength ( 1 ) ;
579+ expect ( actual ) . toMatch ( / ^ .$ / ) ;
580+ set . add ( actual ) ;
581+ }
582+
583+ expect ( set . size ) . toBeGreaterThan ( 5 ) ;
584+ } ) ;
585+ } ) ;
586+
587+ describe ( 'fixed length patterns' , ( ) => {
588+ it ( 'handles case sensitive characters' , ( ) => {
589+ const actual = faker . helpers . fromRegExp ( / w { 100 } / ) ;
590+ expect ( actual ) . toHaveLength ( 100 ) ;
591+ expect ( actual ) . not . toContain ( 'W' ) ;
592+ expect ( actual ) . toContain ( 'w' ) ;
593+ expect ( actual ) . toBe ( 'w' . repeat ( 100 ) ) ;
594+ expect ( actual ) . toMatch ( / ^ w { 100 } $ / ) ;
595+ } ) ;
596+
597+ it ( 'handles case insensitive characters' , ( ) => {
598+ const actual = faker . helpers . fromRegExp ( / w { 100 } / i) ;
599+ expect ( actual ) . toHaveLength ( 100 ) ;
600+ expect ( actual ) . toContain ( 'W' ) ;
601+ expect ( actual ) . toContain ( 'w' ) ;
602+ expect ( actual ) . toMatch ( / ^ W { 100 } $ / i) ;
603+ } ) ;
604+
605+ it ( 'handles case insensitive symbols' , ( ) => {
606+ const actual = faker . helpers . fromRegExp ( / % { 100 } / i) ;
607+ expect ( actual ) . toHaveLength ( 100 ) ;
608+ expect ( actual ) . toBe ( '%' . repeat ( 100 ) ) ;
609+ expect ( actual ) . toMatch ( / ^ % { 100 } $ / ) ;
610+ } ) ;
611+
612+ it ( 'handles the wildcard character' , ( ) => {
613+ const actual = faker . helpers . fromRegExp ( / .{ 100 } / ) ;
614+ expect ( actual ) . toHaveLength ( 100 ) ;
615+ expect ( actual ) . toMatch ( / ^ .{ 100 } $ / ) ;
616+ const set = new Set ( actual ) ;
617+ expect ( set . size ) . toBeGreaterThan ( 5 ) ;
618+ } ) ;
551619 } ) ;
552620
553- it ( 'repeats string {n} number of times' , ( ) => {
554- expect ( faker . helpers . fromRegExp ( '%{10}' ) ) . toBe ( '%' . repeat ( 10 ) ) ;
555- expect ( faker . helpers . fromRegExp ( '%{30}' ) ) . toBe ( '%' . repeat ( 30 ) ) ;
556- expect ( faker . helpers . fromRegExp ( '%{5}' ) ) . toBe ( '%' . repeat ( 5 ) ) ;
621+ describe ( 'length range patterns' , ( ) => {
622+ it ( 'handles case sensitive characters' , ( ) => {
623+ const actual = faker . helpers . fromRegExp ( / w { 5 , 10 } / ) ;
624+ expect ( actual . length ) . toBeGreaterThanOrEqual ( 5 ) ;
625+ expect ( actual . length ) . toBeLessThanOrEqual ( 10 ) ;
626+ expect ( actual ) . not . toContain ( 'W' ) ;
627+ expect ( actual ) . toContain ( 'w' ) ;
628+ expect ( actual ) . toMatch ( / ^ w { 5 , 10 } $ / ) ;
629+ } ) ;
630+
631+ it ( 'handles case insensitive characters' , ( ) => {
632+ const actual = faker . helpers . fromRegExp ( / w { 50 , 100 } / i) ;
633+ expect ( actual . length ) . toBeGreaterThanOrEqual ( 50 ) ;
634+ expect ( actual . length ) . toBeLessThanOrEqual ( 100 ) ;
635+ expect ( actual ) . toContain ( 'W' ) ;
636+ expect ( actual ) . toContain ( 'w' ) ;
637+ expect ( actual ) . toMatch ( / ^ W { 50 , 100 } $ / i) ;
638+ } ) ;
639+
640+ it ( 'handles case insensitive symbols' , ( ) => {
641+ const actual = faker . helpers . fromRegExp ( / % { 50 , 100 } / i) ;
642+ expect ( actual . length ) . toBeGreaterThanOrEqual ( 50 ) ;
643+ expect ( actual . length ) . toBeLessThanOrEqual ( 100 ) ;
644+ expect ( actual ) . toMatch ( / ^ % { 50 , 100 } $ / ) ;
645+ } ) ;
646+
647+ it ( 'handles the wildcard character' , ( ) => {
648+ const actual = faker . helpers . fromRegExp ( / .{ 50 , 100 } / ) ;
649+ expect ( actual . length ) . toBeGreaterThanOrEqual ( 50 ) ;
650+ expect ( actual . length ) . toBeLessThanOrEqual ( 100 ) ;
651+ expect ( actual ) . toMatch ( / ^ .{ 50 , 100 } $ / ) ;
652+ const set = new Set ( actual ) ;
653+ expect ( set . size ) . toBeGreaterThan ( 5 ) ;
654+ } ) ;
557655 } ) ;
558656
559657 it ( 'creates a numerical range' , ( ) => {
560- const string = faker . helpers . fromRegExp ( 'Hello[0-9]' ) ;
561- expect ( string ) . toMatch ( / ^ H e l l o [ 0 - 9 ] $ / ) ;
658+ const actual = faker . helpers . fromRegExp ( 'Hello[0-9]' ) ;
659+ expect ( actual ) . toMatch ( / ^ H e l l o [ 0 - 9 ] $ / ) ;
562660 } ) ;
563661
564662 it ( 'deals with multiple tokens in one string' , ( ) => {
565- const string = faker . helpers . fromRegExp (
663+ const actual = faker . helpers . fromRegExp (
566664 'Test#{5}%{2,5}Testing*[1-5]{10}END'
567665 ) ;
568- expect ( string ) . toMatch ( / ^ T e s t # { 5 } % { 2 , 5 } T e s t i n g * [ 1 - 5 ] { 10 } E N D $ / ) ;
666+ expect ( actual ) . toMatch ( / ^ T e s t # { 5 } % { 2 , 5 } T e s t i n g * [ 1 - 5 ] { 10 } E N D $ / ) ;
569667 } ) ;
570668
571669 it ( 'throws error when min > max outside set' , ( ) => {
@@ -577,18 +675,20 @@ describe('helpers', () => {
577675 } ) ;
578676
579677 it ( 'deals with RegExp object' , ( ) => {
580- const string = faker . helpers . fromRegExp ( / [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } / ) ;
581- expect ( string ) . toMatch ( / ^ [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } $ / ) ;
678+ const actual = faker . helpers . fromRegExp ( / [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } / ) ;
679+ expect ( actual ) . toMatch ( / ^ [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } $ / ) ;
582680 } ) ;
583681
584682 it ( 'doesnt include negated characters' , ( ) => {
585- const string = faker . helpers . fromRegExp ( / [ ^ a - t 0 - 9 ] { 4 } / i) ;
586- expect ( string ) . toMatch ( / [ ^ a - t 0 - 9 ] { 4 } / ) ;
683+ const actual = faker . helpers . fromRegExp ( / [ ^ a - t 0 - 9 ] { 4 } / i) ;
684+ expect ( actual ) . toHaveLength ( 4 ) ;
685+ expect ( actual ) . toMatch ( / [ ^ a - t 0 - 9 ] { 4 } / ) ;
587686 } ) ;
588687
589688 it ( 'handles case insensitive flags' , ( ) => {
590- const string = faker . helpers . fromRegExp ( / [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } / i) ;
591- expect ( string ) . toMatch ( / ^ [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } $ / i) ;
689+ const actual = faker . helpers . fromRegExp ( / [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } / i) ;
690+ expect ( actual ) . toHaveLength ( 9 ) ;
691+ expect ( actual ) . toMatch ( / ^ [ A - D 0 - 9 ] { 4 } - [ A - D 0 - 9 ] { 4 } $ / i) ;
592692 } ) ;
593693 } ) ;
594694
0 commit comments