-
Notifications
You must be signed in to change notification settings - Fork 3
/
testUtils.go
206 lines (169 loc) · 4.24 KB
/
testUtils.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"testing"
)
const (
printCallStackOnFailingTest = false
)
var (
isTestEnvironment = false
)
func callStack() {
if printCallStackOnFailingTest {
debug.PrintStack()
}
}
// IsTestEnvironment should indicate whether the current run is a test run.
func IsTestEnvironment() bool {
return isTestEnvironment
}
// SetIsTestEnvironment sets a flag that indicates that we are currently in
// a test environment.
func SetIsTestEnvironment(isTestEnv bool) {
isTestEnvironment = isTestEnv
}
func expectEqual(t *testing.T, got interface{}, exp interface{}) {
t.Helper()
if exp == got {
return
}
callStack()
t.Errorf("Expected '%v' (type %v), got '%v' (type %v)", exp, reflect.TypeOf(exp), got, reflect.TypeOf(got))
}
func expectNotEqual(t *testing.T, got interface{}, notExp interface{}) {
t.Helper()
typeNotExp := reflect.TypeOf(notExp)
typeGot := reflect.TypeOf(got)
// we always require that both types are identical.
// Without that, testing can be a real pain
if typeNotExp != typeGot {
callStack()
t.Errorf("Types do not match! Expected '%v', got '%v'", typeNotExp, typeGot)
return
}
if notExp == got {
callStack()
t.Errorf("Expected something different than '%v' (type %v)", notExp, typeNotExp)
}
}
func expectNil(t *testing.T, got interface{}) {
// do NOT use with errors! This can lead to strange results
t.Helper()
if !reflect.ValueOf(got).IsNil() {
callStack()
t.Errorf("Expected nil, got '%v' (Type %v)", got, reflect.TypeOf(got))
}
}
func expectNotNil(t *testing.T, got interface{}) {
// do NOT use with errors! This can lead to strange results
t.Helper()
if reflect.ValueOf(got).IsNil() {
callStack()
t.Errorf("Expected not nil, got '%v' (Type %v)", got, reflect.TypeOf(got))
}
}
func expectError(t *testing.T, err error) {
t.Helper()
if err == nil {
callStack()
t.Error("Expected an error, got nil")
}
}
func expectNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
callStack()
t.Errorf("Expected no error, got '%v'", err)
}
}
func expectNotSet(t *testing.T, got interface{}) {
t.Helper()
if IsSet(got) {
callStack()
t.Errorf("Expected not set, got '%v'", got)
}
}
func expectAllExportedSet(t *testing.T, got interface{}) {
t.Helper()
vGot := reflect.ValueOf(got)
switch vGot.Kind() {
case reflect.Struct:
for i := 0; i < vGot.NumField(); i++ {
field := vGot.Field(i)
if field.CanInterface() {
expectAllExportedSet(t, field.Interface())
}
}
case reflect.Ptr:
if IsSet(got) {
expectAllExportedSet(t, vGot.Elem().Interface())
} else {
callStack()
t.Errorf("Expected to be set, but was not: %v / %v", vGot.Type(), vGot.Kind())
}
default:
if !IsSet(got) {
callStack()
t.Errorf("Expected to be set, but was not: %v / %v", vGot.Type(), vGot.Kind())
}
}
}
func expectFileExists(t *testing.T, filename string) {
t.Helper()
info, err := os.Stat(filename)
if err != nil {
t.Errorf("Expected file '%v' is missing: %v", filename, err)
// for debugging reasons, provide all filenames from the containing directory as well
dirname := filepath.Dir(filename)
files, errDir := ioutil.ReadDir(dirname)
if errDir != nil {
t.Logf("Cannot read dir '%v' to analyze issue: %v", dirname, errDir)
return
}
t.Logf("Files in directory %v:", dirname)
for _, file := range files {
t.Logf("- %v\n", file.Name())
}
} else if info.IsDir() {
t.Errorf("Expected file '%v' is a directory", filename)
}
}
func expectKeyExists(t *testing.T, tMap interface{}, key interface{}) {
t.Helper()
vMap := reflect.ValueOf(tMap)
if vMap.Kind() != reflect.Map {
panic("Only maps should be provided here")
}
keyKind := reflect.ValueOf(key).Kind()
mapVKeys := vMap.MapKeys()
for _, vKey := range mapVKeys {
if keyKind != vKey.Kind() {
t.Errorf("Key kinds do not match! '%v' vs '%v'", keyKind, vKey.Kind())
return
}
if key == vKey.Interface() {
return
}
}
callStack()
t.Errorf("Key '%v' was not found in map '%v'", key, tMap)
}
func expectTrue(t *testing.T, v bool) {
t.Helper()
if !v {
callStack()
t.Errorf("Expected true, but was false")
}
}
func expectFalse(t *testing.T, v bool) {
t.Helper()
if v {
callStack()
t.Errorf("Expected false, but was true")
}
}