|
1 | 1 | import { describe, expect, it } from 'vitest';
|
2 |
| -import { faker } from '../src'; |
| 2 | +import { faker, FakerError } from '../src'; |
3 | 3 |
|
4 | 4 | const seededRuns = [
|
5 | 5 | {
|
@@ -69,8 +69,8 @@ const seededRuns = [
|
69 | 69 | length: [79654, '2eiXX/J/*&', 86617, 60111],
|
70 | 70 | },
|
71 | 71 | bigInt: {
|
72 |
| - noArgs: 3745409999962546n, |
73 |
| - value: 42n, |
| 72 | + noArgs: 379177551410048n, |
| 73 | + value: 37n, |
74 | 74 | },
|
75 | 75 | },
|
76 | 76 | },
|
@@ -141,8 +141,8 @@ const seededRuns = [
|
141 | 141 | length: [56052, 21258, 54308, 3397],
|
142 | 142 | },
|
143 | 143 | bigInt: {
|
144 |
| - noArgs: 2620209999973798n, |
145 |
| - value: 42n, |
| 144 | + noArgs: 251225403255239n, |
| 145 | + value: 25n, |
146 | 146 | },
|
147 | 147 | },
|
148 | 148 | },
|
@@ -213,8 +213,8 @@ const seededRuns = [
|
213 | 213 | length: ['Kti5-}$_/`', 76408, 35403, 69406],
|
214 | 214 | },
|
215 | 215 | bigInt: {
|
216 |
| - noArgs: 9285209999907148n, |
217 |
| - value: 42n, |
| 216 | + noArgs: 948721906162743n, |
| 217 | + value: 8n, |
218 | 218 | },
|
219 | 219 | },
|
220 | 220 | },
|
@@ -417,6 +417,19 @@ describe('datatype', () => {
|
417 | 417 | const actual = faker.datatype.bigInt(42);
|
418 | 418 | expect(actual).toEqual(expectations.bigInt.value);
|
419 | 419 | });
|
| 420 | + |
| 421 | + it('should throw when min > max', () => { |
| 422 | + const min = 10000n; |
| 423 | + const max = 999n; |
| 424 | + |
| 425 | + faker.seed(seed); |
| 426 | + |
| 427 | + expect(() => { |
| 428 | + faker.datatype.bigInt({ min, max }); |
| 429 | + }).toThrowError( |
| 430 | + new FakerError(`Max ${max} should be larger then min ${min}.`) |
| 431 | + ); |
| 432 | + }); |
420 | 433 | });
|
421 | 434 | });
|
422 | 435 | }
|
@@ -701,6 +714,76 @@ describe('datatype', () => {
|
701 | 714 | const generateBigInt = faker.datatype.bigInt();
|
702 | 715 | expect(generateBigInt).toBeTypeOf('bigint');
|
703 | 716 | });
|
| 717 | + |
| 718 | + it('should generate a big bigInt value with low delta', () => { |
| 719 | + const min = 999999999n; |
| 720 | + const max = 1000000000n; |
| 721 | + const generateBigInt = faker.datatype.bigInt({ min, max }); |
| 722 | + expect(generateBigInt).toBeTypeOf('bigint'); |
| 723 | + expect(generateBigInt).toBeGreaterThanOrEqual(min); |
| 724 | + expect(generateBigInt).toBeLessThanOrEqual(max); |
| 725 | + }); |
| 726 | + |
| 727 | + it('should return a random bigint given a maximum value as BigInt', () => { |
| 728 | + const max = 10n; |
| 729 | + expect(faker.datatype.bigInt(max)).toBeGreaterThanOrEqual(0n); |
| 730 | + expect(faker.datatype.bigInt(max)).toBeLessThanOrEqual(max); |
| 731 | + }); |
| 732 | + |
| 733 | + it('should return a random bigint given a maximum value as Object', () => { |
| 734 | + const options = { max: 10n }; |
| 735 | + expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual(0n); |
| 736 | + expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual( |
| 737 | + options.max |
| 738 | + ); |
| 739 | + }); |
| 740 | + |
| 741 | + it('should return a random bigint given a maximum value of 0', () => { |
| 742 | + const options = { max: 0n }; |
| 743 | + expect(faker.datatype.bigInt(options)).toBe(0n); |
| 744 | + }); |
| 745 | + |
| 746 | + it('should return a random bigint given a negative bigint minimum and maximum value of 0', () => { |
| 747 | + const options = { min: -100n, max: 0n }; |
| 748 | + expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual( |
| 749 | + options.min |
| 750 | + ); |
| 751 | + expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual( |
| 752 | + options.max |
| 753 | + ); |
| 754 | + }); |
| 755 | + |
| 756 | + it('should return a random bigint between a range', () => { |
| 757 | + const options = { min: 22, max: 33 }; |
| 758 | + for (let i = 0; i < 100; i++) { |
| 759 | + const randomBigInt = faker.datatype.bigInt(options); |
| 760 | + expect(randomBigInt).toBeGreaterThanOrEqual(options.min); |
| 761 | + expect(randomBigInt).toBeLessThanOrEqual(options.max); |
| 762 | + } |
| 763 | + }); |
| 764 | + |
| 765 | + it('should succeed with success-rate', () => { |
| 766 | + const min = 0n; |
| 767 | + const max = 1000000000000n; |
| 768 | + const randomBigInt = faker.datatype.bigInt({ min, max }); |
| 769 | + expect(randomBigInt).toBeGreaterThanOrEqual(min); |
| 770 | + expect(randomBigInt).toBeLessThanOrEqual(max); |
| 771 | + }); |
| 772 | + |
| 773 | + it('should not mutate the input object', () => { |
| 774 | + const initialMin = 1n; |
| 775 | + const initialOtherProperty = 'hello darkness my old friend'; |
| 776 | + const input: { |
| 777 | + min?: bigint; |
| 778 | + max?: bigint; |
| 779 | + otherProperty: string; |
| 780 | + } = Object.freeze({ |
| 781 | + min: initialMin, |
| 782 | + otherProperty: initialOtherProperty, |
| 783 | + }); |
| 784 | + |
| 785 | + expect(() => faker.datatype.bigInt(input)).not.toThrow(); |
| 786 | + }); |
704 | 787 | });
|
705 | 788 | }
|
706 | 789 | });
|
|
0 commit comments