lbry.go/float_test.go

164 lines
3.5 KiB
Go
Raw Normal View History

2014-09-01 06:46:40 +02:00
package null
import (
"encoding/json"
"testing"
)
var (
floatJSON = []byte(`1.2345`)
nullFloatJSON = []byte(`{"Float64":1.2345,"Valid":true}`)
)
func TestFloatFrom(t *testing.T) {
f := FloatFrom(1.2345)
assertFloat(t, f, "FloatFrom()")
zero := FloatFrom(0)
if !zero.Valid {
t.Error("FloatFrom(0)", "is invalid, but should be valid")
2014-09-01 06:46:40 +02:00
}
}
func TestFloatFromPtr(t *testing.T) {
n := float64(1.2345)
iptr := &n
f := FloatFromPtr(iptr)
assertFloat(t, f, "FloatFromPtr()")
null := FloatFromPtr(nil)
assertNullFloat(t, null, "FloatFromPtr(nil)")
}
func TestUnmarshalFloat(t *testing.T) {
var f Float
err := json.Unmarshal(floatJSON, &f)
maybePanic(err)
assertFloat(t, f, "float json")
var nf Float
err = json.Unmarshal(nullFloatJSON, &nf)
maybePanic(err)
assertFloat(t, nf, "sq.NullFloat64 json")
2014-09-01 06:46:40 +02:00
var null Float
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullFloat(t, null, "null json")
var badType Float
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullFloat(t, badType, "wrong type json")
2014-09-01 06:46:40 +02:00
}
func TestTextUnmarshalFloat(t *testing.T) {
var f Float
err := f.UnmarshalText([]byte("1.2345"))
maybePanic(err)
assertFloat(t, f, "UnmarshalText() float")
var blank Float
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullFloat(t, blank, "UnmarshalText() empty float")
var null Float
err = null.UnmarshalText([]byte("null"))
maybePanic(err)
assertNullFloat(t, null, `UnmarshalText() "null"`)
}
func TestMarshalFloat(t *testing.T) {
f := FloatFrom(1.2345)
data, err := json.Marshal(f)
maybePanic(err)
assertJSONEquals(t, data, "1.2345", "non-empty json marshal")
// invalid values should be encoded as null
2014-09-01 06:46:40 +02:00
null := NewFloat(0, false)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, "null", "null json marshal")
2014-09-01 06:46:40 +02:00
}
func TestMarshalFloatText(t *testing.T) {
f := FloatFrom(1.2345)
data, err := f.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "1.2345", "non-empty text marshal")
// invalid values should be encoded as null
2014-09-01 06:46:40 +02:00
null := NewFloat(0, false)
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "null text marshal")
2014-09-01 06:46:40 +02:00
}
func TestFloatPointer(t *testing.T) {
f := FloatFrom(1.2345)
ptr := f.Ptr()
if *ptr != 1.2345 {
t.Errorf("bad %s float: %#v ≠ %s\n", "pointer", ptr, 1.2345)
2014-09-01 06:46:40 +02:00
}
null := NewFloat(0, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s float: %#v ≠ %s\n", "nil pointer", ptr, "nil")
2014-09-01 06:46:40 +02:00
}
}
func TestFloatIsZero(t *testing.T) {
f := FloatFrom(1.2345)
if f.IsZero() {
t.Errorf("IsZero() should be false")
}
null := NewFloat(0, false)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
zero := NewFloat(0, true)
if zero.IsZero() {
t.Errorf("IsZero() should be false")
2014-09-01 06:46:40 +02:00
}
}
func TestFloatSetValid(t *testing.T) {
change := NewFloat(0, false)
assertNullFloat(t, change, "SetValid()")
change.SetValid(1.2345)
assertFloat(t, change, "SetValid()")
}
2014-09-01 06:46:40 +02:00
func TestFloatScan(t *testing.T) {
var f Float
err := f.Scan(1.2345)
maybePanic(err)
assertFloat(t, f, "scanned float")
var null Float
err = null.Scan(nil)
maybePanic(err)
assertNullFloat(t, null, "scanned null")
}
func assertFloat(t *testing.T, f Float, from string) {
if f.Float64 != 1.2345 {
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
}
if !f.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullFloat(t *testing.T, f Float, from string) {
if f.Valid {
t.Error(from, "is valid, but should be invalid")
}
}