| 1 |
package null |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var ( |
| 9 |
boolJSON = []byte(`true`) |
| 10 |
falseJSON = []byte(`false`) |
| 11 |
nullBoolJSON = []byte(`{"Bool":true,"Valid":true}`) |
| 12 |
) |
| 13 |
|
| 14 |
func TestBoolFrom(t *testing.T) { |
| 15 |
b := BoolFrom(true) |
| 16 |
assertBool(t, b, "BoolFrom()") |
| 17 |
|
| 18 |
zero := BoolFrom(false) |
| 19 |
if !zero.Valid { |
| 20 |
t.Error("BoolFrom(false)", "is invalid, but should be valid") |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
func TestBoolFromPtr(t *testing.T) { |
| 25 |
n := true |
| 26 |
bptr := &n |
| 27 |
b := BoolFromPtr(bptr) |
| 28 |
assertBool(t, b, "BoolFromPtr()") |
| 29 |
|
| 30 |
null := BoolFromPtr(nil) |
| 31 |
assertNullBool(t, null, "BoolFromPtr(nil)") |
| 32 |
} |
| 33 |
|
| 34 |
func TestUnmarshalBool(t *testing.T) { |
| 35 |
var b Bool |
| 36 |
err := json.Unmarshal(boolJSON, &b) |
| 37 |
maybePanic(err) |
| 38 |
assertBool(t, b, "bool json") |
| 39 |
|
| 40 |
var nb Bool |
| 41 |
err = json.Unmarshal(nullBoolJSON, &nb) |
| 42 |
maybePanic(err) |
| 43 |
assertBool(t, nb, "sq.NullBool json") |
| 44 |
|
| 45 |
var null Bool |
| 46 |
err = json.Unmarshal(nullJSON, &null) |
| 47 |
maybePanic(err) |
| 48 |
assertNullBool(t, null, "null json") |
| 49 |
|
| 50 |
var badType Bool |
| 51 |
err = json.Unmarshal(intJSON, &badType) |
| 52 |
if err == nil { |
| 53 |
panic("err should not be nil") |
| 54 |
} |
| 55 |
assertNullBool(t, badType, "wrong type json") |
| 56 |
|
| 57 |
var invalid Bool |
| 58 |
err = invalid.UnmarshalJSON(invalidJSON) |
| 59 |
if _, ok := err.(*json.SyntaxError); !ok { |
| 60 |
t.Errorf("expected json.SyntaxError, not %T", err) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestTextUnmarshalBool(t *testing.T) { |
| 65 |
var b Bool |
| 66 |
err := b.UnmarshalText([]byte("true")) |
| 67 |
maybePanic(err) |
| 68 |
assertBool(t, b, "UnmarshalText() bool") |
| 69 |
|
| 70 |
var zero Bool |
| 71 |
err = zero.UnmarshalText([]byte("false")) |
| 72 |
maybePanic(err) |
| 73 |
assertFalseBool(t, zero, "UnmarshalText() false") |
| 74 |
|
| 75 |
var blank Bool |
| 76 |
err = blank.UnmarshalText([]byte("")) |
| 77 |
maybePanic(err) |
| 78 |
assertNullBool(t, blank, "UnmarshalText() empty bool") |
| 79 |
|
| 80 |
var null Bool |
| 81 |
err = null.UnmarshalText([]byte("null")) |
| 82 |
maybePanic(err) |
| 83 |
assertNullBool(t, null, `UnmarshalText() "null"`) |
| 84 |
|
| 85 |
var invalid Bool |
| 86 |
err = invalid.UnmarshalText([]byte(":D")) |
| 87 |
if err == nil { |
| 88 |
panic("err should not be nil") |
| 89 |
} |
| 90 |
assertNullBool(t, invalid, "invalid json") |
| 91 |
} |
| 92 |
|
| 93 |
func TestMarshalBool(t *testing.T) { |
| 94 |
b := BoolFrom(true) |
| 95 |
data, err := json.Marshal(b) |
| 96 |
maybePanic(err) |
| 97 |
assertJSONEquals(t, data, "true", "non-empty json marshal") |
| 98 |
|
| 99 |
zero := NewBool(false, true) |
| 100 |
data, err = json.Marshal(zero) |
| 101 |
maybePanic(err) |
| 102 |
assertJSONEquals(t, data, "false", "zero json marshal") |
| 103 |
|
| 104 |
|
| 105 |
null := NewBool(false, false) |
| 106 |
data, err = json.Marshal(null) |
| 107 |
maybePanic(err) |
| 108 |
assertJSONEquals(t, data, "null", "null json marshal") |
| 109 |
} |
| 110 |
|
| 111 |
func TestMarshalBoolText(t *testing.T) { |
| 112 |
b := BoolFrom(true) |
| 113 |
data, err := b.MarshalText() |
| 114 |
maybePanic(err) |
| 115 |
assertJSONEquals(t, data, "true", "non-empty text marshal") |
| 116 |
|
| 117 |
zero := NewBool(false, true) |
| 118 |
data, err = zero.MarshalText() |
| 119 |
maybePanic(err) |
| 120 |
assertJSONEquals(t, data, "false", "zero text marshal") |
| 121 |
|
| 122 |
|
| 123 |
null := NewBool(false, false) |
| 124 |
data, err = null.MarshalText() |
| 125 |
maybePanic(err) |
| 126 |
assertJSONEquals(t, data, "", "null text marshal") |
| 127 |
} |
| 128 |
|
| 129 |
func TestBoolPointer(t *testing.T) { |
| 130 |
b := BoolFrom(true) |
| 131 |
ptr := b.Ptr() |
| 132 |
if *ptr != true { |
| 133 |
t.Errorf("bad %s bool: %#v ≠ %v\n", "pointer", ptr, true) |
| 134 |
} |
| 135 |
|
| 136 |
null := NewBool(false, false) |
| 137 |
ptr = null.Ptr() |
| 138 |
if ptr != nil { |
| 139 |
t.Errorf("bad %s bool: %#v ≠ %s\n", "nil pointer", ptr, "nil") |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestBoolIsZero(t *testing.T) { |
| 144 |
b := BoolFrom(true) |
| 145 |
if b.IsZero() { |
| 146 |
t.Errorf("IsZero() should be false") |
| 147 |
} |
| 148 |
|
| 149 |
null := NewBool(false, false) |
| 150 |
if !null.IsZero() { |
| 151 |
t.Errorf("IsZero() should be true") |
| 152 |
} |
| 153 |
|
| 154 |
zero := NewBool(false, true) |
| 155 |
if zero.IsZero() { |
| 156 |
t.Errorf("IsZero() should be false") |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestBoolSetValid(t *testing.T) { |
| 161 |
change := NewBool(false, false) |
| 162 |
assertNullBool(t, change, "SetValid()") |
| 163 |
change.SetValid(true) |
| 164 |
assertBool(t, change, "SetValid()") |
| 165 |
} |
| 166 |
|
| 167 |
func TestBoolScan(t *testing.T) { |
| 168 |
var b Bool |
| 169 |
err := b.Scan(true) |
| 170 |
maybePanic(err) |
| 171 |
assertBool(t, b, "scanned bool") |
| 172 |
|
| 173 |
var null Bool |
| 174 |
err = null.Scan(nil) |
| 175 |
maybePanic(err) |
| 176 |
assertNullBool(t, null, "scanned null") |
| 177 |
} |
| 178 |
|
| 179 |
func assertBool(t *testing.T, b Bool, from string) { |
| 180 |
if b.Bool != true { |
| 181 |
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, true) |
| 182 |
} |
| 183 |
if !b.Valid { |
| 184 |
t.Error(from, "is invalid, but should be valid") |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
func assertFalseBool(t *testing.T, b Bool, from string) { |
| 189 |
if b.Bool != false { |
| 190 |
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, false) |
| 191 |
} |
| 192 |
if !b.Valid { |
| 193 |
t.Error(from, "is invalid, but should be valid") |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func assertNullBool(t *testing.T, b Bool, from string) { |
| 198 |
if b.Valid { |
| 199 |
t.Error(from, "is valid, but should be invalid") |
| 200 |
} |
| 201 |
} |