aboutsummaryrefslogtreecommitdiff
path: root/test/modules
diff options
context:
space:
mode:
authorMatt Mayer <[email protected]>2024-03-13 19:32:53 +0700
committerGitHub <[email protected]>2024-03-13 12:32:53 +0000
commit5ef8ef1da13e83efc61702e64d8f75b611afb4e5 (patch)
tree4b64d666c83de96725aea38deb3650ba11ca9ade /test/modules
parent1169a0576ba1469d7c05be0b8fd844bde8a1af13 (diff)
downloadfaker-5ef8ef1da13e83efc61702e64d8f75b611afb4e5.tar.xz
faker-5ef8ef1da13e83efc61702e64d8f75b611afb4e5.zip
feat(number): add multipleOf to faker.number.int (#2586)
Co-authored-by: ST-DDT <[email protected]>
Diffstat (limited to 'test/modules')
-rw-r--r--test/modules/number.spec.ts84
1 files changed, 80 insertions, 4 deletions
diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts
index 475af30d..f318fc5b 100644
--- a/test/modules/number.spec.ts
+++ b/test/modules/number.spec.ts
@@ -61,6 +61,82 @@ describe('number', () => {
expect(actual).lessThanOrEqual(Number.MAX_SAFE_INTEGER);
});
+ it('should return an even integer', () => {
+ const actual = faker.number.int({ multipleOf: 2 });
+
+ expect(actual).toBeTypeOf('number');
+ expect(actual).toSatisfy(Number.isInteger);
+ expect(actual).toSatisfy((x: number) => x % 2 === 0);
+ expect(actual).toBeGreaterThanOrEqual(0);
+ expect(actual).toBeLessThanOrEqual(Number.MAX_SAFE_INTEGER);
+ });
+
+ it('provides numbers with a given multipleOf of 10 with exclusive ends', () => {
+ const results = [
+ ...new Set(
+ Array.from({ length: 100 }, () =>
+ faker.number.int({
+ min: 12,
+ max: 37,
+ multipleOf: 10,
+ })
+ )
+ ),
+ ].sort();
+ expect(results).toEqual([20, 30]);
+ });
+
+ it('provides numbers with a given multipleOf of 10 with inclusive ends', () => {
+ const results = [
+ ...new Set(
+ Array.from({ length: 100 }, () =>
+ faker.number.int({
+ min: 10,
+ max: 50,
+ multipleOf: 10,
+ })
+ )
+ ),
+ ].sort();
+ expect(results).toEqual([10, 20, 30, 40, 50]);
+ });
+
+ it('throws for float multipleOf', () => {
+ const input = {
+ min: 0,
+ max: 10,
+ multipleOf: 0.1,
+ };
+
+ expect(() => faker.number.int(input)).toThrow(
+ new FakerError('multipleOf should be an integer.')
+ );
+ });
+
+ it('throws for negative multipleOf', () => {
+ const input = {
+ min: -10,
+ max: 10,
+ multipleOf: -1,
+ };
+
+ expect(() => faker.number.int(input)).toThrow(
+ new FakerError('multipleOf should be greater than 0.')
+ );
+ });
+
+ it('throws for impossible multipleOf', () => {
+ const input = {
+ min: 11,
+ max: 19,
+ multipleOf: 10,
+ };
+
+ expect(() => faker.number.int(input)).toThrow(
+ new FakerError('No suitable integer value between 11 and 19 found.')
+ );
+ });
+
it('should return a random number given a maximum value as Number', () => {
const actual = faker.number.int(10);
@@ -167,7 +243,7 @@ describe('number', () => {
expect(() => {
faker.number.int({ min: 2.1, max: 2.9 });
}).toThrow(
- new FakerError(`No integer value between 2.1 and 2.9 found.`)
+ new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
@@ -368,7 +444,7 @@ describe('number', () => {
expect(() => {
faker.number.binary({ min: 2.1, max: 2.9 });
}).toThrow(
- new FakerError(`No integer value between 2.1 and 2.9 found.`)
+ new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
@@ -419,7 +495,7 @@ describe('number', () => {
expect(() => {
faker.number.octal({ min: 2.1, max: 2.9 });
}).toThrow(
- new FakerError(`No integer value between 2.1 and 2.9 found.`)
+ new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
@@ -467,7 +543,7 @@ describe('number', () => {
expect(() => {
faker.number.hex({ min: 2.1, max: 2.9 });
}).toThrow(
- new FakerError(`No integer value between 2.1 and 2.9 found.`)
+ new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});