aboutsummaryrefslogtreecommitdiff
path: root/test/modules
diff options
context:
space:
mode:
authorDivisionByZero <[email protected]>2024-01-18 17:14:48 +0100
committerGitHub <[email protected]>2024-01-18 17:14:48 +0100
commit41d87789c7ff353acfd0f5ca88a99c0d1fd3b500 (patch)
tree28d5a0a2e72881ed0202e2b2c86a4fb3b590e69d /test/modules
parentd71bc47a4a7b127b41068016cbed3584a35b47ea (diff)
downloadfaker-41d87789c7ff353acfd0f5ca88a99c0d1fd3b500.tar.xz
faker-41d87789c7ff353acfd0f5ca88a99c0d1fd3b500.zip
feat(number): add parameter `fractionDigits` in float (#1855)
Co-authored-by: ST-DDT <[email protected]>
Diffstat (limited to 'test/modules')
-rw-r--r--test/modules/__snapshots__/number.spec.ts.snap6
-rw-r--r--test/modules/datatype.spec.ts14
-rw-r--r--test/modules/number.spec.ts44
3 files changed, 61 insertions, 3 deletions
diff --git a/test/modules/__snapshots__/number.spec.ts.snap b/test/modules/__snapshots__/number.spec.ts.snap
index c04b4fe5..fc528fd9 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 fractionDigits 1`] = `-0.4261`;
+
exports[`number > 42 > float > with min, max and multipleOf 1`] = `-0.4261`;
exports[`number > 42 > float > with min, max and precision 1`] = `-0.4261`;
@@ -76,6 +78,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 fractionDigits 1`] = `61.0658`;
+
exports[`number > 1211 > float > with min, max and multipleOf 1`] = `61.0658`;
exports[`number > 1211 > float > with min, max and precision 1`] = `61.0658`;
@@ -126,6 +130,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 fractionDigits 1`] = `-12.9153`;
+
exports[`number > 1337 > float > with min, max and multipleOf 1`] = `-12.9153`;
exports[`number > 1337 > float > with min, max and precision 1`] = `-12.9153`;
diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts
index 75f2897c..7a75ce58 100644
--- a/test/modules/datatype.spec.ts
+++ b/test/modules/datatype.spec.ts
@@ -320,15 +320,23 @@ describe('datatype', () => {
const max = 9;
expect(() => {
- faker.datatype.number({ min, max });
+ faker.datatype.float({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
- it('should throw when precision is negative', () => {
+ it('should throw when precision <= 0', () => {
+ const min = 1;
+ const max = 2;
+
+ expect(() => {
+ faker.datatype.float({ min, max, precision: 0 });
+ }).toThrow(
+ new FakerError('multipleOf/precision should be greater than 0.')
+ );
expect(() => {
- faker.datatype.float({ precision: -0.01 });
+ faker.datatype.float({ min, max, precision: -1 });
}).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 c61498fa..922819c0 100644
--- a/test/modules/number.spec.ts
+++ b/test/modules/number.spec.ts
@@ -28,6 +28,11 @@ describe('number', () => {
max: 69,
precision: 0.0001,
})
+ .it('with min, max and fractionDigits', {
+ min: -42,
+ max: 69,
+ fractionDigits: 4,
+ })
.it('with min, max and multipleOf', {
min: -42,
max: 69,
@@ -281,6 +286,45 @@ describe('number', () => {
expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
});
+ it.each(times(100))(
+ 'provides numbers with an exact fractional digits',
+ () => {
+ const actual = faker.number.float({
+ min: 0.5,
+ max: 0.99,
+ fractionDigits: 2,
+ });
+ expect(actual).toBe(Number(actual.toFixed(2)));
+ }
+ );
+
+ it('throws an error if fractionDigits and multipleOf is provided at the same time', () => {
+ expect(() =>
+ faker.number.float({
+ min: 0,
+ max: 10,
+ multipleOf: 0.25,
+ fractionDigits: 6,
+ })
+ ).toThrow(
+ new FakerError(
+ 'multipleOf and fractionDigits cannot be set at the same time.'
+ )
+ );
+ });
+
+ it('throws an error for non integer fractionDigits numbers', () => {
+ expect(() => faker.number.float({ fractionDigits: 1.337 })).toThrow(
+ new FakerError('fractionDigits should be an integer.')
+ );
+ });
+
+ it('throws an error for negative fractionDigits', () => {
+ expect(() => faker.number.float({ fractionDigits: -2 })).toThrow(
+ new FakerError('fractionDigits should be greater than or equal to 0.')
+ );
+ });
+
it('provides numbers with a given precision of 0.2', () => {
const results = [
...new Set(