-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson_writer_test.go
90 lines (83 loc) · 2.27 KB
/
json_writer_test.go
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
85
86
87
88
89
90
package jsonconv
import (
"bytes"
"testing"
)
func TestJsonWriter_JsonObject(t *testing.T) {
// Prepare
data := JsonObject{
"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6",
"user": "Jon Doe",
"score": -100,
"is active": false,
"special1": "&",
"special2": "<",
"special3": ">",
"special4": "\u0026",
"special5": "\u003c",
"special6": "\u003e",
}
buf := &bytes.Buffer{}
wr := NewJsonWriter(buf)
// Process
err := wr.Write(data)
if err != nil {
t.Fatalf("failed to write json, err: %v", err)
}
// Check
s := buf.String()
expect := `{"id":"b042ab5c-ca73-4460-b739-96410ea9d3a6","is active":false,"score":-100,"special1":"\u0026","special2":"\u003c","special3":"\u003e","special4":"\u0026","special5":"\u003c","special6":"\u003e","user":"Jon Doe"}
`
if s == "" {
t.Fatalf("failed to write json to byte buffer")
}
if s != expect {
t.Fatalf("json output is not correct")
}
}
func TestJsonWriter_JsonArray(t *testing.T) {
// Prepare
data := JsonArray{
{
"id": "b042ab5c-ca73-4460-b739-96410ea9d3a6",
"user": "Jon Doe",
"score": -100,
"is active": false,
"special1": "&",
"special2": "<",
"special3": ">",
"special4": "\u0026",
"special5": "\u003c",
"special6": "\u003e",
},
{
"id": "ce06f5b1-5721-42c0-91e1-9f72a09c250a",
"user": "Tuấn",
"score": 1.5,
"is active": true,
},
{
"id": "4e01b638-44e5-4079-8043-baabbff21cc8",
"user": "高橋",
"score": 100000000000000000,
"is active": true,
},
}
buf := &bytes.Buffer{}
wr := NewJsonWriter(buf)
// Process
err := wr.Write(data)
if err != nil {
t.Fatalf("failed to write json, err: %v", err)
}
// Check
s := buf.String()
expected := `[{"id":"b042ab5c-ca73-4460-b739-96410ea9d3a6","is active":false,"score":-100,"special1":"\u0026","special2":"\u003c","special3":"\u003e","special4":"\u0026","special5":"\u003c","special6":"\u003e","user":"Jon Doe"},{"id":"ce06f5b1-5721-42c0-91e1-9f72a09c250a","is active":true,"score":1.5,"user":"Tuấn"},{"id":"4e01b638-44e5-4079-8043-baabbff21cc8","is active":true,"score":100000000000000000,"user":"高橋"}]
`
if s == "" {
t.Fatalf("failed to write json to byte buffer")
}
if s != expected {
t.Fatalf("json output is not correct")
}
}