forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
322 lines (274 loc) · 7.15 KB
/
string_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package null
import (
"encoding/json"
"errors"
"io"
"testing"
"github.com/mailru/easyjson"
"github.com/stretchr/testify/assert"
)
var (
stringJSON = []byte(`"test"`)
blankStringJSON = []byte(`""`)
nullStringJSON = []byte(`{"String":"test","Valid":true}`)
nullJSON = []byte(`null`)
invalidJSON = []byte(`:)`)
)
func TestStringFrom(t *testing.T) {
str := StringFrom("test")
assertStr(t, str, "StringFrom() string")
zero := StringFrom("")
if !zero.Valid {
t.Error("StringFrom(0)", "is invalid, but should be valid")
}
}
func TestStringFromPtr(t *testing.T) {
s := "test"
sptr := &s
str := StringFromPtr(sptr)
assertStr(t, str, "StringFromPtr() string")
sptr = nil
null := StringFromPtr(sptr)
assertNullStr(t, null, "StringFromPtr(nil)")
}
func TestUnmarshalString(t *testing.T) {
var str String
err := json.Unmarshal(stringJSON, &str)
maybePanic(err)
assertStr(t, str, "string json")
var ns String
err = json.Unmarshal(nullStringJSON, &ns)
maybePanic(err)
assertStr(t, ns, "test")
var blank String
err = json.Unmarshal(blankStringJSON, &blank)
maybePanic(err)
if !blank.Valid {
t.Error("blank string should be valid")
}
var empty String
maybePanic(empty.UnmarshalJSON([]byte{}))
assertNullStr(t, empty, "null json")
var null String
err = json.Unmarshal(nullJSON, &null)
maybePanic(err)
assertNullStr(t, null, "null json")
var badType String
err = json.Unmarshal(boolJSON, &badType)
if err == nil {
panic("err should not be nil")
}
assertNullStr(t, badType, "wrong type json")
var invalid String
err = invalid.UnmarshalJSON(invalidJSON)
var syntaxError *json.SyntaxError
if !errors.As(err, &syntaxError) {
t.Errorf("expected wrapped json.SyntaxError, not %T", err)
}
assertNullStr(t, invalid, "invalid json")
}
func TestStringUnmarshalEasyJSON(t *testing.T) {
tests := []struct {
data string
exp String
}{
{
data: "null",
},
{
data: `"hat"`,
exp: StringFrom("hat"),
},
{
data: `""`,
exp: StringFrom(""),
},
{
data: `{"String":"hat","Valid":true}`,
exp: StringFrom("hat"),
},
{
data: `{"string":"hat","valid":true}`,
exp: StringFrom("hat"),
},
}
for _, test := range tests {
t.Run(test.data, func(t *testing.T) {
var str String
assert.NoError(t, easyjson.Unmarshal([]byte(test.data), &str), "easyjson.Unmarshal")
assert.Equal(t, test.exp, str)
var str2 String
assert.NoError(t, json.Unmarshal([]byte(test.data), &str2), "json.Unmarshal")
assert.Equal(t, test.exp, str2)
})
}
}
func TestTextUnmarshalString(t *testing.T) {
var str String
err := str.UnmarshalText([]byte("test"))
maybePanic(err)
assertStr(t, str, "UnmarshalText() string")
var null String
err = null.UnmarshalText([]byte(""))
maybePanic(err)
assertNullStr(t, null, "UnmarshalText() empty string")
}
func TestMarshalString(t *testing.T) {
str := StringFrom("test")
data, err := json.Marshal(str)
maybePanic(err)
assertJSONEquals(t, data, `"test"`, "non-empty json marshal")
data, err = str.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "test", "non-empty text marshal")
// empty values should be encoded as an empty string
zero := StringFrom("")
data, err = json.Marshal(zero)
maybePanic(err)
assertJSONEquals(t, data, `""`, "empty json marshal")
data, err = zero.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "string marshal text")
var sptr *string
null := StringFromPtr(sptr)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, `null`, "null json marshal")
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "string marshal text")
}
func TestStringPointer(t *testing.T) {
str := StringFrom("test")
ptr := str.Ptr()
if *ptr != "test" {
t.Errorf("bad %s string: %#v ≠ %s\n", "pointer", ptr, "test")
}
null := NewString("", false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s string: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestStringIsZero(t *testing.T) {
str := StringFrom("test")
if str.IsZero() {
t.Errorf("IsZero() should be false")
}
blank := StringFrom("")
if blank.IsZero() {
t.Errorf("IsZero() should be false")
}
empty := NewString("", true)
if empty.IsZero() {
t.Errorf("IsZero() should be false")
}
var sptr *string
null := StringFromPtr(sptr)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
}
func TestStringSetValid(t *testing.T) {
change := NewString("", false)
assertNullStr(t, change, "SetValid()")
change.SetValid("test")
assertStr(t, change, "SetValid()")
}
func TestStringScan(t *testing.T) {
var str String
err := str.Scan("test")
maybePanic(err)
assertStr(t, str, "scanned string")
var null String
err = null.Scan(nil)
maybePanic(err)
assertNullStr(t, null, "scanned null")
}
func TestStringValueOrZero(t *testing.T) {
valid := NewString("test", true)
if valid.ValueOrZero() != "test" {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}
invalid := NewString("test", false)
if invalid.ValueOrZero() != "" {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}
func TestStringEqual(t *testing.T) {
str1 := NewString("foo", false)
str2 := NewString("foo", false)
assertStringEqualIsTrue(t, str1, str2)
str1 = NewString("foo", false)
str2 = NewString("bar", false)
assertStringEqualIsTrue(t, str1, str2)
str1 = NewString("foo", true)
str2 = NewString("foo", true)
assertStringEqualIsTrue(t, str1, str2)
str1 = NewString("foo", true)
str2 = NewString("foo", false)
assertStringEqualIsFalse(t, str1, str2)
str1 = NewString("foo", false)
str2 = NewString("foo", true)
assertStringEqualIsFalse(t, str1, str2)
str1 = NewString("foo", true)
str2 = NewString("bar", true)
assertStringEqualIsFalse(t, str1, str2)
}
func TestUnderlyingString(t *testing.T) {
type foo string
const val string = "foo!"
f := foo(val)
nullF := S(f)
if !nullF.Valid {
t.Fatalf("expected the null.String to be valid")
}
if act := nullF.String; val != act {
t.Fatalf("expected %s, but given %s", val, act)
}
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
func assertStr(t *testing.T, s String, from string) {
if s.String != "test" {
t.Errorf("bad %s string: %s ≠ %s\n", from, s.String, "test")
}
if !s.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullStr(t *testing.T, s String, from string) {
if s.Valid {
t.Error(from, "is valid, but should be invalid")
}
}
func assertJSONEquals(t *testing.T, data []byte, cmp string, from string) {
if string(data) != cmp {
t.Errorf("bad %s data: %s ≠ %s\n", from, data, cmp)
}
}
func BenchmarkMarshalNullString(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(b *testing.PB) {
var ns String
enc := json.NewEncoder(io.Discard)
for b.Next() {
enc.Encode(&ns)
}
})
}
func assertStringEqualIsTrue(t *testing.T, a, b String) {
t.Helper()
if !a.Equal(b) {
t.Errorf("Equal() of String{\"%v\", Valid:%t} and String{\"%v\", Valid:%t} should return true", a.String, a.Valid, b.String, b.Valid)
}
}
func assertStringEqualIsFalse(t *testing.T, a, b String) {
t.Helper()
if a.Equal(b) {
t.Errorf("Equal() of String{\"%v\", Valid:%t} and String{\"%v\", Valid:%t} should return false", a.String, a.Valid, b.String, b.Valid)
}
}