forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
323 lines (272 loc) · 6.89 KB
/
bool_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
package null
import (
"encoding/json"
"errors"
"testing"
"github.com/mailru/easyjson"
"github.com/stretchr/testify/assert"
)
var (
boolJSON = []byte(`true`)
nullBoolJSON = []byte(`{"Bool":true,"Valid":true}`)
)
func TestBoolFrom(t *testing.T) {
b := BoolFrom(true)
assertBool(t, b, "BoolFrom()")
zero := BoolFrom(false)
if !zero.Valid {
t.Error("BoolFrom(false)", "is invalid, but should be valid")
}
}
func TestBoolFromPtr(t *testing.T) {
n := true
bptr := &n
b := BoolFromPtr(bptr)
assertBool(t, b, "BoolFromPtr()")
bptr = nil
null := BoolFromPtr(bptr)
assertNullBool(t, null, "BoolFromPtr(nil)")
}
func TestUnmarshalBool(t *testing.T) {
var b Bool
err := json.Unmarshal(boolJSON, &b)
maybePanic(err)
assertBool(t, b, "bool json")
var nb Bool
err = json.Unmarshal(nullBoolJSON, &nb)
maybePanic(err)
var null Bool
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullBool(t, null, "null json")
var empty Bool
var data []byte
err = empty.UnmarshalJSON(data)
maybePanic(err)
assertNullBool(t, empty, "null json")
var badType Bool
err = json.Unmarshal(intJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullBool(t, badType, "wrong type json")
var invalid Bool
err = invalid.UnmarshalJSON(invalidJSON)
var syntaxError *json.SyntaxError
if !errors.As(err, &syntaxError) {
t.Errorf("expected wrapped json.SyntaxError, not %T", err)
}
}
func TestTextUnmarshalBool(t *testing.T) {
var b Bool
err := b.UnmarshalText([]byte("true"))
maybePanic(err)
assertBool(t, b, "UnmarshalText() bool")
var zero Bool
err = zero.UnmarshalText([]byte("false"))
maybePanic(err)
assertFalseBool(t, zero, "UnmarshalText() false")
var blank Bool
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullBool(t, blank, "UnmarshalText() empty bool")
var null Bool
err = null.UnmarshalText(nullLiteral)
maybePanic(err)
assertNullBool(t, null, `UnmarshalText() "null"`)
var invalid Bool
err = invalid.UnmarshalText([]byte(":D"))
if err == nil {
panic("err should not be nil")
}
assertNullBool(t, invalid, "invalid json")
}
func TestMarshalBool(t *testing.T) {
b := BoolFrom(true)
data, err := json.Marshal(b)
maybePanic(err)
assertJSONEquals(t, data, "true", "non-empty json marshal")
zero := NewBool(false, true)
data, err = json.Marshal(zero)
maybePanic(err)
assertJSONEquals(t, data, "false", "zero json marshal")
// invalid values should be encoded as null
null := NewBool(false, false)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, "null", "null json marshal")
}
func TestMarshalBoolText(t *testing.T) {
b := BoolFrom(true)
data, err := b.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "true", "non-empty text marshal")
zero := NewBool(false, true)
data, err = zero.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "false", "zero text marshal")
// invalid values should be encoded as null
null := NewBool(false, false)
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "null text marshal")
}
func TestBoolPointer(t *testing.T) {
b := BoolFrom(true)
ptr := b.Ptr()
if *ptr != true {
t.Errorf("bad %s bool: %#v ≠ %v\n", "pointer", ptr, true)
}
null := NewBool(false, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s bool: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestBoolIsZero(t *testing.T) {
b := BoolFrom(true)
if b.IsZero() {
t.Errorf("IsZero() should be false")
}
null := NewBool(false, false)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
zero := NewBool(false, true)
if zero.IsZero() {
t.Errorf("IsZero() should be false")
}
}
func TestBoolSetValid(t *testing.T) {
change := NewBool(false, false)
assertNullBool(t, change, "SetValid()")
change.SetValid(true)
assertBool(t, change, "SetValid()")
}
func TestBoolScan(t *testing.T) {
var b Bool
err := b.Scan(true)
maybePanic(err)
assertBool(t, b, "scanned bool")
var null Bool
err = null.Scan(nil)
maybePanic(err)
assertNullBool(t, null, "scanned null")
}
func TestBoolValueOrZero(t *testing.T) {
valid := NewBool(true, true)
if valid.ValueOrZero() != true {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}
invalid := NewBool(true, false)
if invalid.ValueOrZero() != false {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}
func TestBoolEqual(t *testing.T) {
b1 := NewBool(true, false)
b2 := NewBool(true, false)
assertBoolEqualIsTrue(t, b1, b2)
b1 = NewBool(true, false)
b2 = NewBool(false, false)
assertBoolEqualIsTrue(t, b1, b2)
b1 = NewBool(true, true)
b2 = NewBool(true, true)
assertBoolEqualIsTrue(t, b1, b2)
b1 = NewBool(true, true)
b2 = NewBool(true, false)
assertBoolEqualIsFalse(t, b1, b2)
b1 = NewBool(true, false)
b2 = NewBool(true, true)
assertBoolEqualIsFalse(t, b1, b2)
b1 = NewBool(true, true)
b2 = NewBool(false, true)
assertBoolEqualIsFalse(t, b1, b2)
}
func TestUnderlyingBool(t *testing.T) {
type foo bool
const val bool = true
f := foo(val)
nullF := B(f)
if !nullF.Valid {
t.Fatalf("expected the null.Bool to be valid")
}
if act := nullF.Bool; val != act {
t.Fatalf("expected %v, but given %v", val, act)
}
}
func assertBool(t *testing.T, b Bool, from string) {
if b.Bool != true {
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, true)
}
if !b.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertFalseBool(t *testing.T, b Bool, from string) {
if b.Bool != false {
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, false)
}
if !b.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullBool(t *testing.T, b Bool, from string) {
if b.Valid {
t.Error(from, "is valid, but should be invalid")
}
}
func TestBoolUnmarshalEasyJSON(t *testing.T) {
tests := []struct {
data string
exp Bool
}{
{
data: "null",
},
{
data: `true`,
exp: BoolFrom(true),
},
{
data: `false`,
exp: BoolFrom(false),
},
{
data: `{"Bool":false,"Valid":true}`,
exp: BoolFrom(false),
},
{
data: `{"bool":true,"valid":true}`,
exp: BoolFrom(true),
},
}
for _, test := range tests {
t.Run(test.data, func(t *testing.T) {
var b1 Bool
assert.NoError(t, easyjson.Unmarshal([]byte(test.data), &b1))
assert.Equal(t, test.exp, b1)
var b2 Bool
assert.NoError(t, json.Unmarshal([]byte(test.data), &b2))
assert.Equal(t, test.exp, b2)
})
}
}
func assertBoolEqualIsTrue(t *testing.T, a, b Bool) {
t.Helper()
if !a.Equal(b) {
t.Errorf("Equal() of Bool{%t, Valid:%t} and Bool{%t, Valid:%t} should return true", a.Bool, a.Valid, b.Bool, b.Valid)
}
}
func assertBoolEqualIsFalse(t *testing.T, a, b Bool) {
t.Helper()
if a.Equal(b) {
t.Errorf("Equal() of Bool{%t, Valid:%t} and Bool{%t, Valid:%t} should return false", a.Bool, a.Valid, b.Bool, b.Valid)
}
}
func TestTrueFalse(t *testing.T) {
assert.True(t, True().Valid)
assert.True(t, True().Bool)
assert.True(t, False().Valid)
assert.False(t, False().Bool)
}