From e4839a9fc91d0ffc36c2015b34fcba33a6797bb4 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 10 Dec 2022 10:38:58 +0100 Subject: [PATCH] fix(number): values out of bounds (#1648) --- src/modules/number/index.ts | 15 +++++++++++---- test/__snapshots__/finance.spec.ts.snap | 4 ++-- test/__snapshots__/location.spec.ts.snap | 4 ++-- test/number.spec.ts | 19 ++++++++----------- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/modules/number/index.ts b/src/modules/number/index.ts index 292fbcf2e3a..cef0b27c5aa 100644 --- a/src/modules/number/index.ts +++ b/src/modules/number/index.ts @@ -43,12 +43,19 @@ export class NumberModule { } const { min = 0, max = min + 99999 } = options; + const effectiveMin = Math.ceil(min); + const effectiveMax = Math.floor(max); - if (max === min) { - return min; + if (effectiveMin === effectiveMax) { + return effectiveMin; } - if (max < min) { + if (effectiveMax < effectiveMin) { + if (max >= min) { + throw new FakerError( + `No integer value between ${min} and ${max} found.` + ); + } throw new FakerError(`Max ${max} should be greater than min ${min}.`); } @@ -56,7 +63,7 @@ export class NumberModule { // @ts-expect-error: access private member field this.faker._mersenne; - return mersenne.next({ min, max: max + 1 }); + return mersenne.next({ min: effectiveMin, max: effectiveMax + 1 }); } /** diff --git a/test/__snapshots__/finance.spec.ts.snap b/test/__snapshots__/finance.spec.ts.snap index 33737a1a8bd..95fa5c2618a 100644 --- a/test/__snapshots__/finance.spec.ts.snap +++ b/test/__snapshots__/finance.spec.ts.snap @@ -14,7 +14,7 @@ exports[`finance > 42 > amount > with max 1`] = `"18.73"`; exports[`finance > 42 > amount > with min 1`] = `"380.79"`; -exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98160"`; +exports[`finance > 42 > amount > with min and max and dec and symbol 1`] = `"$24.98161"`; exports[`finance > 42 > bic > noArgs 1`] = `"UYETSCLLG53"`; @@ -146,7 +146,7 @@ exports[`finance > 1337 > amount > with max 1`] = `"13.10"`; exports[`finance > 1337 > amount > with min 1`] = `"269.40"`; -exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48098"`; +exports[`finance > 1337 > amount > with min and max and dec and symbol 1`] = `"$20.48099"`; exports[`finance > 1337 > bic > noArgs 1`] = `"OEFHLYG18IL"`; diff --git a/test/__snapshots__/location.spec.ts.snap b/test/__snapshots__/location.spec.ts.snap index bceda5a4bb4..41e9f8c0f8a 100644 --- a/test/__snapshots__/location.spec.ts.snap +++ b/test/__snapshots__/location.spec.ts.snap @@ -112,8 +112,8 @@ exports[`location > 1211 > longitude > noArgs 1`] = `154.2673`; exports[`location > 1211 > nearbyGPSCoordinate > near origin 1`] = ` [ - -0.02872051646443488, - 0.05959053473372933, + -0.02872111236834616, + 0.05959024752564801, ] `; diff --git a/test/number.spec.ts b/test/number.spec.ts index 7605a513cb2..f54804399ba 100644 --- a/test/number.spec.ts +++ b/test/number.spec.ts @@ -130,15 +130,13 @@ describe('number', () => { }).toThrowError(`Max ${max} should be greater than min ${min}.`); }); - // TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595 - it.todo( - 'should throw when there is no integer between min and max', - () => { - expect(() => { - faker.number.int({ min: 2.1, max: 2.9 }); - }).toThrow(); - } - ); + it('should throw when there is no integer between min and max', () => { + expect(() => { + faker.number.int({ min: 2.1, max: 2.9 }); + }).toThrow( + new FakerError(`No integer value between 2.1 and 2.9 found.`) + ); + }); }); describe('float', () => { @@ -193,8 +191,7 @@ describe('number', () => { expect(results).toEqual([0, 0.5, 1, 1.5]); }); - // TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595 - it.todo('provides numbers with a given precision of 0.4 steps', () => { + it('provides numbers with a given precision of 0.4 steps', () => { const results = Array.from( new Set( Array.from({ length: 50 }, () =>