Debian Codegraph @

assert.go

raw symbols SCIP
1 // Copyright 2022 Fortio Authors
2
3 // Minimalistic drop in eplacement for github.com/stretchr/testify/assert
4 package assert // import "fortio.org/assert"
5
6 import (
7 "fmt"
8 "reflect"
9 "regexp"
10 "runtime"
11 "strings"
12 "testing"
13 )
14
15 // ObjectsAreEqualValues returns true if a == b (through refection).
16 func ObjectsAreEqualValues(a, b interface{}) bool {
17 return reflect.DeepEqual(a, b)
18 }
19
20 // Errorf is a local variant to get the right line numbers.
21 func Errorf(t *testing.T, format string, rest ...interface{}) {
22 _, file, line, _ := runtime.Caller(2)
23 file = file[strings.LastIndex(file, "/")+1:]
24 fmt.Printf("%s:%d %s\n", file, line, fmt.Sprintf(format, rest...))
25 t.Fail()
26 }
27
28 // NotEqual checks for a not equal b.
29 func NotEqual(t *testing.T, a, b interface{}, msg ...string) {
30 if ObjectsAreEqualValues(a, b) {
31 Errorf(t, "%v unexpectedly equal: %v", a, msg)
32 }
33 }
34
35 // EqualValues checks for a equal b.
36 func EqualValues(t *testing.T, a, b interface{}, msg ...string) {
37 if !ObjectsAreEqualValues(a, b) {
38 Errorf(t, "%v unexpectedly not equal %v: %v", a, b, msg)
39 }
40 }
41
42 // Equal also checks for a equal b.
43 func Equal(t *testing.T, a, b interface{}, msg ...string) {
44 if !ObjectsAreEqualValues(a, b) {
45 Errorf(t, "%v unexpectedly not equal %v: %v", a, b, msg)
46 }
47 }
48
49 // NoError checks for no errors (nil).
50 func NoError(t *testing.T, err error, msg ...string) {
51 if err != nil {
52 Errorf(t, "expecting no error, got %v: %v", err, msg)
53 }
54 }
55
56 // Error checks/expects an error.
57 func Error(t *testing.T, err error, msg ...string) {
58 if err == nil {
59 Errorf(t, "expecting an error, didn't get it: %v", msg)
60 }
61 }
62
63 // True checks bool is true.
64 func True(t *testing.T, b bool, msg ...string) {
65 if !b {
66 Errorf(t, "expecting true, didn't: %v", msg)
67 }
68 }
69
70 // False checks bool is false.
71 func False(t *testing.T, b bool, msg ...string) {
72 if b {
73 Errorf(t, "expecting false, didn't: %v", msg)
74 }
75 }
76
77 // Contains checks that needle is in haystack.
78 func Contains(t *testing.T, haystack, needle string, msg ...string) {
79 if !strings.Contains(haystack, needle) {
80 Errorf(t, "%v doesn't contain %v: %v", haystack, needle, msg)
81 }
82 }
83
84 // Fail fails the test.
85 func Fail(t *testing.T, msg string) {
86 _, file, line, _ := runtime.Caller(1)
87 file = file[strings.LastIndex(file, "/")+1:]
88 fmt.Printf("%s:%d %s\n", file, line, msg)
89 t.FailNow()
90 }
91
92 // CheckEquals checks if actual == expect and fails the test and logs
93 // failure (including filename:linenum if they are not equal).
94 func CheckEquals(t *testing.T, actual interface{}, expected interface{}, msg interface{}) {
95 if expected != actual {
96 _, file, line, _ := runtime.Caller(1)
97 file = file[strings.LastIndex(file, "/")+1:]
98 fmt.Printf("%s:%d mismatch!\nactual:\n%+v\nexpected:\n%+v\nfor %+v\n", file, line, actual, expected, msg)
99 t.Fail()
100 }
101 }
102
103 // Assert is similar to True() under a different name and earlier
104 // fortio/stats test implementation.
105 func Assert(t *testing.T, cond bool, msg string, rest ...interface{}) {
106 if !cond {
107 _, file, line, _ := runtime.Caller(1)
108 file = file[strings.LastIndex(file, "/")+1:]
109 m := fmt.Sprintf(msg, rest...)
110 fmt.Printf("%s:%d assert failure: %s\n", file, line, m)
111 t.Fail()
112 }
113 }
114
115 type hasT interface {
116 T() *testing.T
117 SetT(t *testing.T)
118 }
119
120 // TestSuite to be used as base struct for test suites.
121 // replaces https://pkg.go.dev/github.com/stretchr/[email protected]/suite
122 type TestSuite struct {
123 t *testing.T
124 }
125
126 // T returns the current testing.T.
127 func (s *TestSuite) T() *testing.T {
128 return s.t
129 }
130
131 // SetT sets the testing.T in the suite object.
132 func (s *TestSuite) SetT(t *testing.T) {
133 s.t = t
134 }
135
136 type hasSetupTest interface {
137 SetupTest()
138 }
139 type hasTearDown interface {
140 TearDownTest()
141 }
142
143 // Run runs the test suite with SetupTest first and TearDownTest after.
144 // replaces https://pkg.go.dev/github.com/stretchr/testify/suite#Run
145 func Run(t *testing.T, suite hasT) {
146 suite.SetT(t)
147 tests := []testing.InternalTest{}
148 methodFinder := reflect.TypeOf(suite)
149 var setup hasSetupTest
150 if s, ok := suite.(hasSetupTest); ok {
151 setup = s
152 }
153 var tearDown hasTearDown
154 if td, ok := suite.(hasTearDown); ok {
155 tearDown = td
156 }
157 for i := 0; i < methodFinder.NumMethod(); i++ {
158 method := methodFinder.Method(i)
159 //nolint:staticcheck // consider fixing later for perf but this is just to run a few tests.
160 if ok, _ := regexp.MatchString("^Test", method.Name); !ok {
161 continue
162 }
163 test := testing.InternalTest{
164 Name: method.Name,
165 F: func(_ *testing.T) {
166 method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
167 },
168 }
169 tests = append(tests, test)
170 }
171 for _, test := range tests {
172 if setup != nil {
173 setup.SetupTest()
174 }
175 t.Run(test.Name, test.F)
176 if tearDown != nil {
177 tearDown.TearDownTest()
178 }
179 }
180 }