-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.go
More file actions
78 lines (67 loc) · 1.55 KB
/
string_test.go
File metadata and controls
78 lines (67 loc) · 1.55 KB
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
package nullish
import (
"testing"
"github.com/goccy/go-json"
)
func TestNullString_Value(t *testing.T) {
ns := NewNullString("test", true)
got, err := ns.Value()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "test" {
t.Errorf("expected 'test', got %v", got)
}
ns = NewNullString("", false)
got, err = ns.Value()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != nil {
t.Errorf("expected nil, got %v", got)
}
}
func TestNullString_Scan(t *testing.T) {
var ns NullString
err := ns.Scan("test")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ns.String != "test" || !ns.Valid {
t.Errorf("expected String='test' Valid=true, got String=%q Valid=%v", ns.String, ns.Valid)
}
err = ns.Scan(nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ns.Valid {
t.Error("expected Valid=false for nil")
}
}
func TestNullString_JSON(t *testing.T) {
ns := NewNullString("test", true)
data, err := json.Marshal(ns)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
var decoded NullString
err = json.Unmarshal(data, &decoded)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if ns.String != decoded.String || ns.Valid != decoded.Valid {
t.Errorf("roundtrip failed: expected %+v, got %+v", ns, decoded)
}
}
func BenchmarkNullString_Value(b *testing.B) {
ns := NewNullString("benchmark", true)
for i := 0; i < b.N; i++ {
_, _ = ns.Value()
}
}
func BenchmarkNullString_Scan(b *testing.B) {
for i := 0; i < b.N; i++ {
var ns NullString
_ = ns.Scan("benchmark")
}
}