Skip to content

Commit 817f8a0

Browse files
authoredDec 28, 2024··
fix: basic wildcard range handling + add more tests (#3322)
* fix: basic wirdcard range handling + add more tests * chore: simplify lower/upper case conversion
1 parent ad8b360 commit 817f8a0

File tree

3 files changed

+138
-26
lines changed

3 files changed

+138
-26
lines changed
 

‎src/modules/helpers/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,21 @@ export class SimpleHelpersModule extends SimpleModuleBase {
394394
quantifierMax
395395
);
396396

397+
let replacement: string;
398+
if (token[1] === '.') {
399+
replacement = this.faker.string.alphanumeric(repetitions);
400+
} else if (isCaseInsensitive) {
401+
replacement = this.faker.string.fromCharacters(
402+
[token[1].toLowerCase(), token[1].toUpperCase()],
403+
repetitions
404+
);
405+
} else {
406+
replacement = token[1].repeat(repetitions);
407+
}
408+
397409
pattern =
398410
pattern.slice(0, token.index) +
399-
token[1].repeat(repetitions) +
411+
replacement +
400412
pattern.slice(token.index + token[0].length);
401413
token = SINGLE_CHAR_REG.exec(pattern);
402414
}

‎test/modules/__snapshots__/helpers.spec.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ exports[`helpers > 42 > fromRegExp > with static string 1`] = `"Hello World!"`;
7171

7272
exports[`helpers > 42 > fromRegExp > with wildcard character 1`] = `"."`;
7373

74-
exports[`helpers > 42 > fromRegExp > with wildcard character and min max quantifier 1`] = `".."`;
74+
exports[`helpers > 42 > fromRegExp > with wildcard character and min max quantifier 1`] = `"WJ"`;
7575

76-
exports[`helpers > 42 > fromRegExp > with wildcard character and quantifier 1`] = `"..."`;
76+
exports[`helpers > 42 > fromRegExp > with wildcard character and quantifier 1`] = `"nWJ"`;
7777

7878
exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`;
7979

@@ -293,9 +293,9 @@ exports[`helpers > 1211 > fromRegExp > with static string 1`] = `"Hello World!"`
293293

294294
exports[`helpers > 1211 > fromRegExp > with wildcard character 1`] = `"."`;
295295

296-
exports[`helpers > 1211 > fromRegExp > with wildcard character and min max quantifier 1`] = `"....."`;
296+
exports[`helpers > 1211 > fromRegExp > with wildcard character and min max quantifier 1`] = `"TdZFG"`;
297297

298-
exports[`helpers > 1211 > fromRegExp > with wildcard character and quantifier 1`] = `"..."`;
298+
exports[`helpers > 1211 > fromRegExp > with wildcard character and quantifier 1`] = `"VTd"`;
299299

300300
exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`;
301301

@@ -510,9 +510,9 @@ exports[`helpers > 1337 > fromRegExp > with static string 1`] = `"Hello World!"`
510510
511511
exports[`helpers > 1337 > fromRegExp > with wildcard character 1`] = `"."`;
512512
513-
exports[`helpers > 1337 > fromRegExp > with wildcard character and min max quantifier 1`] = `".."`;
513+
exports[`helpers > 1337 > fromRegExp > with wildcard character and min max quantifier 1`] = `"9h"`;
514514
515-
exports[`helpers > 1337 > fromRegExp > with wildcard character and quantifier 1`] = `"..."`;
515+
exports[`helpers > 1337 > fromRegExp > with wildcard character and quantifier 1`] = `"g9h"`;
516516
517517
exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`;
518518

‎test/modules/helpers.spec.ts

+119-19
Original file line numberDiff line numberDiff line change
@@ -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(/^Hello[0-9]$/);
658+
const actual = faker.helpers.fromRegExp('Hello[0-9]');
659+
expect(actual).toMatch(/^Hello[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(/^Test#{5}%{2,5}Testing*[1-5]{10}END$/);
666+
expect(actual).toMatch(/^Test#{5}%{2,5}Testing*[1-5]{10}END$/);
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-D0-9]{4}-[A-D0-9]{4}/);
581-
expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/);
678+
const actual = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/);
679+
expect(actual).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/);
582680
});
583681

584682
it('doesnt include negated characters', () => {
585-
const string = faker.helpers.fromRegExp(/[^a-t0-9]{4}/i);
586-
expect(string).toMatch(/[^a-t0-9]{4}/);
683+
const actual = faker.helpers.fromRegExp(/[^a-t0-9]{4}/i);
684+
expect(actual).toHaveLength(4);
685+
expect(actual).toMatch(/[^a-t0-9]{4}/);
587686
});
588687

589688
it('handles case insensitive flags', () => {
590-
const string = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/i);
591-
expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i);
689+
const actual = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/i);
690+
expect(actual).toHaveLength(9);
691+
expect(actual).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i);
592692
});
593693
});
594694

0 commit comments

Comments
 (0)
Please sign in to comment.