aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-12-28 04:24:51 +0100
committerGitHub <[email protected]>2024-12-28 03:24:51 +0000
commit817f8a01d93378e00c03cf73154fcec34fd5feef (patch)
tree38f8ec48b1a5332b78c3a5611f07bd2d370651c5
parentad8b3604743bf4f1616752cf4f0a9af8278a4e25 (diff)
downloadfaker-817f8a01d93378e00c03cf73154fcec34fd5feef.tar.xz
faker-817f8a01d93378e00c03cf73154fcec34fd5feef.zip
fix: basic wildcard range handling + add more tests (#3322)
* fix: basic wirdcard range handling + add more tests * chore: simplify lower/upper case conversion
-rw-r--r--src/modules/helpers/index.ts14
-rw-r--r--test/modules/__snapshots__/helpers.spec.ts.snap12
-rw-r--r--test/modules/helpers.spec.ts138
3 files changed, 138 insertions, 26 deletions
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index 473927c3..6bb7e1ba 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -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].toLowerCase(), token[1].toUpperCase()],
+ 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);
}
diff --git a/test/modules/__snapshots__/helpers.spec.ts.snap b/test/modules/__snapshots__/helpers.spec.ts.snap
index 92801e06..44aa22a5 100644
--- a/test/modules/__snapshots__/helpers.spec.ts.snap
+++ b/test/modules/__snapshots__/helpers.spec.ts.snap
@@ -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!"`;
@@ -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`;
@@ -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!"`;
diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts
index 13c66f37..241e8f52 100644
--- a/test/modules/helpers.spec.ts
+++ b/test/modules/helpers.spec.ts
@@ -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', () => {
@@ -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);
});
});