From a5411c927282e8c697ca99624a34e5b5d84a3946 Mon Sep 17 00:00:00 2001 From: Warren James Date: Thu, 5 Jan 2023 13:22:18 -0500 Subject: [PATCH] test(NODE-4873): Add new tests --- test/node/extended_json.test.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/test/node/extended_json.test.ts b/test/node/extended_json.test.ts index 1195be5e..f46773ca 100644 --- a/test/node/extended_json.test.ts +++ b/test/node/extended_json.test.ts @@ -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 }); @@ -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;