lbry.go/zero/float_test.go

166 lines
3.6 KiB
Go

package zero
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 valid, but should be invalid")
}
}
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, "sql.NullFloat64 json")
var zero Float
err = json.Unmarshal(zeroJSON, &zero)
maybePanic(err)
assertNullFloat(t, zero, "zero json")
var null Float
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullFloat(t, null, "null json")
}
func TestTextUnmarshalFloat(t *testing.T) {
var f Float
err := f.UnmarshalText([]byte("1.2345"))
maybePanic(err)
assertFloat(t, f, "UnmarshalText() float")
var zero Float
err = zero.UnmarshalText([]byte("0"))
maybePanic(err)
assertNullFloat(t, zero, "UnmarshalText() zero 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 0
null := NewFloat(0, false)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, "0", "null json marshal")
}
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 zero
null := NewFloat(0, false)
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "0", "null text marshal")
}
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)
}
null := NewFloat(0, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s Float: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
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 true")
}
}
func TestFloatSetValid(t *testing.T) {
change := NewFloat(0, false)
assertNullFloat(t, change, "SetValid()")
change.SetValid(1.2345)
assertFloat(t, change, "SetValid()")
}
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")
}
}