Skip to content

Commit

Permalink
fix: basic wirdcard range handling + add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Dec 3, 2024
1 parent 1bfa1fa commit 8509c66
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 26 deletions.
14 changes: 13 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,21 @@ export class SimpleHelpersModule extends SimpleModuleBase {
quantifierMax
);

let replacement: string;
if (token[1] === '.') {
replacement = this.faker.string.alphanumeric(repetitions);
} else if (isCaseInsensitive) {
replacement = this.faker.string.fromCharacters(
[token[1].toLocaleLowerCase(), token[1].toLocaleUpperCase()],
repetitions
);
} else {
replacement = token[1].repeat(repetitions);
}

pattern =
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
replacement +
pattern.slice(token.index + token[0].length);
token = SINGLE_CHAR_REG.exec(pattern);
}
Expand Down
12 changes: 6 additions & 6 deletions test/modules/__snapshots__/helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ exports[`helpers > 42 > fromRegExp > with static string 1`] = `"Hello World!"`;

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -510,9 +510,9 @@ exports[`helpers > 1337 > fromRegExp > with static string 1`] = `"Hello World!"`
exports[`helpers > 1337 > fromRegExp > with wildcard character 1`] = `"."`;
exports[`helpers > 1337 > fromRegExp > with wildcard character and min max quantifier 1`] = `".."`;
exports[`helpers > 1337 > fromRegExp > with wildcard character and min max quantifier 1`] = `"9h"`;
exports[`helpers > 1337 > fromRegExp > with wildcard character and quantifier 1`] = `"..."`;
exports[`helpers > 1337 > fromRegExp > with wildcard character and quantifier 1`] = `"g9h"`;
exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`;
Expand Down
138 changes: 119 additions & 19 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,29 +543,127 @@ describe('helpers', () => {
});

describe('fromRegExp()', () => {
it('deals with range repeat', () => {
const string = faker.helpers.fromRegExp(/#{5,10}/);
expect(string.length).toBeLessThanOrEqual(10);
expect(string.length).toBeGreaterThanOrEqual(5);
expect(string).toMatch(/^#{5,10}$/);
describe('single character patterns', () => {
it('handles case sensitive characters', () => {
const actual = faker.helpers.fromRegExp(/w/);
expect(actual).toHaveLength(1);
expect(actual).not.toContain('W');
expect(actual).toBe('w');
expect(actual).toMatch(/^w$/);
});

it.skip('handles case insensitive characters', () => {
const set = new Set<string>();
for (let i = 0; i < 100; i++) {
const actual = faker.helpers.fromRegExp(/w/i);
expect(actual).toHaveLength(1);
expect(actual).toMatch(/^W$/i);
set.add(actual);
}

expect(set.size).toBe(2);
});

it('handles case insensitive symbols', () => {
const actual = faker.helpers.fromRegExp(/%/i);
expect(actual).toHaveLength(1);
expect(actual).toBe('%');
expect(actual).toMatch(/^%$/i);
});

it.skip('handles the wildcard character', () => {
const set = new Set<string>();
for (let i = 0; i < 100; i++) {
const actual = faker.helpers.fromRegExp(/./);
expect(actual).toHaveLength(1);
expect(actual).toMatch(/^.$/);
set.add(actual);
}

expect(set.size).toBeGreaterThan(5);
});
});

describe('fixed length patterns', () => {
it('handles case sensitive characters', () => {
const actual = faker.helpers.fromRegExp(/w{100}/);
expect(actual).toHaveLength(100);
expect(actual).not.toContain('W');
expect(actual).toContain('w');
expect(actual).toBe('w'.repeat(100));
expect(actual).toMatch(/^w{100}$/);
});

it('handles case insensitive characters', () => {
const actual = faker.helpers.fromRegExp(/w{100}/i);
expect(actual).toHaveLength(100);
expect(actual).toContain('W');
expect(actual).toContain('w');
expect(actual).toMatch(/^W{100}$/i);
});

it('handles case insensitive symbols', () => {
const actual = faker.helpers.fromRegExp(/%{100}/i);
expect(actual).toHaveLength(100);
expect(actual).toBe('%'.repeat(100));
expect(actual).toMatch(/^%{100}$/);
});

it('handles the wildcard character', () => {
const actual = faker.helpers.fromRegExp(/.{100}/);
expect(actual).toHaveLength(100);
expect(actual).toMatch(/^.{100}$/);
const set = new Set(actual);
expect(set.size).toBeGreaterThan(5);
});
});

it('repeats string {n} number of times', () => {
expect(faker.helpers.fromRegExp('%{10}')).toBe('%'.repeat(10));
expect(faker.helpers.fromRegExp('%{30}')).toBe('%'.repeat(30));
expect(faker.helpers.fromRegExp('%{5}')).toBe('%'.repeat(5));
describe('length range patterns', () => {
it('handles case sensitive characters', () => {
const actual = faker.helpers.fromRegExp(/w{5,10}/);
expect(actual.length).toBeGreaterThanOrEqual(5);
expect(actual.length).toBeLessThanOrEqual(10);
expect(actual).not.toContain('W');
expect(actual).toContain('w');
expect(actual).toMatch(/^w{5,10}$/);
});

it('handles case insensitive characters', () => {
const actual = faker.helpers.fromRegExp(/w{50,100}/i);
expect(actual.length).toBeGreaterThanOrEqual(50);
expect(actual.length).toBeLessThanOrEqual(100);
expect(actual).toContain('W');
expect(actual).toContain('w');
expect(actual).toMatch(/^W{50,100}$/i);
});

it('handles case insensitive symbols', () => {
const actual = faker.helpers.fromRegExp(/%{50,100}/i);
expect(actual.length).toBeGreaterThanOrEqual(50);
expect(actual.length).toBeLessThanOrEqual(100);
expect(actual).toMatch(/^%{50,100}$/);
});

it('handles the wildcard character', () => {
const actual = faker.helpers.fromRegExp(/.{50,100}/);
expect(actual.length).toBeGreaterThanOrEqual(50);
expect(actual.length).toBeLessThanOrEqual(100);
expect(actual).toMatch(/^.{50,100}$/);
const set = new Set(actual);
expect(set.size).toBeGreaterThan(5);
});
});

it('creates a numerical range', () => {
const string = faker.helpers.fromRegExp('Hello[0-9]');
expect(string).toMatch(/^Hello[0-9]$/);
const actual = faker.helpers.fromRegExp('Hello[0-9]');
expect(actual).toMatch(/^Hello[0-9]$/);
});

it('deals with multiple tokens in one string', () => {
const string = faker.helpers.fromRegExp(
const actual = faker.helpers.fromRegExp(
'Test#{5}%{2,5}Testing*[1-5]{10}END'
);
expect(string).toMatch(/^Test#{5}%{2,5}Testing*[1-5]{10}END$/);
expect(actual).toMatch(/^Test#{5}%{2,5}Testing*[1-5]{10}END$/);
});

it('throws error when min > max outside set', () => {
Expand All @@ -577,18 +675,20 @@ describe('helpers', () => {
});

it('deals with RegExp object', () => {
const string = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/);
expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/);
const actual = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/);
expect(actual).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/);
});

it('doesnt include negated characters', () => {
const string = faker.helpers.fromRegExp(/[^a-t0-9]{4}/i);
expect(string).toMatch(/[^a-t0-9]{4}/);
const actual = faker.helpers.fromRegExp(/[^a-t0-9]{4}/i);
expect(actual).toHaveLength(4);
expect(actual).toMatch(/[^a-t0-9]{4}/);
});

it('handles case insensitive flags', () => {
const string = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/i);
expect(string).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i);
const actual = faker.helpers.fromRegExp(/[A-D0-9]{4}-[A-D0-9]{4}/i);
expect(actual).toHaveLength(9);
expect(actual).toMatch(/^[A-D0-9]{4}-[A-D0-9]{4}$/i);
});
});

Expand Down

0 comments on commit 8509c66

Please sign in to comment.