| 1 |
package zero |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"math" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
var ( |
| 11 |
intJSON = []byte(`12345`) |
| 12 |
intStringJSON = []byte(`"12345"`) |
| 13 |
nullIntJSON = []byte(`{"Int64":12345,"Valid":true}`) |
| 14 |
zeroJSON = []byte(`0`) |
| 15 |
) |
| 16 |
|
| 17 |
func TestIntFrom(t *testing.T) { |
| 18 |
i := IntFrom(12345) |
| 19 |
assertInt(t, i, "IntFrom()") |
| 20 |
|
| 21 |
zero := IntFrom(0) |
| 22 |
if zero.Valid { |
| 23 |
t.Error("IntFrom(0)", "is valid, but should be invalid") |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestIntFromPtr(t *testing.T) { |
| 28 |
n := int64(12345) |
| 29 |
iptr := &n |
| 30 |
i := IntFromPtr(iptr) |
| 31 |
assertInt(t, i, "IntFromPtr()") |
| 32 |
|
| 33 |
null := IntFromPtr(nil) |
| 34 |
assertNullInt(t, null, "IntFromPtr(nil)") |
| 35 |
} |
| 36 |
|
| 37 |
func TestUnmarshalInt(t *testing.T) { |
| 38 |
var i Int |
| 39 |
err := json.Unmarshal(intJSON, &i) |
| 40 |
maybePanic(err) |
| 41 |
assertInt(t, i, "int json") |
| 42 |
|
| 43 |
var si Int |
| 44 |
err = json.Unmarshal(intStringJSON, &si) |
| 45 |
maybePanic(err) |
| 46 |
assertInt(t, si, "int string json") |
| 47 |
|
| 48 |
var ni Int |
| 49 |
err = json.Unmarshal(nullIntJSON, &ni) |
| 50 |
maybePanic(err) |
| 51 |
assertInt(t, ni, "sql.NullInt64 json") |
| 52 |
|
| 53 |
var bi Int |
| 54 |
err = json.Unmarshal(floatBlankJSON, &bi) |
| 55 |
maybePanic(err) |
| 56 |
assertNullInt(t, bi, "blank json string") |
| 57 |
|
| 58 |
var zero Int |
| 59 |
err = json.Unmarshal(zeroJSON, &zero) |
| 60 |
maybePanic(err) |
| 61 |
assertNullInt(t, zero, "zero json") |
| 62 |
|
| 63 |
var null Int |
| 64 |
err = json.Unmarshal(nullJSON, &null) |
| 65 |
maybePanic(err) |
| 66 |
assertNullInt(t, null, "null json") |
| 67 |
|
| 68 |
var badType Int |
| 69 |
err = json.Unmarshal(boolJSON, &badType) |
| 70 |
if err == nil { |
| 71 |
panic("err should not be nil") |
| 72 |
} |
| 73 |
assertNullInt(t, badType, "wrong type json") |
| 74 |
|
| 75 |
var invalid Int |
| 76 |
err = invalid.UnmarshalJSON(invalidJSON) |
| 77 |
if _, ok := err.(*json.SyntaxError); !ok { |
| 78 |
t.Errorf("expected json.SyntaxError, not %T", err) |
| 79 |
} |
| 80 |
assertNullInt(t, invalid, "invalid json") |
| 81 |
} |
| 82 |
|
| 83 |
func TestUnmarshalNonIntegerNumber(t *testing.T) { |
| 84 |
var i Int |
| 85 |
err := json.Unmarshal(floatJSON, &i) |
| 86 |
if err == nil { |
| 87 |
panic("err should be present; non-integer number coerced to int") |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestUnmarshalInt64Overflow(t *testing.T) { |
| 92 |
int64Overflow := uint64(math.MaxInt64) |
| 93 |
|
| 94 |
|
| 95 |
var i Int |
| 96 |
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) |
| 97 |
maybePanic(err) |
| 98 |
|
| 99 |
|
| 100 |
int64Overflow++ |
| 101 |
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i) |
| 102 |
if err == nil { |
| 103 |
panic("err should be present; decoded value overflows int64") |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func TestTextUnmarshalInt(t *testing.T) { |
| 108 |
var i Int |
| 109 |
err := i.UnmarshalText([]byte("12345")) |
| 110 |
maybePanic(err) |
| 111 |
assertInt(t, i, "UnmarshalText() int") |
| 112 |
|
| 113 |
var zero Int |
| 114 |
err = zero.UnmarshalText([]byte("0")) |
| 115 |
maybePanic(err) |
| 116 |
assertNullInt(t, zero, "UnmarshalText() zero int") |
| 117 |
|
| 118 |
var blank Int |
| 119 |
err = blank.UnmarshalText([]byte("")) |
| 120 |
maybePanic(err) |
| 121 |
assertNullInt(t, blank, "UnmarshalText() empty int") |
| 122 |
|
| 123 |
var null Int |
| 124 |
err = null.UnmarshalText([]byte("null")) |
| 125 |
maybePanic(err) |
| 126 |
assertNullInt(t, null, `UnmarshalText() "null"`) |
| 127 |
} |
| 128 |
|
| 129 |
func TestMarshalInt(t *testing.T) { |
| 130 |
i := IntFrom(12345) |
| 131 |
data, err := json.Marshal(i) |
| 132 |
maybePanic(err) |
| 133 |
assertJSONEquals(t, data, "12345", "non-empty json marshal") |
| 134 |
|
| 135 |
|
| 136 |
null := NewInt(0, false) |
| 137 |
data, err = json.Marshal(null) |
| 138 |
maybePanic(err) |
| 139 |
assertJSONEquals(t, data, "0", "null json marshal") |
| 140 |
} |
| 141 |
|
| 142 |
func TestMarshalIntText(t *testing.T) { |
| 143 |
i := IntFrom(12345) |
| 144 |
data, err := i.MarshalText() |
| 145 |
maybePanic(err) |
| 146 |
assertJSONEquals(t, data, "12345", "non-empty text marshal") |
| 147 |
|
| 148 |
|
| 149 |
null := NewInt(0, false) |
| 150 |
data, err = null.MarshalText() |
| 151 |
maybePanic(err) |
| 152 |
assertJSONEquals(t, data, "0", "null text marshal") |
| 153 |
} |
| 154 |
|
| 155 |
func TestIntPointer(t *testing.T) { |
| 156 |
i := IntFrom(12345) |
| 157 |
ptr := i.Ptr() |
| 158 |
if *ptr != 12345 { |
| 159 |
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 12345) |
| 160 |
} |
| 161 |
|
| 162 |
null := NewInt(0, false) |
| 163 |
ptr = null.Ptr() |
| 164 |
if ptr != nil { |
| 165 |
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil") |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestIntIsZero(t *testing.T) { |
| 170 |
i := IntFrom(12345) |
| 171 |
if i.IsZero() { |
| 172 |
t.Errorf("IsZero() should be false") |
| 173 |
} |
| 174 |
|
| 175 |
null := NewInt(0, false) |
| 176 |
if !null.IsZero() { |
| 177 |
t.Errorf("IsZero() should be true") |
| 178 |
} |
| 179 |
|
| 180 |
zero := NewInt(0, true) |
| 181 |
if !zero.IsZero() { |
| 182 |
t.Errorf("IsZero() should be true") |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func TestIntScan(t *testing.T) { |
| 187 |
var i Int |
| 188 |
err := i.Scan(12345) |
| 189 |
maybePanic(err) |
| 190 |
assertInt(t, i, "scanned int") |
| 191 |
|
| 192 |
var null Int |
| 193 |
err = null.Scan(nil) |
| 194 |
maybePanic(err) |
| 195 |
assertNullInt(t, null, "scanned null") |
| 196 |
} |
| 197 |
|
| 198 |
func TestIntSetValid(t *testing.T) { |
| 199 |
change := NewInt(0, false) |
| 200 |
assertNullInt(t, change, "SetValid()") |
| 201 |
change.SetValid(12345) |
| 202 |
assertInt(t, change, "SetValid()") |
| 203 |
} |
| 204 |
|
| 205 |
func TestIntValueOrZero(t *testing.T) { |
| 206 |
valid := NewInt(12345, true) |
| 207 |
if valid.ValueOrZero() != 12345 { |
| 208 |
t.Error("unexpected ValueOrZero", valid.ValueOrZero()) |
| 209 |
} |
| 210 |
|
| 211 |
invalid := NewInt(12345, false) |
| 212 |
if invalid.ValueOrZero() != 0 { |
| 213 |
t.Error("unexpected ValueOrZero", invalid.ValueOrZero()) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestIntEqual(t *testing.T) { |
| 218 |
int1 := NewInt(10, false) |
| 219 |
int2 := NewInt(10, false) |
| 220 |
assertIntEqualIsTrue(t, int1, int2) |
| 221 |
|
| 222 |
int1 = NewInt(10, false) |
| 223 |
int2 = NewInt(20, false) |
| 224 |
assertIntEqualIsTrue(t, int1, int2) |
| 225 |
|
| 226 |
int1 = NewInt(10, true) |
| 227 |
int2 = NewInt(10, true) |
| 228 |
assertIntEqualIsTrue(t, int1, int2) |
| 229 |
|
| 230 |
int1 = NewInt(0, true) |
| 231 |
int2 = NewInt(10, false) |
| 232 |
assertIntEqualIsTrue(t, int1, int2) |
| 233 |
|
| 234 |
int1 = NewInt(10, true) |
| 235 |
int2 = NewInt(10, false) |
| 236 |
assertIntEqualIsFalse(t, int1, int2) |
| 237 |
|
| 238 |
int1 = NewInt(10, false) |
| 239 |
int2 = NewInt(10, true) |
| 240 |
assertIntEqualIsFalse(t, int1, int2) |
| 241 |
|
| 242 |
int1 = NewInt(10, true) |
| 243 |
int2 = NewInt(20, true) |
| 244 |
assertIntEqualIsFalse(t, int1, int2) |
| 245 |
} |
| 246 |
|
| 247 |
func assertInt(t *testing.T, i Int, from string) { |
| 248 |
if i.Int64 != 12345 { |
| 249 |
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345) |
| 250 |
} |
| 251 |
if !i.Valid { |
| 252 |
t.Error(from, "is invalid, but should be valid") |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func assertNullInt(t *testing.T, i Int, from string) { |
| 257 |
if i.Valid { |
| 258 |
t.Error(from, "is valid, but should be invalid") |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func assertIntEqualIsTrue(t *testing.T, a, b Int) { |
| 263 |
t.Helper() |
| 264 |
if !a.Equal(b) { |
| 265 |
t.Errorf("Equal() of Int{%v, Valid:%t} and Int{%v, Valid:%t} should return true", a.Int64, a.Valid, b.Int64, b.Valid) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func assertIntEqualIsFalse(t *testing.T, a, b Int) { |
| 270 |
t.Helper() |
| 271 |
if a.Equal(b) { |
| 272 |
t.Errorf("Equal() of Int{%v, Valid:%t} and Int{%v, Valid:%t} should return false", a.Int64, a.Valid, b.Int64, b.Valid) |
| 273 |
} |
| 274 |
} |