Files
smartjson/test/test.both.ts

119 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

import { tap, expect } from '@git.zone/tstest/tapbundle';
2017-02-27 14:05:03 +01:00
2022-06-09 19:54:43 +02:00
import * as smartjson from '../ts/index.js';
2017-02-27 14:05:03 +01:00
2022-06-09 19:54:43 +02:00
class SomeClass extends smartjson.Smartjson {
@smartjson.foldDec() thisis: string = 'test';
2018-07-24 00:38:54 +02:00
constructor() {
super();
console.log(this.saveableProperties);
2017-02-27 14:05:03 +01:00
}
}
2018-07-24 00:38:54 +02:00
let mySomeClass: SomeClass;
2017-02-27 14:05:03 +01:00
tap.test('should create a Foldable extended instance', async () => {
2018-07-24 00:38:54 +02:00
mySomeClass = new SomeClass();
2022-06-09 19:54:43 +02:00
expect(mySomeClass).toBeInstanceOf(SomeClass);
expect(mySomeClass).toBeInstanceOf(smartjson.Smartjson);
2018-07-24 00:38:54 +02:00
});
2017-02-27 14:05:03 +01:00
tap.test('should create a folded object', async () => {
2018-07-24 00:38:54 +02:00
let foldedObject = mySomeClass.foldToObject();
2022-06-09 19:54:43 +02:00
expect(foldedObject).property('thisis').toEqual('test');
2018-07-24 00:38:54 +02:00
});
2017-02-27 14:05:03 +01:00
tap.test('should enfold from object', async () => {
2020-10-05 11:13:17 +00:00
const mySomeClass2 = SomeClass.enfoldFromObject({ thisis: 'test2' });
2022-06-09 19:54:43 +02:00
expect(mySomeClass2).property('thisis').toEqual('test2');
});
tap.test('should products stable jsons', async () => {
const jsonString = smartjson.stringify({
a: 1,
f: 6,
b: 3,
c: 3,
e: 5,
d: 4,
});
console.log(jsonString);
2018-07-24 00:38:54 +02:00
});
2022-10-26 10:56:01 +02:00
tap.test('should work with base64', async () => {
2022-09-13 21:37:42 +02:00
const someObject = {
hi: 'there',
thisIs: 'awesome',
};
const base64Json = smartjson.stringifyBase64(someObject);
console.log(base64Json);
const decodedObject = smartjson.parseBase64(base64Json);
expect(decodedObject).toEqual(someObject);
2022-10-26 10:52:14 +02:00
});
tap.test('stringify should handle plain string', async () => {
const stringifiedString = smartjson.stringify('hello');
console.log(stringifiedString);
2022-10-26 10:56:01 +02:00
expect(stringifiedString).toEqual('"hello"');
2022-10-26 10:52:14 +02:00
expect(smartjson.parse(stringifiedString)).toEqual('hello');
2022-10-26 10:56:01 +02:00
});
2022-09-13 21:37:42 +02:00
2024-02-25 01:43:07 +01:00
tap.test('should work with buffers', async () => {
const someObject = {
2024-03-03 10:29:18 +01:00
myBuffer: new TextEncoder().encode('hello')
2024-02-25 01:43:07 +01:00
};
2024-03-03 10:29:18 +01:00
console.log(someObject.myBuffer);
2024-02-25 01:43:07 +01:00
const stringified = smartjson.stringify(someObject);
2024-03-03 10:29:18 +01:00
console.log(stringified);
2024-02-25 01:43:07 +01:00
const decoded = smartjson.parse(stringified);
2024-03-03 10:29:18 +01:00
console.log(decoded.myBuffer);
let text = new TextDecoder().decode(decoded.myBuffer);
expect(text).toEqual('hello');
2024-02-25 01:43:07 +01:00
});
tap.test('should handle empty buffers', async () => {
const someObject = { empty: new Uint8Array([]) };
const json = smartjson.stringify(someObject);
const parsed = smartjson.parse(json);
expect(parsed.empty).toBeInstanceOf(Uint8Array);
expect(parsed.empty.length).toEqual(0);
});
tap.test('should parse and stringify JSONL', async () => {
const items = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' }
];
const jsonl = smartjson.stringifyJsonL(items);
const parsed = smartjson.parseJsonL(jsonl);
expect(parsed).toEqual(items);
});
tap.test('should deep-compare JSONL strings', async () => {
const a = '{"id":2,"name":"b"}\n{"id":1,"name":"a"}';
const b = '{"id":2,"name":"b"}\n{"id":1,"name":"a"}';
expect(smartjson.deepEqualJsonLStrings(a, b)).toEqual(true);
});
tap.test('should respect simpleOrderArray comparator', async () => {
const obj = { c: 3, a: 1, b: 2 };
const ordered = smartjson.stringify(obj, ['b', 'a']);
// ensure keys b, a come before c
expect(ordered.indexOf('"b"')).toBeLessThan(ordered.indexOf('"a"'));
expect(ordered.indexOf('"a"')).toBeLessThan(ordered.indexOf('"c"'));
});
tap.test('stableOneWayStringify should handle circular references without throwing', async () => {
const a: any = { name: 'x' };
a.self = a;
const b: any = { name: 'x' };
b.self = b;
const s1 = smartjson.stableOneWayStringify(a);
const s2 = smartjson.stableOneWayStringify(b);
expect(typeof s1).toEqual('string');
expect(s1).toEqual(s2);
});
tap.start();