diff options
| author | ST-DDT <[email protected]> | 2023-01-11 19:56:25 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-01-11 18:56:25 +0000 |
| commit | 47b2cfc76b790647e398bf9883368a10b2ff5a68 (patch) | |
| tree | a7bf7f0146915258c2a18c1a825e513cb0c60291 /test/string.spec.ts | |
| parent | f4615e1e05a378dd5ae304543185dc7afd0fb7d8 (diff) | |
| download | faker-47b2cfc76b790647e398bf9883368a10b2ff5a68.tar.xz faker-47b2cfc76b790647e398bf9883368a10b2ff5a68.zip | |
feat(string): generate string from characters (#1717)
Diffstat (limited to 'test/string.spec.ts')
| -rw-r--r-- | test/string.spec.ts | 108 |
1 files changed, 100 insertions, 8 deletions
diff --git a/test/string.spec.ts b/test/string.spec.ts index 1ad429ca..5c256a32 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -7,6 +7,21 @@ const NON_SEEDED_BASED_RUN = 5; describe('string', () => { seededTests(faker, 'string', (t) => { + t.describe('fromCharacters', (t) => { + t.it('with string characters', 'foobar') + .it('with string[] characters', 'foobar'.split('')) + .it('with string characters and length', 'foobar', 5) + .it('with string[] characters and length', 'foobar'.split(''), 5) + .it('with string characters and length range', 'foobar', { + min: 10, + max: 20, + }) + .it('with string[] characters and length range', 'foobar'.split(''), { + min: 10, + max: 20, + }); + }); + t.describe('alpha', (t) => { t.it('noArgs') .itRepeated('with length parameter', 5, 5) @@ -113,6 +128,64 @@ describe('string', () => { describe(`random seeded tests for seed ${faker.seed()}`, () => { for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('fromCharacters', () => { + it('should return single character when no length provided', () => { + const actual = faker.string.fromCharacters('foobar'); + + expect(actual).toHaveLength(1); + }); + + it('should only contain characters from provided string', () => { + const actual = faker.string.fromCharacters('foobar'); + + expect(actual).toMatch(/^[foobar]$/); + }); + + it('should generate 5 random letters', () => { + const actual = faker.string.fromCharacters('foobar', 5); + + expect(actual).toHaveLength(5); + }); + + it.each([0, -1, -100])( + 'should return empty string when length is <= 0', + (length) => { + const actual = faker.string.fromCharacters('foobar', length); + + expect(actual).toBe(''); + } + ); + + it('should return a random amount of characters', () => { + const actual = faker.string.fromCharacters('foobar', { + min: 10, + max: 20, + }); + + expect(actual).toBeTruthy(); + expect(actual).toBeTypeOf('string'); + + expect(actual.length).toBeGreaterThanOrEqual(10); + expect(actual.length).toBeLessThanOrEqual(20); + }); + + it('should throw if no characters are passed (string)', () => { + expect(() => faker.string.fromCharacters('')).toThrowError( + new FakerError( + 'Unable to generate string: No characters to select from.' + ) + ); + }); + + it('should throw if no characters are passed (string[])', () => { + expect(() => faker.string.fromCharacters([])).toThrowError( + new FakerError( + 'Unable to generate string: No characters to select from.' + ) + ); + }); + }); + describe('alpha', () => { it('should return single letter when no length provided', () => { const actual = faker.string.alpha(); @@ -193,7 +266,22 @@ describe('string', () => { expect(alphaText).toMatch(/^[b-oq-z]{5}$/); }); - it('should throw if all possible characters being excluded', () => { + it('should throw if all possible characters being excluded (string)', () => { + const exclude = 'abcdefghijklmnopqrstuvwxyz'; + expect(() => + faker.string.alpha({ + length: 5, + casing: 'lower', + exclude, + }) + ).toThrowError( + new FakerError( + 'Unable to generate string: No characters to select from.' + ) + ); + }); + + it('should throw if all possible characters being excluded (string[])', () => { const exclude = 'abcdefghijklmnopqrstuvwxyz'.split(''); expect(() => faker.string.alpha({ @@ -203,7 +291,7 @@ describe('string', () => { }) ).toThrowError( new FakerError( - 'Unable to generate string, because all possible characters are excluded.' + 'Unable to generate string: No characters to select from.' ) ); }); @@ -332,8 +420,8 @@ describe('string', () => { expect(alphaText).toMatch(/^[0-9b-oq-z]{5}$/); }); - it('should throw if all possible characters being excluded', () => { - const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); + it('should throw if all possible characters being excluded (string)', () => { + const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789'; expect(() => faker.string.alphanumeric({ length: 5, @@ -342,20 +430,24 @@ describe('string', () => { }) ).toThrowError( new FakerError( - 'Unable to generate string, because all possible characters are excluded.' + 'Unable to generate string: No characters to select from.' ) ); }); - it('should throw if all possible characters being excluded via string', () => { - const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789'; + it('should throw if all possible characters being excluded (string[])', () => { + const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); expect(() => faker.string.alphanumeric({ length: 5, casing: 'lower', exclude, }) - ).toThrowError(); + ).toThrowError( + new FakerError( + 'Unable to generate string: No characters to select from.' + ) + ); }); it('should not mutate the input object', () => { |
