-
Notifications
You must be signed in to change notification settings - Fork 44
/
xml_test.go
342 lines (305 loc) · 8.72 KB
/
xml_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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package recurly_test
import (
"encoding/json"
"encoding/xml"
"testing"
"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
)
func TestXML_NullBool(t *testing.T) {
t.Run("ZeroValue", func(t *testing.T) {
var b recurly.NullBool
if value, ok := b.Value(); ok {
t.Fatal("expected ok to be false")
} else if value != false {
t.Fatal("expected false")
}
if b.Bool() != false {
t.Fatalf("unexpected value: %t", b.Bool())
} else if ptr := b.BoolPtr(); ptr != nil {
t.Fatalf("expected nil: %#v", ptr)
}
})
t.Run("True", func(t *testing.T) {
b := recurly.NewBool(true)
if value, ok := b.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != true {
t.Fatal("expected true")
}
if b.Bool() != true {
t.Fatalf("unexpected value: %t", b.Bool())
} else if ptr := b.BoolPtr(); ptr == nil {
t.Fatal("expected non-nil value")
} else if *ptr != true {
t.Fatalf("unexpected value: %#v", ptr)
}
})
t.Run("False", func(t *testing.T) {
b := recurly.NewBool(false)
if value, ok := b.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != false {
t.Fatal("expected false")
}
if b.Bool() != false {
t.Fatalf("unexpected value: %t", b.Bool())
} else if ptr := b.BoolPtr(); ptr == nil {
t.Fatal("expected non-nil value")
} else if *ptr != false {
t.Fatalf("unexpected value: %#v", ptr)
}
})
type testStruct struct {
XMLName xml.Name `xml:"test"`
Value recurly.NullBool `xml:"b"`
}
t.Run("Encode", func(t *testing.T) {
for i, tt := range []struct {
value recurly.NullBool
expect string
}{
{value: recurly.NewBool(true), expect: `<test><b>true</b></test>`},
{value: recurly.NewBool(false), expect: `<test><b>false</b></test>`},
{expect: `<test></test>`}, // zero value
} {
if xml, err := xml.Marshal(testStruct{Value: tt.value}); err != nil {
t.Fatalf("%d %#v", i, err)
} else if string(xml) != tt.expect {
t.Fatalf("%d %s", i, string(xml))
}
}
})
t.Run("Decode", func(t *testing.T) {
for i, tt := range []struct {
expect recurly.NullBool
input string
}{
{expect: recurly.NewBool(true), input: `<test><b>true</b></test>`},
{expect: recurly.NewBool(false), input: `<test><b>false</b></test>`},
{input: `<test></test>`},
} {
var dst testStruct
if err := xml.Unmarshal([]byte(tt.input), &dst); err != nil {
t.Fatalf("%d %#v", i, err)
} else if diff := cmp.Diff(testStruct{XMLName: xml.Name{Local: "test"}, Value: tt.expect}, dst); diff != "" {
t.Fatalf("%d %s", i, diff)
}
}
})
t.Run("JSON", func(t *testing.T) {
for i, tt := range []struct {
b recurly.NullBool
expect string
}{
{b: recurly.NewBool(true), expect: "true"},
{b: recurly.NewBool(false), expect: "false"},
{expect: "null"}, // zero value
} {
if b, err := json.Marshal(tt.b); err != nil {
t.Fatalf("%d %#v", i, err)
} else if string(b) != tt.expect {
t.Fatalf("%d %s", i, string(b))
}
}
})
}
func TestXML_NullBoolPtr(t *testing.T) {
boolVal := true
b := recurly.NewBoolPtr(&boolVal)
if value, ok := b.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != true {
t.Fatal("expected true")
}
}
func TestXML_NullInt(t *testing.T) {
t.Run("ZeroValue", func(t *testing.T) {
var i recurly.NullInt
if value, ok := i.Value(); ok {
t.Fatal("expected ok to be false")
} else if value != 0 {
t.Fatalf("unexpected value: %d", value)
}
if i.Int() != 0 {
t.Fatalf("unexpected value: %d", i.Int())
} else if ptr := i.IntPtr(); ptr != nil {
t.Fatalf("expected nil: %#v", ptr)
}
})
i := recurly.NewInt(1)
if value, ok := i.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != 1 {
t.Fatalf("unexpected value: %d", value)
}
if i.Int() != 1 {
t.Fatalf("unexpected value: %d", i.Int())
} else if ptr := i.IntPtr(); ptr == nil {
t.Fatal("expected non-nil value")
} else if *ptr != 1 {
t.Fatalf("unexpected value: %#v", ptr)
}
i = recurly.NewInt(0)
if value, ok := i.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != 0 {
t.Fatalf("unexpected value: %d", value)
}
if i.Int() != 0 {
t.Fatalf("unexpected value: %d", i.Int())
} else if ptr := i.IntPtr(); ptr == nil {
t.Fatal("expected non-nil value")
} else if *ptr != 0 {
t.Fatalf("unexpected value: %#v", ptr)
}
type testStruct struct {
XMLName xml.Name `xml:"test"`
Value recurly.NullInt `xml:"i"`
}
t.Run("Encode", func(t *testing.T) {
for i, tt := range []struct {
value recurly.NullInt
expect string
}{
{value: recurly.NewInt(1), expect: `<test><i>1</i></test>`},
{value: recurly.NewInt(0), expect: `<test><i>0</i></test>`},
{value: recurly.NewInt(-1), expect: `<test><i>-1</i></test>`},
{expect: `<test></test>`}, // zero value
} {
if xml, err := xml.Marshal(testStruct{Value: tt.value}); err != nil {
t.Fatalf("%d %#v", i, err)
} else if string(xml) != tt.expect {
t.Fatalf("%d %s", i, string(xml))
}
}
})
t.Run("Decode", func(t *testing.T) {
for i, tt := range []struct {
expect recurly.NullInt
input string
}{
{expect: recurly.NewInt(1), input: `<test><i>1</i></test>`},
{expect: recurly.NewInt(0), input: `<test><i>0</i></test>`},
{input: `<test></test>`}, // zero value
} {
var dst testStruct
if err := xml.Unmarshal([]byte(tt.input), &dst); err != nil {
t.Fatalf("%d %#v", i, err)
} else if diff := cmp.Diff(testStruct{XMLName: xml.Name{Local: "test"}, Value: tt.expect}, dst); diff != "" {
t.Fatalf("%d %s", i, diff)
}
}
})
}
func TestXML_NullIntPtr(t *testing.T) {
i := recurly.NewIntPtr(nil)
if value, ok := i.Value(); ok {
t.Fatal("expected ok to be false")
} else if value != 0 {
t.Fatalf("unexpected value: %d", value)
}
intVal := 1
i = recurly.NewIntPtr(&intVal)
if value, ok := i.Value(); !ok {
t.Fatal("expected ok to be true")
} else if value != 1 {
t.Fatalf("unexpected value: %d", value)
}
}
func TestXML_NullTime(t *testing.T) {
t.Run("ZeroValue", func(t *testing.T) {
var rt recurly.NullTime
if value, ok := rt.Value(); ok {
t.Fatal("expected ok to be false")
} else if !value.IsZero() {
t.Fatalf("expected zero time: %s", value.String())
}
if !rt.Time().IsZero() {
t.Fatalf("unexpected value: %#v", rt.Time())
} else if ptr := rt.TimePtr(); ptr != nil {
t.Fatalf("expected nil value: %#v", ptr)
}
})
v := MustParseTime("2011-10-25T12:00:00Z")
rt := recurly.NewTime(v)
if value, ok := rt.Value(); !ok {
t.Fatal("expected ok to be true")
} else if !value.Equal(v) {
t.Fatalf("unexpected value: %v", value)
}
if !rt.Time().Equal(v) {
t.Fatalf("unexpected value: %#v", rt.Time())
} else if ptr := rt.TimePtr(); ptr == nil {
t.Fatal("expected non-nil value")
} else if !ptr.Equal(v) {
t.Fatalf("unexpected value: %#v", ptr)
}
t.Run("Encode", func(t *testing.T) {
// Value
if b, err := xml.Marshal(struct {
XMLName xml.Name `xml:"test"`
Time recurly.NullTime `xml:"time"`
}{
Time: recurly.NewTime(v),
}); err != nil {
t.Fatal(err)
} else if string(b) != `<test><time>2011-10-25T12:00:00Z</time></test>` {
t.Fatal(string(b))
}
// No value.
if b, err := xml.Marshal(struct {
XMLName xml.Name `xml:"test"`
Time recurly.NullTime `xml:"time"`
}{
Time: recurly.NullTime{},
}); err != nil {
t.Fatal(err)
} else if string(b) != `<test></test>` {
t.Fatal(string(b))
}
})
t.Run("Decode", func(t *testing.T) {
// Value
var s struct {
XMLName xml.Name `xml:"test"`
Time recurly.NullTime `xml:"time"`
}
if err := xml.Unmarshal([]byte(`<test><time>2011-10-25T12:00:00Z</time></test>`), &s); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(s.Time, recurly.NewTime(v)); diff != "" {
t.Fatal(diff)
}
// Reset and try no value.
s.XMLName = xml.Name{}
s.Time = recurly.NullTime{}
if err := xml.Unmarshal([]byte(`<test></test>`), &s); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(s.Time, recurly.NullTime{}); diff != "" {
t.Fatal(diff)
}
// Reset and try with tag but no value. Recurly often uses this format.
s.XMLName = xml.Name{}
s.Time = recurly.NullTime{}
if err := xml.Unmarshal([]byte(`<test><time nil="nil"/></test>`), &s); err != nil {
t.Fatal(err)
} else if diff := cmp.Diff(s.Time, recurly.NullTime{}); diff != "" {
t.Fatal(diff)
}
})
}
func TestXML_NullTimePtr(t *testing.T) {
rt := recurly.NewTimePtr(nil)
if value, ok := rt.Value(); ok {
t.Fatal("expected ok to be false")
} else if !value.IsZero() {
t.Fatalf("expected zero time: %s", value.String())
}
v := MustParseTime("2011-10-25T12:00:00Z")
rt = recurly.NewTimePtr(&v)
if value, ok := rt.Value(); !ok {
t.Fatal("expected ok to be true")
} else if !value.Equal(v) {
t.Fatalf("unexpected value: %v", value)
}
}