/
tester.go
172 lines (148 loc) · 4.64 KB
/
tester.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
package must
import (
"fmt"
"reflect"
"github.com/kylelemons/godebug/diff"
"github.com/kylelemons/godebug/pretty"
)
var _ MustTester = Tester{}
/*
Tester implements MustTester and provides a TestingT to be used for all check functions.
*/
type Tester struct {
T TestingT // *testing.T or equivalent
InterfaceComparison func(expected, got interface{}) bool // Optional custom interface comparison function
InterfaceDiff func(expected, got interface{}) string // Optional custom interace diff function
}
/*
BeEqual compares the expected and got interfaces, triggering an error on the Tester's T if they are not equal.
This corresponds to the function BeEqual
*/
func (tester Tester) BeEqual(expected, got interface{}, a ...interface{}) bool {
tester.T.Helper()
if !tester.equal(expected, got) {
tester.formattedError("diff\n%s", a, tester.diff(expected, got))
return false
}
return true
}
/*
BeEqualErrors compares the expected and got errors, triggering an error on the Tester's T if they are not equal.
This corresponds to the function BeEqualErrors
*/
func (tester Tester) BeEqualErrors(expected, got error, a ...interface{}) bool {
tester.T.Helper()
if expected == nil && got == nil {
return true
}
if (expected == nil || got == nil) || expected.Error() != got.Error() {
tester.formattedError("Expected '%v', got '%v'", a, getErrMessage(expected), getErrMessage(got))
return false
}
return true
}
/*
BeNoError checks whether got is set, triggering an error on the Tester's T if it is non-nil.
This corresponds to the function BeNoError
*/
func (tester Tester) BeNoError(got error, a ...interface{}) bool {
tester.T.Helper()
if got == nil {
return true
}
tester.formattedError("error: %s", a, got.Error())
return false
}
/*
BeSameLength checks whether the two inputs have the same length according to the len function.
This corresponds to the function BeSameLength
*/
func (tester Tester) BeSameLength(expected, got interface{}, a ...interface{}) bool {
tester.T.Helper()
lenExpected, err := lenterface(expected)
if err != nil {
tester.formattedError("could not test lengths - %v", a, err)
return false
}
lenGot, err := lenterface(got)
if err != nil {
tester.formattedError("could not test lengths - %v", a, err)
return false
}
if lenExpected == lenGot {
return true
}
tester.formattedError("expected length %d, got length %d", a, lenExpected, lenGot)
return false
}
// BeError checks that the received error is not nil
func (tester Tester) BeError(got error, a ...interface{}) bool {
tester.T.Helper()
if got != nil {
return true
}
tester.formattedError("expected an error, but got nil", a)
return false
}
// BeErrorIf checks that the received error corresponds to the errorExpected flag
func (tester Tester) BeErrorIf(errorExpected bool, got error, a ...interface{}) bool {
tester.T.Helper()
if errorExpected {
return tester.BeError(got, a...)
}
return tester.BeNoError(got, a...)
}
func lenterface(val interface{}) (int, error) {
kind := reflect.TypeOf(val).Kind()
switch kind {
case reflect.Slice, reflect.Map, reflect.String, reflect.Chan, reflect.Array:
s := reflect.ValueOf(val)
return s.Len(), nil
case reflect.Ptr:
return lenterfacePtr(reflect.ValueOf(val))
}
return 0, fmt.Errorf("cannot get the length of type: %v", kind)
}
func lenterfacePtr(val reflect.Value) (int, error) {
i := reflect.Indirect(val)
switch i.Kind() {
case reflect.Slice, reflect.Map, reflect.String, reflect.Chan, reflect.Array:
return i.Len(), nil
}
return 0, fmt.Errorf("cannot get the length of a pointer to type: %v", i.Kind())
}
func (tester Tester) equal(expected, got interface{}) bool {
if tester.InterfaceComparison != nil {
return tester.InterfaceComparison(expected, got)
}
return pretty.Compare(expected, got) == ""
}
func (tester Tester) diff(expected, got interface{}) string {
if tester.InterfaceDiff != nil {
return tester.InterfaceDiff(expected, got)
}
// Do string diff if strings. Compare does not handle multiline strings well
e, eok := expected.(string)
g, gok := got.(string)
if eok && gok {
return fmt.Sprintf("(- expected, + got)\n%v", diff.Diff(e, g))
}
return fmt.Sprintf("(- expected, + got)\n%v", pretty.Compare(expected, got))
}
func (tester Tester) formattedError(format string, a []interface{}, following ...interface{}) {
tester.T.Helper()
if len(a) > 0 {
var args []interface{}
args = append(args, fmt.Sprint(a...))
args = append(args, following...)
tester.T.Errorf("%v: "+format, args...)
} else {
tester.T.Errorf(format, following...)
}
}
func getErrMessage(err error) string {
if err != nil {
return err.Error()
}
return "<nil>"
}