-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompress_test.go
More file actions
229 lines (214 loc) · 4.79 KB
/
decompress_test.go
File metadata and controls
229 lines (214 loc) · 4.79 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package jsoncompressor
import (
"testing"
)
func TestDecompress(t *testing.T) {
{
type MyStruct struct {
K1 int `json:"k1"`
K2 string `json:"k2"`
K3 []int `json:"k3"`
K5 *struct {
K6 int `json:"k6"`
K8 string `json:"k8"`
} `json:"k5"`
}
raw := []byte(`[1,"2",[3,4],[7,"8"]]`)
var data MyStruct
err := Unmarshal(raw, &data)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshaled: %+v", data)
if data.K1 != 1 || data.K2 != "2" || len(data.K3) != 2 || data.K3[0] != 3 || data.K3[1] != 4 || data.K5.K6 != 7 || data.K5.K8 != "8" {
t.Fatalf("Unmarshaled data is invalid")
}
dataV2 := &MyStruct{}
err = Unmarshal(raw, &dataV2)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshaled: %+v", dataV2)
}
{
raw := []byte(`1`)
var data int
err := Unmarshal(raw, &data)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshaled: %+v", data)
}
{
raw := []byte(`["test"]`)
var data []string
err := Unmarshal(raw, &data)
if err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshaled: %v", data)
}
}
func TestDecompressTagWithoutNameAndLengthMismatch(t *testing.T) {
type S struct {
A int `json:",omitempty"`
B int `json:"b"`
}
{
raw := []byte(`[10,20]`)
var s S
if err := Unmarshal(raw, &s); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if s.A != 10 || s.B != 20 {
t.Fatalf("Unexpected values: %+v", s)
}
}
{
raw := []byte(`[1,2,3]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error on length mismatch (too many)")
}
}
{
raw := []byte(`[1]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error on length mismatch (too few)")
}
}
}
func TestDecompressPointerInitialization(t *testing.T) {
type Child struct {
A int `json:"a"`
}
type S struct {
Child *Child `json:"child"`
}
raw := []byte(`[[1]]`)
var s S
if err := Unmarshal(raw, &s); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if s.Child == nil || s.Child.A != 1 {
t.Fatalf("Unexpected values: %+v", s)
}
}
func TestDecompressTypeMismatch(t *testing.T) {
type S struct {
A int `json:"a"`
}
raw := []byte(`["oops"]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error on type mismatch")
}
}
func TestDecompressUnsupportedKind(t *testing.T) {
type S struct {
C chan int `json:"c"`
}
raw := []byte(`[1]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error for unsupported kind")
}
}
func TestDecompressTargetValidation(t *testing.T) {
if err := Unmarshal([]byte(`1`), 1); err == nil {
t.Fatalf("expected error for non-pointer target")
}
var ptr *int
if err := Unmarshal([]byte(`1`), ptr); err == nil {
t.Fatalf("expected error for nil pointer target")
}
}
func TestDecompressNullPointerField(t *testing.T) {
type S struct {
A *int `json:"a"`
}
raw := []byte(`[null]`)
var s S
if err := Unmarshal(raw, &s); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if s.A != nil {
t.Fatalf("expected nil pointer, got: %+v", s.A)
}
}
func TestDecompressInterfaceAssignment(t *testing.T) {
type S struct {
A any `json:"a"`
}
raw := []byte(`["hello"]`)
var s S
if err := Unmarshal(raw, &s); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if s.A != "hello" {
t.Fatalf("unexpected interface value: %+v", s.A)
}
}
func TestDecompressSliceElementMismatch(t *testing.T) {
type S struct {
A []int `json:"a"`
}
raw := []byte(`[["oops"]]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error for slice element mismatch")
}
}
func TestDecompressUnsignedNegative(t *testing.T) {
type S struct {
A uint `json:"a"`
}
raw := []byte(`[-1]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error for negative uint value")
}
}
func TestDecompressNoTaggedFields(t *testing.T) {
type S struct {
A int
B string
}
{
raw := []byte(`[]`)
var s S
if err := Unmarshal(raw, &s); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
}
{
raw := []byte(`[1]`)
var s S
if err := Unmarshal(raw, &s); err == nil {
t.Fatalf("expected error for data without tagged fields")
}
}
}
func TestDecompressMap(t *testing.T) {
{
raw := []byte(`{"a":1,"b":2}`)
var out map[string]int
if err := Unmarshal(raw, &out); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if len(out) != 2 || out["a"] != 1 || out["b"] != 2 {
t.Fatalf("unexpected map values: %+v", out)
}
}
{
raw := []byte(`{"nums":[1,2]}`)
var out map[string][]int
if err := Unmarshal(raw, &out); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
if len(out["nums"]) != 2 || out["nums"][0] != 1 || out["nums"][1] != 2 {
t.Fatalf("unexpected map values: %+v", out)
}
}
}