diff options
| author | DivisionByZero <[email protected]> | 2023-12-30 13:19:29 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-12-30 12:19:29 +0000 |
| commit | 87e0490978ae4fd2a7ca9aab550a475add513ef6 (patch) | |
| tree | 84922009f0d5eb8e998c2bb72698018e049eba07 /test/modules | |
| parent | c10899b638ae36c09833d4de8d79dd3958d58e5a (diff) | |
| download | faker-87e0490978ae4fd2a7ca9aab550a475add513ef6.tar.xz faker-87e0490978ae4fd2a7ca9aab550a475add513ef6.zip | |
refactor(number): deprecate precision in favor of multipleOf in float (#2564)
Diffstat (limited to 'test/modules')
| -rw-r--r-- | test/modules/__snapshots__/number.spec.ts.snap | 6 | ||||
| -rw-r--r-- | test/modules/datatype.spec.ts | 8 | ||||
| -rw-r--r-- | test/modules/number.spec.ts | 37 |
3 files changed, 47 insertions, 4 deletions
diff --git a/test/modules/__snapshots__/number.spec.ts.snap b/test/modules/__snapshots__/number.spec.ts.snap index 3d10ddb4..c04b4fe5 100644 --- a/test/modules/__snapshots__/number.spec.ts.snap +++ b/test/modules/__snapshots__/number.spec.ts.snap @@ -26,6 +26,8 @@ exports[`number > 42 > float > with min 1`] = `-25.894775084685534`; exports[`number > 42 > float > with min and max 1`] = `-0.4260473116301`; +exports[`number > 42 > float > with min, max and multipleOf 1`] = `-0.4261`; + exports[`number > 42 > float > with min, max and precision 1`] = `-0.4261`; exports[`number > 42 > float > with plain number 1`] = `1.498160457238555`; @@ -74,6 +76,8 @@ exports[`number > 1211 > float > with min 1`] = `-2.073633389081806`; exports[`number > 1211 > float > with min and max 1`] = `61.06573706539348`; +exports[`number > 1211 > float > with min, max and multipleOf 1`] = `61.0658`; + exports[`number > 1211 > float > with min, max and precision 1`] = `61.0658`; exports[`number > 1211 > float > with plain number 1`] = `3.7140806149691343`; @@ -122,6 +126,8 @@ exports[`number > 1337 > float > with min 1`] = `-30.732938923640177`; exports[`number > 1337 > float > with min and max 1`] = `-12.915260942419991`; +exports[`number > 1337 > float > with min, max and multipleOf 1`] = `-12.9153`; + exports[`number > 1337 > float > with min, max and precision 1`] = `-12.9153`; exports[`number > 1337 > float > with plain number 1`] = `1.048098704777658`; diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts index 3d65797d..75f2897c 100644 --- a/test/modules/datatype.spec.ts +++ b/test/modules/datatype.spec.ts @@ -236,7 +236,9 @@ describe('datatype', () => { it('should throw when precision is negative', () => { expect(() => { faker.datatype.number({ precision: -0.01 }); - }).toThrow(new FakerError('Precision should be greater than 0.')); + }).toThrow( + new FakerError('multipleOf/precision should be greater than 0.') + ); }); }); @@ -327,7 +329,9 @@ describe('datatype', () => { it('should throw when precision is negative', () => { expect(() => { faker.datatype.float({ precision: -0.01 }); - }).toThrow(new FakerError('Precision should be greater than 0.')); + }).toThrow( + new FakerError('multipleOf/precision should be greater than 0.') + ); }); }); diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index 81a1e992..c61498fa 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -27,6 +27,11 @@ describe('number', () => { min: -42, max: 69, precision: 0.0001, + }) + .it('with min, max and multipleOf', { + min: -42, + max: 69, + multipleOf: 0.0001, }); }); @@ -244,6 +249,22 @@ describe('number', () => { expect(results).toEqual([0, 0.5, 1, 1.5]); }); + it('provides numbers with a given multipleOf of 0.5 steps', () => { + const results = [ + ...new Set( + Array.from({ length: 50 }, () => + faker.number.float({ + min: 0, + max: 1.5, + multipleOf: 0.5, + }) + ) + ), + ].sort(); + + expect(results).toEqual([0, 0.5, 1, 1.5]); + }); + it('provides numbers with a given precision of 0.4 steps', () => { const results = [ ...new Set( @@ -292,13 +313,25 @@ describe('number', () => { it('throws an error for precision 0', () => { expect(() => faker.number.float({ precision: 0 })).toThrow( - new FakerError('Precision should be greater than 0.') + new FakerError('multipleOf/precision should be greater than 0.') + ); + }); + + it('throws an error for multipleOf 0', () => { + expect(() => faker.number.float({ multipleOf: 0 })).toThrow( + new FakerError('multipleOf/precision should be greater than 0.') ); }); it('throws an error for negative precision', () => { expect(() => faker.number.float({ precision: -0.01 })).toThrow( - new FakerError('Precision should be greater than 0.') + new FakerError('multipleOf/precision should be greater than 0.') + ); + }); + + it('throws an error for negative multipleOf', () => { + expect(() => faker.number.float({ multipleOf: -0.01 })).toThrow( + new FakerError('multipleOf/precision should be greater than 0.') ); }); |
