diff options
| author | Shinigami <[email protected]> | 2022-05-21 16:30:33 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-21 14:30:33 +0000 |
| commit | 4c0e41831f8d2fad92f85cea647cbd0873fd842e (patch) | |
| tree | ea01cb901167f9c8b09a4ed463665c71a14bad47 /test | |
| parent | 5af79f487bd1537676d017ae09563e53d18458c4 (diff) | |
| download | faker-4c0e41831f8d2fad92f85cea647cbd0873fd842e.tar.xz faker-4c0e41831f8d2fad92f85cea647cbd0873fd842e.zip | |
feat: add casing option (#955)
Co-authored-by: Eric Cheng <[email protected]>
Diffstat (limited to 'test')
| -rw-r--r-- | test/random.spec.ts | 61 |
1 files changed, 55 insertions, 6 deletions
diff --git a/test/random.spec.ts b/test/random.spec.ts index d6fe3da7..3ff24ff7 100644 --- a/test/random.spec.ts +++ b/test/random.spec.ts @@ -179,9 +179,13 @@ describe('random', () => { expect(actual).toMatch(/^[a-z]$/); }); - it('should return uppercase when upcase option is true', () => { - const actual = faker.random.alpha({ upcase: true }); - expect(actual).toMatch(/^[A-Z]$/); + it.each([ + ['upper', /^[A-Z]{250}$/], + ['lower', /^[a-z]{250}$/], + ['mixed', /^[a-zA-Z]{250}$/], + ] as const)('should return %s-case', (casing, pattern) => { + const actual = faker.random.alpha({ count: 250, casing }); + expect(actual).toMatch(pattern); }); it('should generate many random letters', () => { @@ -190,6 +194,15 @@ describe('random', () => { expect(actual).toHaveLength(5); }); + it.each([0, -1, -100])( + 'should return empty string when length is <= 0', + (length) => { + const actual = faker.random.alpha(length); + + expect(actual).toBe(''); + } + ); + it('should be able to ban some characters', () => { const actual = faker.random.alpha({ count: 5, @@ -210,14 +223,28 @@ describe('random', () => { expect(alphaText).toMatch(/^[b-oq-z]{5}$/); }); + it('should throw if all possible characters being banned', () => { + const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); + expect(() => + faker.random.alpha({ + count: 5, + bannedChars, + }) + ).toThrowError( + new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ) + ); + }); + it('should not mutate the input object', () => { const input: { count: number; - upcase: boolean; + casing: 'mixed'; bannedChars: string[]; } = Object.freeze({ count: 5, - upcase: true, + casing: 'mixed', bannedChars: ['a', '%'], }); @@ -233,12 +260,30 @@ describe('random', () => { expect(actual).toHaveLength(1); }); + it.each([ + ['upper', /^[A-Z0-9]{250}$/], + ['lower', /^[a-z0-9]{250}$/], + ['mixed', /^[a-zA-Z0-9]{250}$/], + ] as const)('should return %s-case', (casing, pattern) => { + const actual = faker.random.alphaNumeric(250, { casing }); + expect(actual).toMatch(pattern); + }); + it('should generate many random characters', () => { const actual = faker.random.alphaNumeric(5); expect(actual).toHaveLength(5); }); + it.each([0, -1, -100])( + 'should return empty string when length is <= 0', + (length) => { + const actual = faker.random.alphaNumeric(length); + + expect(actual).toBe(''); + } + ); + it('should be able to ban all alphabetic characters', () => { const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split(''); const alphaText = faker.random.alphaNumeric(5, { @@ -278,7 +323,11 @@ describe('random', () => { faker.random.alphaNumeric(5, { bannedChars, }) - ).toThrowError(); + ).toThrowError( + new FakerError( + 'Unable to generate string, because all possible characters are banned.' + ) + ); }); it('should not mutate the input object', () => { |
