-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertions.go
243 lines (207 loc) · 5.44 KB
/
assertions.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
package assert
import (
"bufio"
"bytes"
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"unicode"
"unicode/utf8"
)
func IsType(t *testing.T, expectedType interface{}, object interface{}) bool {
if !objectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) {
return fail(t, fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object)))
}
return true
}
func Equal(t *testing.T, expected, actual interface{}) bool {
if objectsAreEqual(expected, actual) {
return true
}
return fail(t, fmt.Sprintf("Expected:\n%v\nBut Got:\n%v", expected, actual))
}
func Nil(t *testing.T, object interface{}) bool {
if isNil(object) {
return true
}
return fail(t, fmt.Sprintf("Expected nil, but got: %#v", object))
}
func True(t *testing.T, value bool) bool {
if !value {
return fail(t, "Should be true")
}
return true
}
func False(t *testing.T, value bool) bool {
if value {
return fail(t, "Should be false")
}
return true
}
func NoError(t *testing.T, err error) bool {
if err != nil {
return fail(t, fmt.Sprintf("Received unexpected error:\n%+v", err))
}
return true
}
func EqualError(t *testing.T, theError error, errString string) bool {
if !isError(t, theError) {
return false
}
expected := errString
actual := theError.Error()
// don't need to use deep equals here, we know they are both strings
if expected != actual {
return fail(t, fmt.Sprintf("Error message not equal:\n"+
"expected: %q\n"+
"actual : %q", expected, actual))
}
return true
}
func ErrorContains(t *testing.T, theError error, contains string) bool {
if !isError(t, theError) {
return false
}
actual := theError.Error()
if !strings.Contains(actual, contains) {
return fail(t, fmt.Sprintf("Error %#v does not contain %#v", actual, contains))
}
return true
}
func objectsAreEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}
func isError(t *testing.T, err error) bool {
if err == nil {
return fail(t, "An error is expected but got nil.")
}
return true
}
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
switch value.Kind() {
case
reflect.Chan, reflect.Func,
reflect.Interface, reflect.Map,
reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return value.IsNil()
default:
return false
}
}
func fail(t *testing.T, failureMessage string) bool {
content := []labeledContent{
{"Error Trace", strings.Join(callerInfo(), "\n\t\t\t")},
{"Error", failureMessage},
}
t.Errorf("\n%s", ""+labeledOutput(content...))
return false
}
func callerInfo() []string {
var pc uintptr
var ok bool
var file string
var line int
var name string
var callers []string
for i := 0; ; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
// This is a huge edge case, but it will panic if this is the case, see #180
if file == "<autogenerated>" {
break
}
f := runtime.FuncForPC(pc)
if f == nil {
break
}
name = f.Name()
// testing.tRunner is the standard library function that calls
// tests. Subtests are called directly by tRunner, without going through
// the Test/Benchmark/Example function that contains the t.Run calls, so
// with subtests we should break when we hit tRunner, without adding it
// to the list of callers.
if name == "testing.tRunner" {
break
}
parts := strings.Split(file, "/")
if len(parts) > 1 {
filename := parts[len(parts)-1]
dir := parts[len(parts)-2]
if (dir != "github.com/apimatic/go-core-runtime/assert" && dir != "mock" && dir != "require") || filename == "mock_test.go" {
callers = append(callers, fmt.Sprintf("%s:%d", file, line))
}
}
// Drop the package
segments := strings.Split(name, ".")
name = segments[len(segments)-1]
if isTest(name, "Test") ||
isTest(name, "Benchmark") ||
isTest(name, "Example") {
break
}
}
return callers
}
func isTest(name, prefix string) bool {
if !strings.HasPrefix(name, prefix) {
return false
}
if len(name) == len(prefix) { // "Test" is ok
return true
}
r, _ := utf8.DecodeRuneInString(name[len(prefix):])
return !unicode.IsLower(r)
}
type labeledContent struct {
label string
content string
}
func labeledOutput(content ...labeledContent) string {
longestLabel := 0
for _, v := range content {
if len(v.label) > longestLabel {
longestLabel = len(v.label)
}
}
var output string
for _, v := range content {
output += "\t" + v.label + ":" + strings.Repeat(" ", longestLabel-len(v.label)) + "\t" + indentMessageLines(v.content, longestLabel) + "\n"
}
return output
}
func indentMessageLines(message string, longestLabelLen int) string {
outBuf := new(bytes.Buffer)
for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
// no need to align first line because it starts at the correct location (after the label)
if i != 0 {
// append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab
outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t")
}
outBuf.WriteString(scanner.Text())
}
return outBuf.String()
}