2014-09-01 06:46:40 +02:00
|
|
|
package null
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-10 16:00:05 +01:00
|
|
|
boolJSON = []byte(`true`)
|
|
|
|
falseJSON = []byte(`false`)
|
2014-09-01 06:46:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoolFrom(t *testing.T) {
|
|
|
|
b := BoolFrom(true)
|
|
|
|
assertBool(t, b, "BoolFrom()")
|
|
|
|
|
|
|
|
zero := BoolFrom(false)
|
2014-09-01 20:22:17 +02:00
|
|
|
if !zero.Valid {
|
|
|
|
t.Error("BoolFrom(false)", "is invalid, but should be valid")
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolFromPtr(t *testing.T) {
|
2014-09-01 20:22:17 +02:00
|
|
|
n := true
|
|
|
|
bptr := &n
|
2014-09-01 06:46:40 +02:00
|
|
|
b := BoolFromPtr(bptr)
|
|
|
|
assertBool(t, b, "BoolFromPtr()")
|
|
|
|
|
|
|
|
null := BoolFromPtr(nil)
|
|
|
|
assertNullBool(t, null, "BoolFromPtr(nil)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalBool(t *testing.T) {
|
|
|
|
var b Bool
|
|
|
|
err := json.Unmarshal(boolJSON, &b)
|
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertBool(t, b, "bool json")
|
2014-09-01 06:46:40 +02:00
|
|
|
|
|
|
|
var null Bool
|
|
|
|
err = json.Unmarshal(nullJSON, &null)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullBool(t, null, "null json")
|
2014-09-09 05:31:58 +02:00
|
|
|
|
|
|
|
var badType Bool
|
|
|
|
err = json.Unmarshal(intJSON, &badType)
|
|
|
|
if err == nil {
|
|
|
|
panic("err should not be nil")
|
|
|
|
}
|
|
|
|
assertNullBool(t, badType, "wrong type json")
|
2015-09-10 13:05:24 +02:00
|
|
|
|
|
|
|
var invalid Bool
|
|
|
|
err = invalid.UnmarshalJSON(invalidJSON)
|
|
|
|
if _, ok := err.(*json.SyntaxError); !ok {
|
|
|
|
t.Errorf("expected json.SyntaxError, not %T", err)
|
|
|
|
}
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextUnmarshalBool(t *testing.T) {
|
|
|
|
var b Bool
|
2014-09-01 20:22:17 +02:00
|
|
|
err := b.UnmarshalText([]byte("true"))
|
2014-09-01 06:46:40 +02:00
|
|
|
maybePanic(err)
|
|
|
|
assertBool(t, b, "UnmarshalText() bool")
|
|
|
|
|
|
|
|
var zero Bool
|
2014-09-01 20:22:17 +02:00
|
|
|
err = zero.UnmarshalText([]byte("false"))
|
2014-09-01 06:46:40 +02:00
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertFalseBool(t, zero, "UnmarshalText() false")
|
2014-09-01 06:46:40 +02:00
|
|
|
|
|
|
|
var blank Bool
|
|
|
|
err = blank.UnmarshalText([]byte(""))
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullBool(t, blank, "UnmarshalText() empty bool")
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
var invalid Bool
|
|
|
|
err = invalid.UnmarshalText([]byte(":D"))
|
|
|
|
if err == nil {
|
|
|
|
panic("err should not be nil")
|
|
|
|
}
|
|
|
|
assertNullBool(t, invalid, "invalid json")
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBool(t *testing.T) {
|
|
|
|
b := BoolFrom(true)
|
|
|
|
data, err := json.Marshal(b)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "true", "non-empty json marshal")
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
zero := NewBool(false, true)
|
|
|
|
data, err = json.Marshal(zero)
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "false", "zero json marshal")
|
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
2014-09-01 06:46:40 +02:00
|
|
|
null := NewBool(false, false)
|
|
|
|
data, err = json.Marshal(null)
|
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertJSONEquals(t, data, "null", "null json marshal")
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalBoolText(t *testing.T) {
|
|
|
|
b := BoolFrom(true)
|
|
|
|
data, err := b.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "true", "non-empty text marshal")
|
|
|
|
|
2014-09-01 20:22:17 +02:00
|
|
|
zero := NewBool(false, true)
|
|
|
|
data, err = zero.MarshalText()
|
|
|
|
maybePanic(err)
|
|
|
|
assertJSONEquals(t, data, "false", "zero text marshal")
|
|
|
|
|
|
|
|
// invalid values should be encoded as null
|
2014-09-01 06:46:40 +02:00
|
|
|
null := NewBool(false, false)
|
|
|
|
data, err = null.MarshalText()
|
|
|
|
maybePanic(err)
|
2014-09-01 20:22:17 +02:00
|
|
|
assertJSONEquals(t, data, "", "null text marshal")
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolPointer(t *testing.T) {
|
|
|
|
b := BoolFrom(true)
|
|
|
|
ptr := b.Ptr()
|
|
|
|
if *ptr != true {
|
2015-01-28 07:15:30 +01:00
|
|
|
t.Errorf("bad %s bool: %#v ≠ %v\n", "pointer", ptr, true)
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
null := NewBool(false, false)
|
|
|
|
ptr = null.Ptr()
|
|
|
|
if ptr != nil {
|
|
|
|
t.Errorf("bad %s bool: %#v ≠ %s\n", "nil pointer", ptr, "nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolIsZero(t *testing.T) {
|
|
|
|
b := BoolFrom(true)
|
|
|
|
if b.IsZero() {
|
|
|
|
t.Errorf("IsZero() should be false")
|
|
|
|
}
|
|
|
|
|
|
|
|
null := NewBool(false, false)
|
|
|
|
if !null.IsZero() {
|
|
|
|
t.Errorf("IsZero() should be true")
|
|
|
|
}
|
|
|
|
|
|
|
|
zero := NewBool(false, true)
|
2014-09-01 20:22:17 +02:00
|
|
|
if zero.IsZero() {
|
|
|
|
t.Errorf("IsZero() should be false")
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-01 07:25:57 +02:00
|
|
|
func TestBoolSetValid(t *testing.T) {
|
|
|
|
change := NewBool(false, false)
|
|
|
|
assertNullBool(t, change, "SetValid()")
|
|
|
|
change.SetValid(true)
|
|
|
|
assertBool(t, change, "SetValid()")
|
|
|
|
}
|
|
|
|
|
2014-09-01 06:46:40 +02:00
|
|
|
func TestBoolScan(t *testing.T) {
|
|
|
|
var b Bool
|
|
|
|
err := b.Scan(true)
|
|
|
|
maybePanic(err)
|
|
|
|
assertBool(t, b, "scanned bool")
|
|
|
|
|
|
|
|
var null Bool
|
|
|
|
err = null.Scan(nil)
|
|
|
|
maybePanic(err)
|
|
|
|
assertNullBool(t, null, "scanned null")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertBool(t *testing.T, b Bool, from string) {
|
|
|
|
if b.Bool != true {
|
2014-09-01 20:22:17 +02:00
|
|
|
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, true)
|
|
|
|
}
|
|
|
|
if !b.Valid {
|
|
|
|
t.Error(from, "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertFalseBool(t *testing.T, b Bool, from string) {
|
|
|
|
if b.Bool != false {
|
|
|
|
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, false)
|
2014-09-01 06:46:40 +02:00
|
|
|
}
|
|
|
|
if !b.Valid {
|
|
|
|
t.Error(from, "is invalid, but should be valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNullBool(t *testing.T, b Bool, from string) {
|
|
|
|
if b.Valid {
|
|
|
|
t.Error(from, "is valid, but should be invalid")
|
|
|
|
}
|
|
|
|
}
|