|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { Long, BSONError, __noBigInt__ } from '../register-bson'; |
| 3 | +import { BSON_INT32_MAX, BSON_INT32_MIN } from '../../src/constants'; |
3 | 4 |
|
4 | 5 | describe('Long', function () { |
5 | 6 | it('accepts strings in the constructor', function () { |
@@ -164,6 +165,41 @@ describe('Long', function () { |
164 | 165 | }); |
165 | 166 | }); |
166 | 167 |
|
| 168 | + describe('static fromBigInt()', function () { |
| 169 | + const inputs: [ |
| 170 | + name: string, |
| 171 | + input: bigint, |
| 172 | + unsigned: boolean | undefined, |
| 173 | + expectedStr?: string |
| 174 | + ][] = [ |
| 175 | + ['0', BigInt('0'), false, '0'], |
| 176 | + ['-0 (bigint coerces this to 0)', BigInt('-0'), false, '0'], |
| 177 | + [ |
| 178 | + 'max unsigned input', |
| 179 | + BigInt(Long.MAX_UNSIGNED_VALUE.toString(10)), |
| 180 | + true, |
| 181 | + Long.MAX_UNSIGNED_VALUE.toString(10) |
| 182 | + ], |
| 183 | + ['max signed input', BigInt(Long.MAX_VALUE.toString(10)), false, Long.MAX_VALUE.toString(10)], |
| 184 | + ['min signed input', BigInt(Long.MIN_VALUE.toString(10)), false, Long.MIN_VALUE.toString(10)], |
| 185 | + ['negative greater than 32 bits', BigInt(-9228915101), false, '-9228915101'], |
| 186 | + ['less than 32 bits', BigInt(245666), false, '245666'], |
| 187 | + ['unsigned less than 32 bits', BigInt(245666), true, '245666'], |
| 188 | + ['negative less than 32 bits', BigInt(-245666), false, '-245666'], |
| 189 | + ['max int32', BigInt(BSON_INT32_MAX), false, BSON_INT32_MAX.toString(10)], |
| 190 | + ['max int32 unsigned', BigInt(BSON_INT32_MAX), true, BSON_INT32_MAX.toString(10)], |
| 191 | + ['min int32', BigInt(BSON_INT32_MIN), false, BSON_INT32_MIN.toString(10)] |
| 192 | + ]; |
| 193 | + |
| 194 | + for (const [testName, num, unsigned, expectedStr] of inputs) { |
| 195 | + context(`when the input is ${testName}`, () => { |
| 196 | + it(`should return a Long representation of the input`, () => { |
| 197 | + expect(Long.fromBigInt(num, unsigned).toString(10)).to.equal(expectedStr); |
| 198 | + }); |
| 199 | + }); |
| 200 | + } |
| 201 | + }); |
| 202 | + |
167 | 203 | describe('static fromString()', function () { |
168 | 204 | const successInputs: [ |
169 | 205 | name: string, |
|
0 commit comments