forked from volatiletech/null
-
Notifications
You must be signed in to change notification settings - Fork 1
/
byte_test.go
162 lines (136 loc) · 3.38 KB
/
byte_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package null
import (
"encoding/json"
"testing"
)
var (
byteJSON = []byte(`"b"`)
)
func TestByteFrom(t *testing.T) {
i := ByteFrom('b')
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) {
n := byte('b')
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
err := json.Unmarshal(nullJSON, &null)
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
err := i.UnmarshalText([]byte("b"))
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) {
i := ByteFrom('b')
data, err := json.Marshal(i)
maybePanic(err)
assertJSONEquals(t, data, `"b"`, "non-empty json marshal")
// 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) {
i := ByteFrom('b')
data, err := i.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "b", "non-empty text marshal")
// 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) {
i := ByteFrom('b')
ptr := i.Ptr()
if *ptr != 'b' {
t.Errorf("bad %s int: %#v ≠ %d\n", "pointer", ptr, 'b')
}
null := NewByte(0, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s int: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestByteIsZero(t *testing.T) {
i := ByteFrom('b')
if i.IsZero() {
t.Errorf("IsZero() should be false")
}
null := NewByte(0, false)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
zero := NewByte(0, true)
if zero.IsZero() {
t.Errorf("IsZero() should be false")
}
}
func TestByteSetValid(t *testing.T) {
change := NewByte(0, false)
assertNullByte(t, change, "SetValid()")
change.SetValid('b')
assertByte(t, change, "SetValid()")
}
func TestByteScan(t *testing.T) {
var i Byte
err := i.Scan("b")
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) {
if i.Byte != 'b' {
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Byte, 'b')
}
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")
}
}