diff options
| author | Pablo Ladreyt <[email protected]> | 2023-01-03 05:03:05 -0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-01-03 08:03:05 +0000 |
| commit | d3229fcdf28b5a1abdbc44a7bcdde934bd472bf2 (patch) | |
| tree | cf0ac5d86fc02f564f3369f821945c6b8f036e0b /test/number.spec.ts | |
| parent | 84b3c20c7d4459e820bbce65b64201b29283b149 (diff) | |
| download | faker-d3229fcdf28b5a1abdbc44a7bcdde934bd472bf2.tar.xz faker-d3229fcdf28b5a1abdbc44a7bcdde934bd472bf2.zip | |
feat(number): add binary and octal random number generation (#1708)
Diffstat (limited to 'test/number.spec.ts')
| -rw-r--r-- | test/number.spec.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/number.spec.ts b/test/number.spec.ts index f5480439..91208e00 100644 --- a/test/number.spec.ts +++ b/test/number.spec.ts @@ -11,6 +11,8 @@ describe('number', () => { seededTests(faker, 'number', (t) => { t.describeEach( 'int', + 'binary', + 'octal', 'hex' )((t) => { t.it('noArgs') @@ -240,6 +242,72 @@ describe('number', () => { }); }); + describe('binary', () => { + it('generates single binary character when no additional argument was provided', () => { + const binary = faker.number.binary(); + expect(binary).toBeTypeOf('string'); + expect(binary).toHaveLength(1); + expect(binary).toMatch(/^[01]$/); + }); + + it('generates a random binary string with a custom max value', () => { + const binary = faker.number.binary(5); + const binaryNum = parseInt(binary, 2); + expect(binaryNum).toBeLessThanOrEqual(5); + expect(binary).toMatch(/^[01]+$/); + }); + + it('generates a random binary in a specific range', () => { + const binary = faker.number.binary({ min: 15, max: 255 }); + + const binaryNum = parseInt(binary, 2); + expect(binaryNum).toBeLessThanOrEqual(255); + expect(binaryNum).greaterThanOrEqual(15); + }); + + it('should throw when min > max', () => { + const min = 10; + const max = 9; + + expect(() => { + faker.number.binary({ min, max }); + }).toThrowError(`Max ${max} should be greater than min ${min}.`); + }); + }); + + describe('octal', () => { + it('generates single octal character when no additional argument was provided', () => { + const octal = faker.number.octal(); + expect(octal).toBeTypeOf('string'); + expect(octal).toHaveLength(1); + expect(octal).toMatch(/^[0-7]$/); + }); + + it('generates a random octal string with a custom max value', () => { + const octal = faker.number.octal(5); + const octalNum = parseInt(octal, 8); + expect(octalNum).toBeLessThanOrEqual(5); + expect(octal).toMatch(/^[0-7]+$/); + }); + + it('generates a random octal in a specific range', () => { + const octal = faker.number.octal({ min: 15, max: 255 }); + + const octalNum = parseInt(octal, 8); + expect(octalNum).toBeLessThanOrEqual(255); + expect(octalNum).greaterThanOrEqual(15); + }); + + it('should throw when min > max', () => { + const min = 10; + const max = 9; + + expect(() => { + faker.number.octal({ min, max }); + }).toThrowError(`Max ${max} should be greater than min ${min}.`); + }); + }); + describe('hex', () => { it('generates single hex character when no additional argument was provided', () => { const hex = faker.number.hex(); |
