Debian Codegraph @

int_test.go

raw symbols SCIP
1 package null
2
3 import (
4 "encoding/json"
5 "math"
6 "strconv"
7 "testing"
8 )
9
10 var (
11 intJSON = []byte(`12345`)
12 nullIntJSON = []byte(`{"Int64":12345,"Valid":true}`)
13 )
14
15 func TestIntFrom(t *testing.T) {
16 i := IntFrom(12345)
17 assertInt(t, i, "IntFrom()")
18
19 zero := IntFrom(0)
20 if !zero.Valid {
21 t.Error("IntFrom(0)", "is invalid, but should be valid")
22 }
23 }
24
25 func TestIntFromPtr(t *testing.T) {
26 n := int64(12345)
27 iptr := &n
28 i := IntFromPtr(iptr)
29 assertInt(t, i, "IntFromPtr()")
30
31 null := IntFromPtr(nil)
32 assertNullInt(t, null, "IntFromPtr(nil)")
33 }
34
35 func TestUnmarshalInt(t *testing.T) {
36 var i Int
37 err := json.Unmarshal(intJSON, &i)
38 maybePanic(err)
39 assertInt(t, i, "int json")
40
41 var ni Int
42 err = json.Unmarshal(nullIntJSON, &ni)
43 maybePanic(err)
44 assertInt(t, ni, "sq.NullInt64 json")
45
46 var null Int
47 err = json.Unmarshal(nullJSON, &null)
48 maybePanic(err)
49 assertNullInt(t, null, "null json")
50
51 var badType Int
52 err = json.Unmarshal(boolJSON, &badType)
53 if err == nil {
54 panic("err should not be nil")
55 }
56 assertNullInt(t, badType, "wrong type json")
57
58 var invalid Int
59 err = invalid.UnmarshalJSON(invalidJSON)
60 if _, ok := err.(*json.SyntaxError); !ok {
61 t.Errorf("expected json.SyntaxError, not %T", err)
62 }
63 assertNullInt(t, invalid, "invalid json")
64 }
65
66 func TestUnmarshalNonIntegerNumber(t *testing.T) {
67 var i Int
68 err := json.Unmarshal(floatJSON, &i)
69 if err == nil {
70 panic("err should be present; non-integer number coerced to int")
71 }
72 }
73
74 func TestUnmarshalInt64Overflow(t *testing.T) {
75 int64Overflow := uint64(math.MaxInt64)
76
77 // Max int64 should decode successfully
78 var i Int
79 err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
80 maybePanic(err)
81
82 // Attempt to overflow
83 int64Overflow++
84 err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
85 if err == nil {
86 panic("err should be present; decoded value overflows int64")
87 }
88 }
89
90 func TestTextUnmarshalInt(t *testing.T) {
91 var i Int
92 err := i.UnmarshalText([]byte("12345"))
93 maybePanic(err)
94 assertInt(t, i, "UnmarshalText() int")
95
96 var blank Int
97 err = blank.UnmarshalText([]byte(""))
98 maybePanic(err)
99 assertNullInt(t, blank, "UnmarshalText() empty int")
100
101 var null Int
102 err = null.UnmarshalText([]byte("null"))
103 maybePanic(err)
104 assertNullInt(t, null, `UnmarshalText() "null"`)
105 }
106
107 func TestMarshalInt(t *testing.T) {
108 i := IntFrom(12345)
109 data, err := json.Marshal(i)
110 maybePanic(err)
111 assertJSONEquals(t, data, "12345", "non-empty json marshal")
112
113 // invalid values should be encoded as null
114 null := NewInt(0, false)
115 data, err = json.Marshal(null)
116 maybePanic(err)
117 assertJSONEquals(t, data, "null", "null json marshal")
118 }
119
120 func TestMarshalIntText(t *testing.T) {
121 i := IntFrom(12345)
122 data, err := i.MarshalText()
123 maybePanic(err)
124 assertJSONEquals(t, data, "12345", "non-empty text marshal")
125
126 // invalid values should be encoded as null
127 null := NewInt(0, false)
128 data, err = null.MarshalText()
129 maybePanic(err)
130 assertJSONEquals(t, data, "", "null text marshal")
131 }
132
133 func TestIntPointer(t *testing.T) {
134 i := IntFrom(12345)
135 ptr := i.Ptr()
136 if *ptr != 12345 {
137 t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345)
138 }
139
140 null := NewInt(0, false)
141 ptr = null.Ptr()
142 if ptr != nil {
143 t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
144 }
145 }
146
147 func TestIntIsZero(t *testing.T) {
148 i := IntFrom(12345)
149 if i.IsZero() {
150 t.Errorf("IsZero() should be false")
151 }
152
153 null := NewInt(0, false)
154 if !null.IsZero() {
155 t.Errorf("IsZero() should be true")
156 }
157
158 zero := NewInt(0, true)
159 if zero.IsZero() {
160 t.Errorf("IsZero() should be false")
161 }
162 }
163
164 func TestIntSetValid(t *testing.T) {
165 change := NewInt(0, false)
166 assertNullInt(t, change, "SetValid()")
167 change.SetValid(12345)
168 assertInt(t, change, "SetValid()")
169 }
170
171 func TestIntScan(t *testing.T) {
172 var i Int
173 err := i.Scan(12345)
174 maybePanic(err)
175 assertInt(t, i, "scanned int")
176
177 var null Int
178 err = null.Scan(nil)
179 maybePanic(err)
180 assertNullInt(t, null, "scanned null")
181 }
182
183 func assertInt(t *testing.T, i Int, from string) {
184 if i.Int64 != 12345 {
185 t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345)
186 }
187 if !i.Valid {
188 t.Error(from, "is invalid, but should be valid")
189 }
190 }
191
192 func assertNullInt(t *testing.T, i Int, from string) {
193 if i.Valid {
194 t.Error(from, "is valid, but should be invalid")
195 }
196 }