2016-11-10 16:40:50 +01:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
byteJSON = []byte(`"b"`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestByteFrom(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
i := ByteFrom('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
assertByte(t, i, "ByteFrom()")
|
|
|
|
|
|
|
|
zero := ByteFrom(0)
|
|
|
|
if !zero.Valid {
|
|
|
|
t.Error("ByteFrom(0)", "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteFromPtr(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
n := byte('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
iptr := &n
|
|
|
|
i := ByteFromPtr(iptr)
|
|
|
|
assertByte(t, i, "ByteFromPtr()")
|
|
|
|
|
|
|
|
null := ByteFromPtr(nil)
|
|
|
|
assertNullByte(t, null, "ByteFromPtr(nil)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalByte(t *testing.T) {
|
|
|
|
var null Byte
|
2016-11-11 08:15:01 +01:00
|
|
|
err := json.Unmarshal(nullJSON, &null)
|
2016-11-10 16:40:50 +01:00
|
|
|
maybePanic(err)
|
|
|
|
assertNullByte(t, null, "null json")
|
|
|
|
|
|
|
|
var badType Byte
|
|
|
|
err = json.Unmarshal(boolJSON, &badType)
|
|
|
|
if err == nil {
|
|
|
|
panic("err should not be nil")
|
|
|
|
}
|
|
|
|
assertNullByte(t, badType, "wrong type json")
|
|
|
|
|
|
|
|
var invalid Byte
|
|
|
|
err = invalid.UnmarshalJSON(invalidJSON)
|
|
|
|
if _, ok := err.(*json.SyntaxError); !ok {
|
|
|
|
t.Errorf("expected json.SyntaxError, not %T", err)
|
|
|
|
}
|
|
|
|
assertNullByte(t, invalid, "invalid json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalNonByteegerNumber(t *testing.T) {
|
|
|
|
var i Byte
|
|
|
|
err := json.Unmarshal(float64JSON, &i)
|
|
|
|
if err == nil {
|
|
|
|
panic("err should be present; non-integer number coerced to int")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextUnmarshalByte(t *testing.T) {
|
|
|
|
var i Byte
|
2016-11-11 08:15:01 +01:00
|
|
|
err := i.UnmarshalText([]byte("b"))
|
2016-11-10 16:40:50 +01:00
|
|
|
maybePanic(err)
|
|
|
|
assertByte(t, i, "UnmarshalText() int")
|
|
|
|
|
|
|
|
var blank Byte
|
|
|
|
err = blank.UnmarshalText([]byte(""))
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullByte(t, blank, "UnmarshalText() empty int")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalByte(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
i := ByteFrom('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
data, err := json.Marshal(i)
|
|
|
|
maybePanic(err)
|
2016-11-11 08:15:01 +01:00
|
|
|
assertJSONEquals(t, data, `"b"`, "non-empty json marshal")
|
2016-11-10 16:40:50 +01:00
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
|
|
|
null := NewByte(0, false)
|
|
|
|
data, err = json.Marshal(null)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "null", "null json marshal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalByteText(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
i := ByteFrom('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
data, err := i.MarshalText()
|
|
|
|
maybePanic(err)
|
2016-11-11 08:15:01 +01:00
|
|
|
assertJSONEquals(t, data, "b", "non-empty text marshal")
|
2016-11-10 16:40:50 +01:00
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
|
|
|
null := NewByte(0, false)
|
|
|
|
data, err = null.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "", "null text marshal")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytePointer(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
i := ByteFrom('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
ptr := i.Ptr()
|
2016-11-11 08:15:01 +01:00
|
|
|
if *ptr != 'b' {
|
|
|
|
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 'b')
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewByte(0, false)
|
|
|
|
ptr = null.Ptr()
|
|
|
|
if ptr != nil {
|
|
|
|
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 20:14:43 +02:00
|
|
|
func TestByteIsNull(t *testing.T) {
|
2016-11-11 08:15:01 +01:00
|
|
|
i := ByteFrom('b')
|
2017-07-06 20:14:43 +02:00
|
|
|
if i.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be false")
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewByte(0, false)
|
2017-07-06 20:14:43 +02:00
|
|
|
if !null.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be true")
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
zero := NewByte(0, true)
|
2017-07-06 20:14:43 +02:00
|
|
|
if zero.IsNull() {
|
|
|
|
t.Errorf("IsNull() should be false")
|
|
|
|
}
|
|
|
|
|
|
|
|
var testInt interface{}
|
|
|
|
testInt = zero
|
|
|
|
if _, ok := testInt.(Nullable); !ok {
|
|
|
|
t.Errorf("Nullable interface should be implemented")
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteSetValid(t *testing.T) {
|
|
|
|
change := NewByte(0, false)
|
|
|
|
assertNullByte(t, change, "SetValid()")
|
2016-11-11 08:15:01 +01:00
|
|
|
change.SetValid('b')
|
2016-11-10 16:40:50 +01:00
|
|
|
assertByte(t, change, "SetValid()")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteScan(t *testing.T) {
|
|
|
|
var i Byte
|
2016-11-12 04:40:11 +01:00
|
|
|
err := i.Scan("b")
|
2016-11-10 16:40:50 +01:00
|
|
|
maybePanic(err)
|
|
|
|
assertByte(t, i, "scanned int")
|
|
|
|
|
|
|
|
var null Byte
|
|
|
|
err = null.Scan(nil)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullByte(t, null, "scanned null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertByte(t *testing.T, i Byte, from string) {
|
2016-11-11 08:15:01 +01:00
|
|
|
if i.Byte != 'b' {
|
|
|
|
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 'b')
|
2016-11-10 16:40:50 +01:00
|
|
|
}
|
|
|
|
if !i.Valid {
|
|
|
|
t.Error(from, "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNullByte(t *testing.T, i Byte, from string) {
|
|
|
|
if i.Valid {
|
|
|
|
t.Error(from, "is valid, but should be invalid")
|
|
|
|
}
|
|
|
|
}
|