91 lines
3.4 KiB
TypeScript
91 lines
3.4 KiB
TypeScript
import { expect, test } from 'vitest';
|
|
import { extendedJsonReviver } from '../source/index.ts';
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver reconstructs a positive bigint.
|
|
*/
|
|
const testReviverReconstructsPositiveBigint = (): void => {
|
|
// A positive bigint
|
|
const reconstructed = extendedJsonReviver('_', '<bigint: 42n>');
|
|
|
|
// The value is reconstructed as a bigint, not left as a string
|
|
expect(reconstructed).toBe(42n);
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver reconstructs a negative bigint.
|
|
*/
|
|
const testReviverReconstructsNegativeBigint = (): void => {
|
|
// A negative bigint
|
|
const reconstructed = extendedJsonReviver('_', '<bigint: -7n>');
|
|
|
|
// The sign is preserved
|
|
expect(reconstructed).toBe(-7n);
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver reconstructs zero as a bigint.
|
|
*/
|
|
const testReviverReconstructsZeroBigint = (): void => {
|
|
const reconstructed = extendedJsonReviver('_', '<bigint: 0n>');
|
|
|
|
// Zero is reconstructed as bigint 0n, not the number 0 or the string '0'
|
|
expect(reconstructed).toBe(0n);
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver reconstructs a Uint8Array.
|
|
*/
|
|
const testReviverReconstructsUint8Array = (): void => {
|
|
// A Uint8Array
|
|
const reconstructed = extendedJsonReviver('_', '<uint8array: abcd>');
|
|
|
|
// The value is a Uint8Array with the correct bytes
|
|
expect(reconstructed).toStrictEqual(new Uint8Array([ 0xab, 0xcd ]));
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver reconstructs an empty Uint8Array when the hex string is empty.
|
|
*/
|
|
const testReviverReconstructsEmptyUint8Array = (): void => {
|
|
const reconstructed = extendedJsonReviver('_', '<uint8array: >');
|
|
|
|
// An empty Uint8Array is returned, not null, undefined, or an empty string
|
|
expect(reconstructed).toStrictEqual(new Uint8Array(0));
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver passes through plain strings.
|
|
*/
|
|
const testReviverPassesThroughPlainString = (): void => {
|
|
const reconstructed = extendedJsonReviver('_', 'just a string');
|
|
|
|
// The value is passed through unchanged
|
|
expect(reconstructed).toBe('just a string');
|
|
};
|
|
|
|
/**
|
|
* Tests that extendedJsonReviver passes through non-string values.
|
|
*/
|
|
const testReviverPassesThroughNonStringValues = (): void => {
|
|
// Numbers, booleans, null, and objects pass through unchanged
|
|
expect(extendedJsonReviver('_', 42)).toBe(42);
|
|
expect(extendedJsonReviver('_', true)).toBe(true);
|
|
expect(extendedJsonReviver('_', false)).toBe(false);
|
|
expect(extendedJsonReviver('_', null)).toBe(null);
|
|
expect(extendedJsonReviver('_', undefined)).toBe(undefined);
|
|
expect(extendedJsonReviver('_', { foo: 'bar' })).toStrictEqual({ foo: 'bar' });
|
|
};
|
|
|
|
const runTests = async (): Promise<void> => {
|
|
test('extendedJsonReviver: reconstructs a positive bigint', testReviverReconstructsPositiveBigint);
|
|
test('extendedJsonReviver: reconstructs a negative bigint', testReviverReconstructsNegativeBigint);
|
|
test('extendedJsonReviver: reconstructs zero as bigint', testReviverReconstructsZeroBigint);
|
|
test('extendedJsonReviver: reconstructs a Uint8Array from hex', testReviverReconstructsUint8Array);
|
|
test('extendedJsonReviver: reconstructs an empty Uint8Array', testReviverReconstructsEmptyUint8Array);
|
|
test('extendedJsonReviver: passes through plain strings', testReviverPassesThroughPlainString);
|
|
test('extendedJsonReviver: passes through non-string values', testReviverPassesThroughNonStringValues);
|
|
};
|
|
|
|
await runTests();
|