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