Add tests for non-integer floats being cast to Int silently, and int64 overflows
This commit is contained in:
parent
c0d98407b9
commit
041ab70863
1 changed files with 27 additions and 0 deletions
27
int_test.go
27
int_test.go
|
@ -2,6 +2,8 @@ package null
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -54,6 +56,31 @@ func TestUnmarshalInt(t *testing.T) {
|
||||||
assertNullInt(t, badType, "wrong type json")
|
assertNullInt(t, badType, "wrong type json")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalNonIntegerNumber(t *testing.T) {
|
||||||
|
floatJSON = []byte(`123.45`)
|
||||||
|
var i Int
|
||||||
|
err := json.Unmarshal(floatJSON, &i)
|
||||||
|
if err == nil {
|
||||||
|
panic("err should be present; non-integer number coerced to int")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalInt64Overflow(t *testing.T) {
|
||||||
|
int64Overflow := uint64(math.MaxInt64)
|
||||||
|
|
||||||
|
// Max int64 should decode successfully
|
||||||
|
var i Int
|
||||||
|
err := json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||||
|
maybePanic(err)
|
||||||
|
|
||||||
|
// Attempt to overflow
|
||||||
|
int64Overflow++
|
||||||
|
err = json.Unmarshal([]byte(strconv.FormatUint(int64Overflow, 10)), &i)
|
||||||
|
if err == nil {
|
||||||
|
panic("err should be present; decoded value overflows int64")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTextUnmarshalInt(t *testing.T) {
|
func TestTextUnmarshalInt(t *testing.T) {
|
||||||
var i Int
|
var i Int
|
||||||
err := i.UnmarshalText([]byte("12345"))
|
err := i.UnmarshalText([]byte("12345"))
|
||||||
|
|
Loading…
Reference in a new issue