Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <[email protected]>
  • Loading branch information
Hailong-am committed Oct 30, 2024
1 parent 6c3fd08 commit b011e0d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/unit/serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ test('Long numerals enabled', (t) => {
t.match(res, `"[ ${longNegative.toString()}, ${longPositive.toString()} ]"`);
});

test('Long numerals enabled and json not includes large numbers', (t) => {
t.plan(2);
const s = new Serializer({ enableLongNumeralSupport: true });
const longPositive = BigInt(Number.MAX_SAFE_INTEGER) * 2n; // eslint-disable-line no-undef
const longNegative = BigInt(Number.MIN_SAFE_INTEGER) * 2n; // eslint-disable-line no-undef
const json =
`{` +
// The space before and after the values, and the lack of spaces before comma are intentional
`"false-positive-1": "෴${longNegative.toString()}", ` +
`"false-positive-2": "[ ߷${longPositive.toString()} ]", ` +
`"false-positive-3": "\\": ֍${longPositive.toString()}\\"", ` +
`"false-positive-4": "෴߷֍${longPositive.toString()}", ` +
`"normal-number": 2024,` +
`"max-safe-integer": ${Number.MAX_SAFE_INTEGER},` +
`"min-safe-integer": ${Number.MIN_SAFE_INTEGER}` +
`}`;
const obj = s.deserialize(json);
const res = s.serialize(obj);
t.same(obj, {
'normal-number': 2024,
'max-safe-integer': `${Number.MAX_SAFE_INTEGER}`,
'min-safe-integer': `${Number.MIN_SAFE_INTEGER}`,
'false-positive-4': `෴߷֍${longPositive.toString()}`,
'false-positive-3': `": ֍${longPositive.toString()}"`,
'false-positive-2': `[ ߷${longPositive.toString()} ]`,
'false-positive-1': `෴${longNegative.toString()}`,
});
// The space before and after the values, and the lack of spaces before comma are intentional
t.equal(res.replace(/\s+/g, ''), json.replace(/\s+/g, ''));
});

test('long numerals not enabled', (t) => {
t.plan(5);
const s = new Serializer({ enableLongNumeralSupport: false });
Expand Down

0 comments on commit b011e0d

Please sign in to comment.