-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.test.ts
29 lines (25 loc) · 856 Bytes
/
json.test.ts
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
import { asserts } from './deps.ts'
import { jsonDecode, jsonEncode } from './json.ts'
Deno.test('jsonDecode decodes what jsonEncode encodes', () => {
const value = {
foo: {
bar: true,
bin: 'baz',
fatty: 8123,
acid: .3,
},
data: null,
}
asserts.assertEquals(jsonDecode(jsonEncode(value)), value)
// Can also encode/decode primitives
asserts.assertEquals(jsonDecode(jsonEncode(true)), true)
asserts.assertEquals(jsonDecode(jsonEncode('asd')), 'asd')
asserts.assertEquals(jsonDecode(jsonEncode(123)), 123)
asserts.assertEquals(jsonDecode(jsonEncode(.123)), .123)
asserts.assertEquals(jsonDecode(jsonEncode(null)), null)
})
Deno.test("jsonDecode throws if it can't decode", () => {
asserts.assertThrows(() => jsonDecode('duh'))
asserts.assertThrows(() => jsonDecode(''))
asserts.assertThrows(() => jsonDecode('{ new: }'))
})