This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
180 lines (150 loc) · 3.25 KB
/
util_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
package fig
import (
"path/filepath"
"reflect"
"regexp"
"testing"
"time"
)
func Test_stringSlice(t *testing.T) {
for _, tc := range []struct {
In string
Want []string
}{
{
In: "false",
Want: []string{"false"},
},
{
In: "1,5,2",
Want: []string{"1", "5", "2"},
},
{
In: "[hello , world]",
Want: []string{"hello ", " world"},
},
{
In: "[foo]",
Want: []string{"foo"},
},
} {
t.Run(tc.In, func(t *testing.T) {
got := stringSlice(tc.In)
if !reflect.DeepEqual(tc.Want, got) {
t.Fatalf("want %+v, got %+v", tc.Want, got)
}
})
}
}
func Test_fileExists(t *testing.T) {
dir := filepath.Join("testdata", "valid")
ok := fileExists(dir)
if ok {
t.Errorf("fileExists(dir) == true, expected false")
}
bad := "oompa-loompa"
ok = fileExists(bad)
if ok {
t.Errorf("fileExists(bad) == true, expected false")
}
good := "fig.go"
ok = fileExists(good)
if !ok {
t.Errorf("fileExists(good) == false, expected true")
}
}
func Test_isStructPtr(t *testing.T) {
type cfgType struct{}
var cfg cfgType
ok := isStructPtr(cfg)
if ok {
t.Errorf("isStructPtr(cfg) == true, expected false")
}
ok = isStructPtr(&cfg)
if !ok {
t.Errorf("isStructPtr(*cfg) == false, expected true")
}
var i int
ok = isStructPtr(&i)
if ok {
t.Errorf("isStructPtr(*i) == true, expected false")
}
}
func Test_isZero(t *testing.T) {
t.Run("nil slice is zero", func(t *testing.T) {
var s []string
if isZero(reflect.ValueOf(s)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("empty slice is zero", func(t *testing.T) {
s := []string{}
if isZero(reflect.ValueOf(s)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("nil pointer is zero", func(t *testing.T) {
var s *string
if isZero(reflect.ValueOf(s)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("non-nil pointer is not zero", func(t *testing.T) {
var a *string
b := "b"
a = &b
if isZero(reflect.ValueOf(a)) == true {
t.Fatalf("isZero == true")
}
})
t.Run("struct is not zero", func(t *testing.T) {
a := struct {
B string
}{}
if isZero(reflect.ValueOf(a)) == true {
t.Fatalf("isZero == true")
}
})
t.Run("zero time is zero", func(t *testing.T) {
td := time.Time{}
if isZero(reflect.ValueOf(td)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("non-zero time is not zero", func(t *testing.T) {
td := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
if isZero(reflect.ValueOf(td)) == true {
t.Fatalf("isZero == true")
}
})
t.Run("zero regexp is zero", func(t *testing.T) {
var re *regexp.Regexp
if isZero(reflect.ValueOf(re)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("non-zero regexp is not zero", func(t *testing.T) {
re := regexp.MustCompile(".*")
if isZero(reflect.ValueOf(re)) == true {
t.Fatalf("isZero == true")
}
})
t.Run("reflect invalid is zero", func(t *testing.T) {
var x interface{}
if isZero(reflect.ValueOf(&x).Elem().Elem()) == false {
t.Fatalf("isZero == false")
}
})
t.Run("0 int is zero", func(t *testing.T) {
x := 0
if isZero(reflect.ValueOf(x)) == false {
t.Fatalf("isZero == false")
}
})
t.Run("5 int is not zero", func(t *testing.T) {
x := 5
if isZero(reflect.ValueOf(x)) == true {
t.Fatalf("isZero == true")
}
})
}