Skip to content

Commit

Permalink
test(NODE-4873): Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Jan 5, 2023
1 parent 04a3374 commit a5411c9
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ describe('Extended JSON', function () {
expect(parsed.a).to.equal(Number(dataView.getBigInt64(VALUE_OFFSET, true)));
});


it('serializes bigint values to numberLong in canonical mode', function () {
const number = { a: 2n };
const serialized = EJSON.stringify(number, { relaxed: false });
Expand All @@ -238,6 +237,36 @@ describe('Extended JSON', function () {
expect(serialized).to.equal('{"a":-9007199254740992,"b":9007199254740994}');
});

it('produces bigint strings that pass loose equality checks with native bigint values that are are 64 bits wide or less', function () {
const number = { a: 12345n };
const serialized = EJSON.stringify(number, { relaxed: false });
const parsed = JSON.parse(serialized);
// eslint-disable-next-line eqeqeq
expect(parsed.a.$numberLong == 12345n).true;
});

it('produces bigint strings that are equal to the strings generated when using BigInt.toString when the bigint values used are 64 bits wide or less', function () {
const number = { a: 12345n };
const serialized = EJSON.stringify(number, { relaxed: false });
const parsed = JSON.parse(serialized);
expect(parsed.a.$numberLong).to.equal(12345n.toString());
});

it('produces bigint strings that fail loose equality checks with native bigint values that are more than 64 bits wide', function () {
const number = { a: 0x1234_5678_1234_5678_9999n };
const serialized = EJSON.stringify(number, { relaxed: false });
const parsed = JSON.parse(serialized);
// eslint-disable-next-line eqeqeq
expect(parsed.a.$numberLong == 0x1234_5678_1234_5678_9999n).false;
});

it('produces bigint strings that are not equal to the strings generated when using BigInt.toString when the bigint values used are more than 64 bits wide', function () {
const number = { a: 0x1234_5678_1234_5678_9999n };
const serialized = EJSON.stringify(number, { relaxed: false });
const parsed = JSON.parse(serialized);
expect(parsed.a.$numberLong).to.not.equal(0x1234_5678_1234_5678_9999n.toString());
});

it('should correctly parse null values', function () {
expect(EJSON.parse('null')).to.be.null;
expect(EJSON.parse('[null]')[0]).to.be.null;
Expand Down

0 comments on commit a5411c9

Please sign in to comment.