|
1 | 1 | import { describe, it, expect, vi } from 'vitest'; |
2 | 2 | import * as fc from 'fast-check'; |
3 | | -import { bigInt } from '../../../src/arbitrary/bigInt'; |
| 3 | +import { bigInt, type BigIntConstraints } from '../../../src/arbitrary/bigInt'; |
4 | 4 |
|
5 | 5 | import { fakeArbitrary } from './__test-helpers__/ArbitraryHelpers'; |
6 | 6 | import { declareCleaningHooksForSpies } from './__test-helpers__/SpyCleaner'; |
@@ -89,4 +89,44 @@ describe('bigInt', () => { |
89 | 89 | expect(() => bigInt({ min: high, max: low })).toThrowError(); |
90 | 90 | }), |
91 | 91 | )); |
| 92 | + |
| 93 | + it('should handle union type of [number, number] | BigIntConstraints', () => |
| 94 | + fc.assert( |
| 95 | + fc.property( |
| 96 | + fc.oneof( |
| 97 | + // no argument |
| 98 | + fc.tuple(), |
| 99 | + // min, max range |
| 100 | + fc.tuple(fc.bigInt(), fc.bigInt()).map<[bigint, bigint]>((t) => (t[0] < t[1] ? [t[0], t[1]] : [t[1], t[0]])), |
| 101 | + // both min and max defined |
| 102 | + fc.tuple( |
| 103 | + fc.record({ min: fc.bigInt(), max: fc.bigInt() }).map((c) => ({ |
| 104 | + min: c.min < c.max ? c.min : c.max, |
| 105 | + max: c.min < c.max ? c.max : c.min, |
| 106 | + })), |
| 107 | + ), |
| 108 | + // one or the other maybe defined |
| 109 | + fc.tuple(fc.record({ min: fc.option(fc.bigInt(), { nil: undefined }) })), |
| 110 | + fc.tuple(fc.record({ max: fc.option(fc.bigInt(), { nil: undefined }) })), |
| 111 | + ), |
| 112 | + (args: [] | [bigint, bigint] | [BigIntConstraints]) => { |
| 113 | + // Arrange |
| 114 | + const [expectedMin, expectedMax] = |
| 115 | + args.length === 0 ? [null, null] : args.length === 1 ? [args[0].min, args[0].max] : [args[0], args[1]]; |
| 116 | + const instance = fakeBigIntArbitrary(); |
| 117 | + const BigIntArbitrary = vi.spyOn(BigIntArbitraryMock, 'BigIntArbitrary'); |
| 118 | + BigIntArbitrary.mockImplementation(() => instance); |
| 119 | + |
| 120 | + // Act |
| 121 | + const arb = bigInt(...args); |
| 122 | + |
| 123 | + // Assert |
| 124 | + expect(BigIntArbitrary).toHaveBeenCalledWith( |
| 125 | + expectedMin ?? expect.any(BigInt), |
| 126 | + expectedMax ?? expect.any(BigInt), |
| 127 | + ); |
| 128 | + expect(arb).toBe(instance); |
| 129 | + }, |
| 130 | + ), |
| 131 | + )); |
92 | 132 | }); |
0 commit comments