Repository navigation
Expand file tree
/
Copy pathjson_test.go
More file actions
197 lines (169 loc) · 4.84 KB
/
Copy pathjson_test.go
File metadata and controls
197 lines (169 loc) · 4.84 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package enc_test
import (
"encoding/json"
"testing"
"github.com/ohait/forego/enc"
"github.com/ohait/forego/test"
)
// Empty json.RawMessage should cause error in encoding/json, but not in enc.
func TestEmptyRawMessage(t *testing.T) {
c := test.Context(t)
empty := json.RawMessage{}
_, err := json.Marshal(empty)
test.Error(t, err)
node, err := enc.Marshal(c, empty)
test.NoError(t, err)
b := enc.JSON{}.Encode(c, node)
test.EqualsStr(t, "null", string(b)) // it started out as nothing (invalid json), but enc converts it to "null"
err = enc.UnmarshalJSON(c, b, &empty)
test.NoError(t, err)
test.EqualsGo(t, json.RawMessage("null"), empty)
}
func TestStruct(t *testing.T) {
c := test.Context(t)
type X struct {
S string `json:"s"`
I int `json:"i"`
F float64 `json:"f"`
A any `json:"a"`
}
x := X{S: "str", I: 42, F: 3.14, A: 3}
n, err := enc.Marshal(c, x)
test.NoError(t, err)
test.ContainsGo(t, n, `42`)
j := enc.JSON{}.Encode(c, n)
test.EqualsStr(t, `{"s":"str","i":42,"f":3.14,"a":3}`, string(j))
}
func TestJSON(t *testing.T) {
c := test.Context(t)
codec := &enc.JSON{}
check := func(t *testing.T, j string, nodeIn enc.Node) {
t.Helper()
t.Logf("%s <=> %#v", j, nodeIn)
jIn := []byte(j)
jOut := codec.Encode(c, nodeIn)
test.EqualsGo(t, string(jIn), string(jOut))
nodeOut, err := codec.Decode(c, jIn)
test.NoError(t, err)
test.EqualsGo(t, nodeIn, nodeOut)
}
// only check from node to json
checkLeft := func(t *testing.T, j string, nodeIn enc.Node) {
t.Helper()
t.Logf("%s <== %#v", j, nodeIn)
jIn := []byte(j)
jOut := codec.Encode(c, nodeIn)
test.EqualsGo(t, string(jIn), string(jOut))
}
t.Run("scalars", func(t *testing.T) {
check(t, `null`, enc.Nil{})
checkLeft(t, `1`, enc.Integer(1))
checkLeft(t, `3.14`, enc.Float(3.14))
check(t, `3.14`, enc.Digits(`3.14`))
check(t, `3`, enc.Digits(`3`))
check(t, `true`, enc.Bool(true))
check(t, `"foo"`, enc.String("foo"))
checkLeft(t, `"AQID"`, enc.Bytes{1, 2, 3})
check(t, `"\""`, enc.String(`"`))
check(t, `"\\"`, enc.String(`\`))
check(t, `"\\\""`, enc.String(`\"`))
})
t.Run("maps", func(t *testing.T) {
check(t, `{}`, enc.Map{})
checkLeft(t, `{"one":3.14}`, enc.Map{"one": enc.Float(3.14)})
m := enc.Map{"one": enc.Integer(1), "nil": enc.Nil{}, "foo": enc.String("bar")}
j := codec.Encode(c, m)
test.ContainsJSON(t, j, `"nil":null`)
test.ContainsJSON(t, j, `"foo":"bar"`)
test.ContainsJSON(t, j, `"one":1`)
})
t.Run("pairs", func(t *testing.T) {
checkLeft(t, `{}`, enc.Pairs{})
checkLeft(t, `{"b":1,"a":2,"":null}`, enc.Pairs{{"b", enc.Integer(1)}, {"a", enc.Integer(2)}, {"", enc.Nil{}}})
})
t.Run("lists", func(t *testing.T) {
check(t, `[]`, enc.List{})
check(t, `[null]`, enc.List{enc.Nil{}})
check(t, `[1,"two",false]`, enc.List{enc.Integer(1), enc.String("two"), enc.Bool(false)})
})
t.Run("deep", func(t *testing.T) {
check(t, `[{}]`, enc.List{enc.Map{}})
check(t, `[[],null]`, enc.List{enc.List{}, enc.Nil{}})
check(t, `{"l":[]}`, enc.Map{"l": enc.List{}})
})
}
func TestRawNode(t *testing.T) {
c := test.Context(t)
t.Run("enc.Node", func(t *testing.T) {
var x struct {
S string `json:"s"`
X enc.Node `json:"x"` // you can use enc.Node to have access to the interstitial similarly to json.RawMessage
}
in := enc.Map{
"s": enc.String("foo"),
"x": enc.Map{
"ok": enc.Bool(true),
},
}
err := enc.Unmarshal(c, in, &x)
test.NoError(t, err)
test.EqualsGo(t, enc.Map{"ok": enc.Bool(true)}, x.X)
t.Logf("x: %+v", x)
})
t.Run("enc.Map", func(t *testing.T) {
var x struct {
S string `json:"s"`
X enc.Map `json:"x"` // you can use enc.Map to force the interstitial to be an object
}
in := enc.Map{
"s": enc.String("foo"),
"x": enc.Map{
"ok": enc.Bool(true),
},
}
err := enc.Unmarshal(c, in, &x)
test.NoError(t, err)
test.EqualsGo(t, enc.Map{"ok": enc.Bool(true)}, x.X)
t.Logf("x: %+v", x)
})
t.Run("enc.Map FAIL", func(t *testing.T) {
var x struct {
S string `json:"s"`
X enc.Map `json:"x"` // you can use enc.Map to force the interstitial to be an object
}
in := enc.Map{
"s": enc.String("foo"),
"x": enc.String("bar"), // will fail to unmarshal into map
}
err := enc.Unmarshal(c, in, &x)
test.Error(t, err)
t.Logf("OK!")
})
}
func TestNoData(t *testing.T) {
c := test.Context(t)
{
n, err := enc.JSON{}.Decode(c, nil)
test.NoError(t, err)
test.Nil(t, n)
}
{
n, err := enc.JSON{}.Decode(c, []byte{})
test.NoError(t, err)
test.Nil(t, n)
}
}
func TestBytesJSONCompat(t *testing.T) {
c := test.Context(t)
in := []byte{1, 2, 3}
n, err := enc.Marshal(c, in)
test.NoError(t, err)
test.EqualsGo(t, enc.Bytes{1, 2, 3}, n)
j, err := enc.MarshalJSON(c, in)
test.NoError(t, err)
test.EqualsStr(t, `"AQID"`, string(j))
var out []byte
err = enc.UnmarshalJSON(c, j, &out)
test.NoError(t, err)
test.EqualsGo(t, in, out)
}