1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
import { isHexadecimal, isOctal } from 'validator';
import { describe, expect, it, vi } from 'vitest';
import { FakerError, SimpleFaker, faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { MERSENNE_MAX_VALUE } from '../utils/mersenne-test-utils';
import { times } from './../support/times';
function isFloat(value: number): boolean {
return value % 1 !== 0;
}
function isBinary(str: string): boolean {
return [...str].every((char) => char === '0' || char === '1');
}
describe('number', () => {
seededTests(faker, 'number', (t) => {
t.describeEach(
'int',
'binary',
'octal',
'hex'
)((t) => {
t.it('noArgs')
.it('with value', 1)
.it('with options', { min: 0, max: 10 });
});
t.describe('float', (t) => {
t.it('with plain number', 4)
.it('with min', { min: -42 })
.it('with max', { max: 69 })
.it('with min and max', { min: -42, max: 69 })
.it('with min, max and fractionDigits', {
min: -42,
max: 69,
fractionDigits: 4,
})
.it('with min, max and multipleOf', {
min: -42,
max: 69,
multipleOf: 0.0001,
});
});
t.describe('bigInt', (t) => {
t.it('noArgs')
.it('with number value', 42)
.it('with string value', '69')
.it('with boolean value', true)
.it('with bigint value', 123n)
.it('with options', { min: -42, max: 69 })
.it('with big options', {
min: 6135715171537515454317351n,
max: 32465761264574654845432354n,
});
});
t.describe('romanNumeral', (t) => {
t.it('noArgs')
.it('with number value', 1000)
.it('with only min', { min: 5 })
.it('with only max', { max: 165 })
.it('with min as 1', { min: 1 })
.it('with max as 3999', { max: 3999 })
.it('with min and max', { min: 100, max: 502 });
});
});
describe(`random seeded tests for seed ${faker.seed()}`, () => {
describe('int', () => {
it('should return an integer between 0 and Number.MAX_SAFE_INTEGER (inclusive) by default', () => {
const actual = faker.number.int();
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(0);
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);
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(10);
});
it('should return a random number given a maximum value as Object', () => {
const actual = faker.number.int({ max: 10 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(10);
});
it('should return a random number given a maximum value of 0', () => {
const actual = faker.number.int({ max: 0 });
expect(actual).toBe(0);
});
it('should return a random number given a negative number minimum and maximum value of 0', () => {
const actual = faker.number.int({ min: -100, max: 0 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(-100);
expect(actual).toBeLessThanOrEqual(0);
});
it('should return a random number between a range', () => {
for (let i = 0; i < 100; i++) {
const actual = faker.number.int({ min: 22, max: 33 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(22);
expect(actual).toBeLessThanOrEqual(33);
}
});
it('should return inclusive negative max value', () => {
let foundNegative4 = false;
let foundNegative5 = false;
for (let iter = 0; iter < 1000; iter++) {
const actual = faker.number.int({ min: -5, max: -4 });
if (actual === -4) {
foundNegative4 = true;
} else if (actual === -5) {
foundNegative5 = true;
}
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(Number.isInteger);
expect(actual).toBeGreaterThanOrEqual(-5);
expect(actual).toBeLessThanOrEqual(-4);
if (foundNegative4 && foundNegative5) {
break;
}
}
expect(foundNegative4).toBeTruthy();
expect(foundNegative5).toBeTruthy();
});
it('should not mutate the input object', () => {
const input: {
min?: number;
max?: number;
precision?: number;
otherProperty: string;
} = Object.freeze({
min: 1,
precision: 1,
otherProperty: 'hello darkness my old friend',
});
expect(() => faker.number.int(input)).not.toThrow();
});
it('should throw when min > max', () => {
const min = 10;
const max = 9;
expect(() => {
faker.number.int({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
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 suitable integer value between 2.1 and 2.9 found.`)
);
});
});
describe('float', () => {
it('should return a float between 0 and 1 (inclusive) by default', () => {
const actual = faker.number.float();
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(isFloat);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(1);
});
it('should return a random float with given max', () => {
const actual = faker.number.float(3);
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(isFloat);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(3);
});
it('should return a random number given a max value of 10', () => {
const actual = faker.number.float({ max: 10 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(isFloat);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(10);
});
it('should return 0 given a max value of 0', () => {
expect(faker.number.float({ max: 0 })).toBe(0);
});
it('should return a random number given a negative number min and max value of 0', () => {
const actual = faker.number.float({ min: -100, max: 0 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(isFloat);
expect(actual).toBeGreaterThanOrEqual(-100);
expect(actual).toBeLessThanOrEqual(0);
});
it('should return a random number between a range', () => {
for (let i = 0; i < 5; i++) {
const actual = faker.number.float({ min: 22, max: 33 });
expect(actual).toBeTypeOf('number');
expect(actual).toSatisfy(isFloat);
expect(actual).toBeGreaterThanOrEqual(22);
expect(actual).toBeLessThanOrEqual(33);
}
});
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.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('throws an error for multipleOf 0', () => {
expect(() => faker.number.float({ multipleOf: 0 })).toThrow(
new FakerError('multipleOf should be greater than 0.')
);
});
it('throws an error for negative multipleOf', () => {
expect(() => faker.number.float({ multipleOf: -0.01 })).toThrow(
new FakerError('multipleOf should be greater than 0.')
);
});
it('should not modify the input object', () => {
expect(() =>
faker.number.float(Object.freeze({ min: 1, max: 2 }))
).not.toThrow();
});
it('should throw when min > max', () => {
const min = 10;
const max = 9;
expect(() => {
faker.number.float({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
});
describe('binary', () => {
it('generates single binary character when no additional argument was provided', () => {
const binary = faker.number.binary();
expect(binary).toBeTypeOf('string');
expect(binary).toSatisfy(isBinary);
expect(binary).toHaveLength(1);
});
it('generates a random binary string with a custom max value', () => {
const binary = faker.number.binary(5);
expect(binary).toBeTypeOf('string');
expect(binary).toSatisfy(isBinary);
const binaryNum = Number.parseInt(binary, 2);
expect(binaryNum).toBeLessThanOrEqual(5);
});
it('generates a random binary in a specific range', () => {
const binary = faker.number.binary({ min: 15, max: 255 });
expect(binary).toBeTypeOf('string');
expect(binary).toSatisfy(isBinary);
const binaryNum = Number.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 });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
it('should throw when there is no integer between min and max', () => {
expect(() => {
faker.number.binary({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
describe('octal', () => {
it('generates single octal character when no additional argument was provided', () => {
const octal = faker.number.octal();
expect(octal).toBeTypeOf('string');
expect(octal).toSatisfy(isOctal);
expect(octal).toHaveLength(1);
});
it('generates a random octal string with a custom max value', () => {
const octal = faker.number.octal(5);
expect(octal).toBeTypeOf('string');
expect(octal).toSatisfy(isOctal);
const octalNum = Number.parseInt(octal, 8);
expect(octalNum).toBeLessThanOrEqual(5);
});
it('generates a random octal in a specific range', () => {
const octal = faker.number.octal({ min: 15, max: 255 });
expect(octal).toBeTypeOf('string');
expect(octal).toSatisfy(isOctal);
const octalNum = Number.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 });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
it('should throw when there is no integer between min and max', () => {
expect(() => {
faker.number.octal({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
describe('hex', () => {
it('generates single hex character when no additional argument was provided', () => {
const hex = faker.number.hex();
expect(hex).toBeTypeOf('string');
expect(hex).toSatisfy(isHexadecimal);
expect(hex).toHaveLength(1);
});
it('generates a random hex string', () => {
const hex = faker.number.hex(5);
expect(hex).toBeTypeOf('string');
expect(hex).toSatisfy(isHexadecimal);
});
it('generates a random hex in a specific range', () => {
const hex = faker.number.hex({ min: 15, max: 255 });
expect(hex).toBeTypeOf('string');
expect(hex).toSatisfy(isHexadecimal);
const hexNum = Number.parseInt(hex, 16);
expect(hexNum).toBeLessThanOrEqual(255);
expect(hexNum).greaterThanOrEqual(15);
});
it('should throw when min > max', () => {
const min = 10;
const max = 9;
expect(() => {
faker.number.hex({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be greater than min ${min}.`)
);
});
it('should throw when there is no integer between min and max', () => {
expect(() => {
faker.number.hex({ min: 2.1, max: 2.9 });
}).toThrow(
new FakerError(`No suitable integer value between 2.1 and 2.9 found.`)
);
});
});
describe('bigInt', () => {
it('should generate a bigInt value', () => {
const generateBigInt = faker.number.bigInt();
expect(generateBigInt).toBeTypeOf('bigint');
});
it('should generate a big bigInt value with low delta', () => {
const generateBigInt = faker.number.bigInt({
min: 999999999n,
max: 1000000000n,
});
expect(generateBigInt).toBeTypeOf('bigint');
expect(generateBigInt).toBeGreaterThanOrEqual(999999999n);
expect(generateBigInt).toBeLessThanOrEqual(1000000000n);
});
it('should return a random bigint given a maximum value as BigInt', () => {
const generateBigInt = faker.number.bigInt(10n);
expect(generateBigInt).toBeGreaterThanOrEqual(0n);
expect(generateBigInt).toBeLessThanOrEqual(10n);
});
it('should return a random bigint given a maximum value as Object', () => {
const generateBigInt = faker.number.bigInt({ max: 10n });
expect(generateBigInt).toBeGreaterThanOrEqual(0n);
expect(generateBigInt).toBeLessThanOrEqual(10n);
});
it('should return a random bigint given a maximum value of 0', () => {
expect(faker.number.bigInt({ max: 0n })).toBe(0n);
});
it('should return a random bigint given a negative bigint minimum and maximum value of 0', () => {
const generateBigInt = faker.number.bigInt({ min: -100n, max: 0n });
expect(generateBigInt).toBeGreaterThanOrEqual(-100n);
expect(generateBigInt).toBeLessThanOrEqual(0n);
});
it('should return a random bigint between a range', () => {
const randomBigInt = faker.number.bigInt({ min: 22, max: 33 });
expect(randomBigInt).toBeGreaterThanOrEqual(22);
expect(randomBigInt).toBeLessThanOrEqual(33);
});
it('should return a random bigint for a very large range', () => {
const randomBigInt = faker.number.bigInt({
min: 0n,
max: 10000000000000000000000n,
});
expect(randomBigInt).toBeGreaterThanOrEqual(0n);
expect(randomBigInt).toBeLessThanOrEqual(10000000000000000000000n);
});
it('should not mutate the input object', () => {
const input: {
min?: bigint;
max?: bigint;
otherProperty: string;
} = Object.freeze({
min: 1n,
otherProperty: 'hello darkness my old friend',
});
expect(() => faker.number.bigInt(input)).not.toThrow();
});
it('should throw when min > max', () => {
const min = 10000n;
const max = 999n;
expect(() => {
faker.number.bigInt({ min, max });
}).toThrow(
new FakerError(`Max ${max} should be larger then min ${min}.`)
);
});
});
describe('romanNumeral', () => {
it('should generate a Roman numeral within default range', () => {
const roman = faker.number.romanNumeral();
expect(roman).toBeTypeOf('string');
expect(roman).toMatch(/^[IVXLCDM]+$/);
});
it('should generate a Roman numeral with max value of 1000', () => {
const roman = faker.number.romanNumeral(1000);
expect(roman).toMatch(/^[IVXLCDM]+$/);
});
it.each(
Object.entries({
I: 1,
IV: 4,
IX: 9,
X: 10,
XXVII: 27,
XC: 90,
XCIX: 99,
CCLXIII: 263,
DXXXVI: 536,
DCCXIX: 719,
MDCCCLI: 1851,
MDCCCXCII: 1892,
MMCLXXXIII: 2183,
MMCMXLIII: 2943,
MMMDCCLXVI: 3766,
MMMDCCLXXIV: 3774,
MMMCMXCIX: 3999,
})
)(
'should generate a Roman numeral %s for value %d',
(expected: string, value: number) => {
const mock = vi.spyOn(faker.number, 'int');
mock.mockReturnValue(value);
const actual = faker.number.romanNumeral();
mock.mockRestore();
expect(actual).toBe(expected);
}
);
it('should throw when min value is less than 1', () => {
expect(() => {
faker.number.romanNumeral({ min: 0 });
}).toThrow(new FakerError('Min value 0 should be 1 or greater.'));
});
it('should throw when max value is greater than 3999', () => {
expect(() => {
faker.number.romanNumeral({ max: 4000 });
}).toThrow(new FakerError('Max value 4000 should be 3999 or less.'));
});
it('should throw when max value is less than min value', () => {
expect(() => {
faker.number.romanNumeral({ min: 500, max: 100 });
}).toThrow(new FakerError('Max 100 should be greater than min 500.'));
});
});
});
describe('value range tests', () => {
const customFaker = new SimpleFaker();
// @ts-expect-error: access private member field
const randomizer = customFaker._randomizer;
describe('int', () => {
it('should be able to return 0', () => {
randomizer.next = () => 0;
const actual = customFaker.number.int();
expect(actual).toBe(0);
});
it('should be able to return MAX_SAFE_INTEGER', () => {
randomizer.next = () => MERSENNE_MAX_VALUE;
const actual = customFaker.number.int();
expect(actual).toBe(Number.MAX_SAFE_INTEGER);
});
});
describe('float', () => {
it('should be able to return 0', () => {
randomizer.next = () => 0;
const actual = customFaker.number.float();
expect(actual).toBe(0);
});
it('should be able to return almost 1', () => {
randomizer.next = () => MERSENNE_MAX_VALUE;
const actual = customFaker.number.float();
expect(actual).toBe(MERSENNE_MAX_VALUE);
});
});
});
});
|