-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
180 lines (159 loc) · 4.6 KB
/
assert.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
// Copyright 2022 Fortio Authors
// Minimalistic drop in eplacement for github.com/stretchr/testify/assert
package assert // import "fortio.org/assert"
import (
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
// ObjectsAreEqualValues returns true if a == b (through refection).
func ObjectsAreEqualValues(a, b interface{}) bool {
return reflect.DeepEqual(a, b)
}
// Errorf is a local variant to get the right line numbers.
func Errorf(t *testing.T, format string, rest ...interface{}) {
_, file, line, _ := runtime.Caller(2)
file = file[strings.LastIndex(file, "/")+1:]
fmt.Printf("%s:%d %s\n", file, line, fmt.Sprintf(format, rest...))
t.Fail()
}
// NotEqual checks for a not equal b.
func NotEqual(t *testing.T, a, b interface{}, msg ...string) {
if ObjectsAreEqualValues(a, b) {
Errorf(t, "%v unexpectedly equal: %v", a, msg)
}
}
// EqualValues checks for a equal b.
func EqualValues(t *testing.T, a, b interface{}, msg ...string) {
if !ObjectsAreEqualValues(a, b) {
Errorf(t, "%v unexpectedly not equal %v: %v", a, b, msg)
}
}
// Equal also checks for a equal b.
func Equal(t *testing.T, a, b interface{}, msg ...string) {
if !ObjectsAreEqualValues(a, b) {
Errorf(t, "%v unexpectedly not equal %v: %v", a, b, msg)
}
}
// NoError checks for no errors (nil).
func NoError(t *testing.T, err error, msg ...string) {
if err != nil {
Errorf(t, "expecting no error, got %v: %v", err, msg)
}
}
// Error checks/expects an error.
func Error(t *testing.T, err error, msg ...string) {
if err == nil {
Errorf(t, "expecting an error, didn't get it: %v", msg)
}
}
// True checks bool is true.
func True(t *testing.T, b bool, msg ...string) {
if !b {
Errorf(t, "expecting true, didn't: %v", msg)
}
}
// False checks bool is false.
func False(t *testing.T, b bool, msg ...string) {
if b {
Errorf(t, "expecting false, didn't: %v", msg)
}
}
// Contains checks that needle is in haystack.
func Contains(t *testing.T, haystack, needle string, msg ...string) {
if !strings.Contains(haystack, needle) {
Errorf(t, "%v doesn't contain %v: %v", haystack, needle, msg)
}
}
// Fail fails the test.
func Fail(t *testing.T, msg string) {
_, file, line, _ := runtime.Caller(1)
file = file[strings.LastIndex(file, "/")+1:]
fmt.Printf("%s:%d %s\n", file, line, msg)
t.FailNow()
}
// CheckEquals checks if actual == expect and fails the test and logs
// failure (including filename:linenum if they are not equal).
func CheckEquals(t *testing.T, actual interface{}, expected interface{}, msg interface{}) {
if expected != actual {
_, file, line, _ := runtime.Caller(1)
file = file[strings.LastIndex(file, "/")+1:]
fmt.Printf("%s:%d mismatch!\nactual:\n%+v\nexpected:\n%+v\nfor %+v\n", file, line, actual, expected, msg)
t.Fail()
}
}
// Assert is similar to True() under a different name and earlier
// fortio/stats test implementation.
func Assert(t *testing.T, cond bool, msg string, rest ...interface{}) {
if !cond {
_, file, line, _ := runtime.Caller(1)
file = file[strings.LastIndex(file, "/")+1:]
m := fmt.Sprintf(msg, rest...)
fmt.Printf("%s:%d assert failure: %s\n", file, line, m)
t.Fail()
}
}
type hasT interface {
T() *testing.T
SetT(t *testing.T)
}
// TestSuite to be used as base struct for test suites.
// replaces https://pkg.go.dev/github.com/stretchr/[email protected]/suite
type TestSuite struct {
t *testing.T
}
// T returns the current testing.T.
func (s *TestSuite) T() *testing.T {
return s.t
}
// SetT sets the testing.T in the suite object.
func (s *TestSuite) SetT(t *testing.T) {
s.t = t
}
type hasSetupTest interface {
SetupTest()
}
type hasTearDown interface {
TearDownTest()
}
// Run runs the test suite with SetupTest first and TearDownTest after.
// replaces https://pkg.go.dev/github.com/stretchr/testify/suite#Run
func Run(t *testing.T, suite hasT) {
suite.SetT(t)
tests := []testing.InternalTest{}
methodFinder := reflect.TypeOf(suite)
var setup hasSetupTest
if s, ok := suite.(hasSetupTest); ok {
setup = s
}
var tearDown hasTearDown
if td, ok := suite.(hasTearDown); ok {
tearDown = td
}
for i := 0; i < methodFinder.NumMethod(); i++ {
method := methodFinder.Method(i)
//nolint:staticcheck // consider fixing later for perf but this is just to run a few tests.
if ok, _ := regexp.MatchString("^Test", method.Name); !ok {
continue
}
test := testing.InternalTest{
Name: method.Name,
F: func(_ *testing.T) {
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
},
}
tests = append(tests, test)
}
for _, test := range tests {
if setup != nil {
setup.SetupTest()
}
t.Run(test.Name, test.F)
if tearDown != nil {
tearDown.TearDownTest()
}
}
}