-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
201 lines (191 loc) · 4.46 KB
/
json_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
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
package option
import (
"encoding/json"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"testing"
)
type S struct {
Foo Option[string] `json:"foo"`
Bar Option[string] `json:"bar"`
}
func TestOption_UnmarshalJSON(t *testing.T) {
type T = int
cases := []struct {
name string
jsonStr string
expected Option[T]
expectedErr bool
}{
{
name: "Test UnmarshalJSON with null value",
jsonStr: "null",
expected: None[T](),
expectedErr: false,
},
{
name: "Test UnmarshalJSON with normal value",
jsonStr: "123",
expected: Some[T](123),
expectedErr: false,
},
{
name: "Test UnmarshalJSON with invalid value",
jsonStr: "-",
expected: None[T](),
expectedErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var actual Option[T]
err := actual.UnmarshalJSON([]byte(tc.jsonStr))
if !tc.expectedErr {
if err != nil {
t.Errorf("UnmarshalJSON() error = %v", err)
}
// validate with standard method
assert.Equal(t, actual, tc.expected)
// validate with custom equality
if !cmp.Equal(actual, tc.expected) {
t.Errorf("UnmarshalJSON() = %v, want %v", actual, tc.expected)
}
} else if err == nil {
t.Error("UnmarshalJSON() expected error, but there is no error")
}
})
}
}
func TestOption_UnmarshalJSONStr(t *testing.T) {
type T = string
cases := []struct {
name string
jsonStr string
expected Option[T]
expectedErr bool
}{
{
name: "Test UnmarshalJSON with null value",
jsonStr: "null",
expected: None[T](),
expectedErr: false,
},
{
name: "Test UnmarshalJSON with normal value",
jsonStr: "\"option\"",
expected: Some[T]("option"),
expectedErr: false,
},
{
name: "Test UnmarshalJSON with invalid value",
jsonStr: "-",
expected: None[T](),
expectedErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var actual Option[T]
err := actual.UnmarshalJSON([]byte(tc.jsonStr))
if !tc.expectedErr {
if err != nil {
t.Errorf("UnmarshalJSON() error = %v", err)
}
// validate with standard method
assert.Equal(t, actual, tc.expected)
// validate with custom equality
if !cmp.Equal(actual, tc.expected) {
t.Errorf("UnmarshalJSON() = %v, want %v", actual, tc.expected)
}
} else if err == nil {
t.Error("UnmarshalJSON() expected error, but there is no error")
}
})
}
}
func TestOption_UnmarshalJSON_fromStruct(t *testing.T) {
cases := []struct {
name string
jsonStr string
expected S
expectedErr bool
}{
{
name: "Test struct with empty values",
jsonStr: "{\"foo\":null,\"bar\":null}",
expected: S{
Foo: None[string](),
Bar: None[string](),
},
expectedErr: false,
},
{
name: "Test struct with filled values",
jsonStr: "{\"foo\":\"bar\",\"bar\":\"baz\"}",
expected: S{
Foo: Some("bar"),
Bar: Some("baz"),
},
expectedErr: false,
},
{
name: "Test struct with invalid values",
jsonStr: "{\"foo\":1,\"bar\":\"baz\"}",
expected: S{
Foo: None[string](),
Bar: None[string](),
},
expectedErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
actual := S{}
t.Logf("actual = %v", actual)
err := json.Unmarshal([]byte(tc.jsonStr), &actual)
if !tc.expectedErr {
if err != nil {
t.Errorf("UnmarshalJSON() error = %v", err)
}
// validate with standard method
assert.Equal(t, actual, tc.expected)
// validate with custom equality
if !cmp.Equal(actual, tc.expected) {
t.Errorf("UnmarshalJSON() = %v, want %v", actual, tc.expected)
}
} else if err == nil {
t.Error("UnmarshalJSON() expected error, but there is no error")
}
})
}
}
func TestOption_MarshalJSON(t *testing.T) {
type T = int
cases := []struct {
name string
obj Option[T]
expected string
}{
{
name: "Test MarshalJSON with None value",
obj: None[T](),
expected: "null",
},
{
name: "Test MarshalJSON with Some value",
obj: Some[T](123),
expected: "123",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
actual, err := tc.obj.MarshalJSON()
if err != nil {
t.Errorf("MarshalJSON() error = %v", err)
}
if string(actual) != tc.expected {
t.Errorf("MarshalJSON() = %v, want %v", string(actual), tc.expected)
}
})
}
}