forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
178 lines (158 loc) · 3.96 KB
/
value_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
package null
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/guregu/null/v5/internal"
)
func TestValue(t *testing.T) {
testValue[string](t, "hello")
testValue[uint32](t, 1337)
testValue[uint64](t, 42)
type myint int
testValue[myint](t, 2)
}
func testValue[T any](t *testing.T, good T) {
t.Run(internal.TypeName[Value[T]](), func(t *testing.T) {
var zero T
var nilv *T
// valid Value[T]
testValueValid[T](t, good)
testValueValid[T](t, zero)
// invalid Value[T]
t.Run("null", func(t *testing.T) {
null := NewValue(zero, false)
if !null.IsZero() {
t.Errorf("%v IsZero() should be true", null)
}
nullVFP := ValueFromPtr(nilv)
if !reflect.DeepEqual(null, nullVFP) {
t.Errorf("%#v != %#v", null, nullVFP)
}
nullp := null.Ptr()
if nullp != nil {
t.Errorf("%#v Ptr() should be nil", null)
}
nullVOZ := null.ValueOrZero()
if !reflect.DeepEqual(nullVOZ, zero) {
t.Error("ValueOrZero() want:", zero, "got:", nullVOZ)
}
t.Run("MarshalJSON", func(t *testing.T) {
wantJSON, err := json.Marshal(nilv)
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal(null)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(wantJSON, got) {
t.Error("unexpected json. want:", string(wantJSON), "got:", string(got))
}
t.Run("UnmarshalJSON", func(t *testing.T) {
var want T
if err := json.Unmarshal(wantJSON, &want); err != nil {
t.Fatal(err)
}
var got Value[T]
if err := json.Unmarshal(wantJSON, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got.V) {
t.Error("bad unmarshal. want:", want, "got:", got)
}
if !got.IsZero() {
t.Errorf("%#v IsZero() should be true", got)
}
})
})
t.Run("Scan(nil)", func(t *testing.T) {
var want sql.Null[T]
if err := want.Scan(nil); err != nil {
t.Fatal(err)
}
var got Value[T]
if err := got.Scan(nil); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got.Null) {
t.Error("bad scan. want:", want, "got:", got)
}
})
t.Run(fmt.Sprintf("SetValid(%v)", zero), func(t *testing.T) {
valid2 := null
valid2.SetValid(zero)
if valid2.IsZero() {
t.Errorf("%#v IsZero() should be false", valid2)
}
})
})
})
}
func testValueValid[T any](t *testing.T, value T) {
valid := NewValue(value, true)
if valid.IsZero() {
t.Errorf("%#v IsZero() should be false", valid)
}
validVF := ValueFrom(value)
if !reflect.DeepEqual(valid, validVF) {
t.Errorf("%#v != %#v", valid, validVF)
}
validVFP := ValueFromPtr(&value)
if !reflect.DeepEqual(valid, validVFP) {
t.Errorf("%#v != %#v", valid, validVFP)
}
validp := valid.Ptr()
if validp == nil {
t.Errorf("%#v Ptr() shouldn't be nil", valid)
}
validVOZ := valid.ValueOrZero()
if !reflect.DeepEqual(validVOZ, value) {
t.Error("ValueOrZero() want:", value, "got:", validVOZ)
}
t.Run("MarshalJSON", func(t *testing.T) {
wantJSON, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
got, err := json.Marshal(valid)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(wantJSON, got) {
t.Error("unexpected json. want:", string(wantJSON), "got:", string(got))
}
t.Run("UnmarshalJSON", func(t *testing.T) {
var want T
if err := json.Unmarshal(wantJSON, &want); err != nil {
t.Fatal(err)
}
var got Value[T]
if err := json.Unmarshal(wantJSON, &got); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got.V) {
t.Error("bad unmarshal. want:", want, "got:", got)
}
if got.IsZero() {
t.Errorf("%#v IsZero() should be false", got)
}
})
})
t.Run(fmt.Sprintf("Scan(%v)", value), func(t *testing.T) {
var want sql.Null[T]
if err := want.Scan(value); err != nil {
t.Fatal(err)
}
var got Value[T]
if err := got.Scan(value); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(want, got.Null) {
t.Error("bad scan. want:", want, "got:", got)
}
})
}