-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_test.go
153 lines (137 loc) · 3.62 KB
/
list_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
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
package empty
import (
"bytes"
"encoding/json"
"testing"
)
type testStruct struct {
Name string `json:"naMe,omitempty"`
}
func TestEmpty_MarshalJSON(t *testing.T) {
emptyPrimitive := List[int]{}
m, err := json.Marshal(emptyPrimitive)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[]")) {
t.Error("Empty array should marshal with brackets")
}
var emptyPrimitivePointer List[int]
m, err = json.Marshal(emptyPrimitivePointer)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[]")) {
t.Error("Nil array should marshal with brackets")
}
emptyStruct := List[testStruct]{}
m, err = json.Marshal(emptyStruct)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[]")) {
t.Error("Empty array should marshal with brackets")
}
var emptyStructPointer List[int]
m, err = json.Marshal(emptyStructPointer)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[]")) {
t.Error("Nil array should marshal with brackets")
}
notEmptyPrimitive := List[int]{10, 42}
m, err = json.Marshal(notEmptyPrimitive)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[10,42]")) {
t.Error("Filled array should marshal normally")
}
notEmptyPrimitivePointer := &List[int]{10, 42}
m, err = json.Marshal(notEmptyPrimitivePointer)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte("[10,42]")) {
t.Error("Filled array pointer should marshal normally")
}
notEmptyStruct := List[testStruct]{{Name: "test1"}, {Name: ""}}
m, err = json.Marshal(notEmptyStruct)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte(`[{"naMe":"test1"},{}]`)) {
t.Error("Filled array should marshal normally")
}
notEmptyStructPointer := &List[testStruct]{{Name: "test1"}, {Name: ""}}
m, err = json.Marshal(notEmptyStructPointer)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m, []byte(`[{"naMe":"test1"},{}]`)) {
t.Error("Filled array pointer should marshal normally")
}
}
func TestEmpty_UnmarshalJSON(t *testing.T) {
emptyPrimitiveJson := []byte(`null`)
emptyPrimitive := &List[int]{}
err := json.Unmarshal(emptyPrimitiveJson, emptyPrimitive)
if err != nil {
t.Error(err)
}
if emptyPrimitive == nil {
t.Error("Null json array should return empty go slice")
}
bracketPrimitiveJson := []byte(`[]`)
bracketPrimitive := &List[int]{}
err = json.Unmarshal(bracketPrimitiveJson, bracketPrimitive)
if err != nil {
t.Error(err)
}
if emptyPrimitive == nil {
t.Error("Empty json array should return empty go slice")
}
primitiveJson := []byte(`[10,42]`)
primitive := &List[int]{}
err = json.Unmarshal(primitiveJson, primitive)
if err != nil {
t.Error(err)
}
if emptyPrimitive == nil {
t.Error("Empty json array should return empty go slice")
}
emptyStructJson := []byte(`null`)
emptyStruct := &List[testStruct]{}
err = json.Unmarshal(emptyStructJson, emptyStruct)
if err != nil {
t.Error(err)
}
if emptyPrimitive == nil {
t.Error("Null json array should return empty go slice")
}
bracketStructJson := []byte(`[]`)
bracketStruct := &List[testStruct]{}
err = json.Unmarshal(bracketStructJson, bracketStruct)
if err != nil {
t.Error(err)
}
if emptyPrimitive == nil {
t.Error("Empty json array should return empty go slice")
}
structJson := []byte(`[{"naMe": "test42"},{}]`)
parsedStruct := List[testStruct]{}
err = json.Unmarshal(structJson, &parsedStruct)
if err != nil {
t.Error(err)
}
if len(parsedStruct) != 2 {
t.Error("Json array must have len of 2")
}
if parsedStruct[0].Name != "test42" {
t.Error("Did not parse naMe field with value test42 correctly")
}
if parsedStruct[1].Name != "" {
t.Error("Did not parse naMe field with zero value correctly")
}
}