1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import * as json from './json.js';
import { expect, describe, it } from 'vitest';
describe('encode', async () => {
it('converts atoms', async () => {
expect(json.encode(0)).toStrictEqual('0');
expect(json.encode('hello')).toStrictEqual('"hello"');
expect(json.encode(null)).toStrictEqual('null');
expect(json.encode(true)).toStrictEqual('true');
});
it('converts lists with indentation', async () => {
expect(json.encode([])).toStrictEqual('[]');
expect(json.encode(['hello', 'world'])).toStrictEqual('[\n "hello",\n "world"\n]');
});
it('converts objects with indentation', async () => {
expect(json.encode({})).toStrictEqual('{}');
expect(json.encode({ field: 'value' })).toStrictEqual('{\n "field": "value"\n}');
});
});
describe('decode', async () => {
it('converts valid json values', async () => {
expect(json.decode('0')).toStrictEqual(0);
expect(json.decode('null')).toStrictEqual(null);
expect(json.decode('{}')).toStrictEqual({});
expect(json.decode('[1, 2, "3"]')).toStrictEqual([1, 2, '3']);
});
it('converts invalid json to `undefined`', async () => {
expect(json.decode('')).toBeUndefined();
expect(json.decode('{"foo":')).toBeUndefined();
expect(json.decode('arbitrary nonsense')).toBeUndefined();
});
});
|