File: json_test.go

package info (click to toggle)
golang-github-d5-tengo 2.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: makefile: 12
file content (84 lines) | stat: -rw-r--r-- 2,945 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package stdlib_test

import "testing"

func TestJSON(t *testing.T) {
	module(t, "json").call("encode", 5).
		expect([]byte("5"))
	module(t, "json").call("encode", "foobar").
		expect([]byte(`"foobar"`))
	module(t, "json").call("encode", MAP{"foo": 5}).
		expect([]byte("{\"foo\":5}"))
	module(t, "json").call("encode", IMAP{"foo": 5}).
		expect([]byte("{\"foo\":5}"))
	module(t, "json").call("encode", ARR{1, 2, 3}).
		expect([]byte("[1,2,3]"))
	module(t, "json").call("encode", IARR{1, 2, 3}).
		expect([]byte("[1,2,3]"))
	module(t, "json").call("encode", MAP{"foo": "bar"}).
		expect([]byte("{\"foo\":\"bar\"}"))
	module(t, "json").call("encode", MAP{"foo": 1.8}).
		expect([]byte("{\"foo\":1.8}"))
	module(t, "json").call("encode", MAP{"foo": true}).
		expect([]byte("{\"foo\":true}"))
	module(t, "json").call("encode", MAP{"foo": '8'}).
		expect([]byte("{\"foo\":56}"))
	module(t, "json").call("encode", MAP{"foo": []byte("foo")}).
		expect([]byte("{\"foo\":\"Zm9v\"}")) // json encoding returns []byte as base64 encoded string
	module(t, "json").
		call("encode", MAP{"foo": ARR{"bar", 1, 1.8, '8', true}}).
		expect([]byte("{\"foo\":[\"bar\",1,1.8,56,true]}"))
	module(t, "json").
		call("encode", MAP{"foo": IARR{"bar", 1, 1.8, '8', true}}).
		expect([]byte("{\"foo\":[\"bar\",1,1.8,56,true]}"))
	module(t, "json").
		call("encode", MAP{"foo": ARR{ARR{"bar", 1}, ARR{"bar", 1}}}).
		expect([]byte("{\"foo\":[[\"bar\",1],[\"bar\",1]]}"))
	module(t, "json").
		call("encode", MAP{"foo": MAP{"string": "bar"}}).
		expect([]byte("{\"foo\":{\"string\":\"bar\"}}"))
	module(t, "json").
		call("encode", MAP{"foo": IMAP{"string": "bar"}}).
		expect([]byte("{\"foo\":{\"string\":\"bar\"}}"))
	module(t, "json").
		call("encode", MAP{"foo": MAP{"map1": MAP{"string": "bar"}}}).
		expect([]byte("{\"foo\":{\"map1\":{\"string\":\"bar\"}}}"))
	module(t, "json").
		call("encode", ARR{ARR{"bar", 1}, ARR{"bar", 1}}).
		expect([]byte("[[\"bar\",1],[\"bar\",1]]"))

	module(t, "json").call("decode", `5`).
		expect(5)
	module(t, "json").call("decode", `"foo"`).
		expect("foo")
	module(t, "json").call("decode", `[1,2,3,"bar"]`).
		expect(ARR{1, 2, 3, "bar"})
	module(t, "json").call("decode", `{"foo":5}`).
		expect(MAP{"foo": 5})
	module(t, "json").call("decode", `{"foo":2.5}`).
		expect(MAP{"foo": 2.5})
	module(t, "json").call("decode", `{"foo":true}`).
		expect(MAP{"foo": true})
	module(t, "json").call("decode", `{"foo":"bar"}`).
		expect(MAP{"foo": "bar"})
	module(t, "json").call("decode", `{"foo":[1,2,3,"bar"]}`).
		expect(MAP{"foo": ARR{1, 2, 3, "bar"}})

	module(t, "json").
		call("indent", []byte("{\"foo\":[\"bar\",1,1.8,56,true]}"), "", "  ").
		expect([]byte(`{
  "foo": [
    "bar",
    1,
    1.8,
    56,
    true
  ]
}`))

	module(t, "json").
		call("html_escape", []byte(
			`{"M":"<html>foo &`+"\xe2\x80\xa8 \xe2\x80\xa9"+`</html>"}`)).
		expect([]byte(
			`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
}