| 1 | // Copyright 2022 Fortio Authors |
| 2 | |
| 3 | // Minimalistic drop in eplacement for github.com/stretchr/testify/assert |
| 4 | package assertscip-go gomod fortio.org/assert . `fortio.org/assert`/ // import "fortio.org/assert" |
| 5 | |
| 6 | import ( |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 8 | "reflectscip-go gomod github.com/golang/go/src go1.18 reflect/" |
| 9 | "regexpscip-go gomod github.com/golang/go/src go1.18 regexp/" |
| 10 | "runtimescip-go gomod github.com/golang/go/src go1.18 runtime/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 12 | "testingscip-go gomod github.com/golang/go/src go1.18 testing/" |
| 13 | ) |
| 14 | |
| 15 | // ObjectsAreEqualValues returns true if a == b (through refection). |
| 16 | func ObjectsAreEqualValuesscip-go gomod fortio.org/assert . `fortio.org/assert`/ObjectsAreEqualValues().(a, b interface{}) bool { |
| 17 | return reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.18 reflect/DeepEqual().(a, b) |
| 18 | } |
| 19 | |
| 20 | // Errorf is a local variant to get the right line numbers. |
| 21 | func Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, format string, rest ...interface{}) { |
| 22 | _, file, line, _ := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Callerscip-go gomod github.com/golang/go/src go1.18 runtime/Caller().(2) |
| 23 | file = file[stringsscip-go gomod github.com/golang/go/src go1.18 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.18 strings/LastIndex().(file, "/")+1:] |
| 24 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printfscip-go gomod github.com/golang/go/src go1.18 fmt/Printf().("%s:%d %s\n", file, line, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().(format, rest...)) |
| 25 | t.Failscip-go gomod github.com/golang/go/src go1.18 testing/common#Fail().() |
| 26 | } |
| 27 | |
| 28 | // NotEqual checks for a not equal b. |
| 29 | func NotEqualscip-go gomod fortio.org/assert . `fortio.org/assert`/NotEqual().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, a, b interface{}, msg ...string) { |
| 30 | if ObjectsAreEqualValuesscip-go gomod fortio.org/assert . `fortio.org/assert`/ObjectsAreEqualValues().(a, b) { |
| 31 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "%v unexpectedly equal: %v", a, msg) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // EqualValues checks for a equal b. |
| 36 | func EqualValuesscip-go gomod fortio.org/assert . `fortio.org/assert`/EqualValues().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, a, b interface{}, msg ...string) { |
| 37 | if !ObjectsAreEqualValuesscip-go gomod fortio.org/assert . `fortio.org/assert`/ObjectsAreEqualValues().(a, b) { |
| 38 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "%v unexpectedly not equal %v: %v", a, b, msg) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Equal also checks for a equal b. |
| 43 | func Equalscip-go gomod fortio.org/assert . `fortio.org/assert`/Equal().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, a, b interface{}, msg ...string) { |
| 44 | if !ObjectsAreEqualValuesscip-go gomod fortio.org/assert . `fortio.org/assert`/ObjectsAreEqualValues().(a, b) { |
| 45 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "%v unexpectedly not equal %v: %v", a, b, msg) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // NoError checks for no errors (nil). |
| 50 | func NoErrorscip-go gomod fortio.org/assert . `fortio.org/assert`/NoError().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, err error, msg ...string) { |
| 51 | if err != nil { |
| 52 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "expecting no error, got %v: %v", err, msg) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Error checks/expects an error. |
| 57 | func Errorscip-go gomod fortio.org/assert . `fortio.org/assert`/Error().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, err error, msg ...string) { |
| 58 | if err == nil { |
| 59 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "expecting an error, didn't get it: %v", msg) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // True checks bool is true. |
| 64 | func Truescip-go gomod fortio.org/assert . `fortio.org/assert`/True().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, b bool, msg ...string) { |
| 65 | if !b { |
| 66 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "expecting true, didn't: %v", msg) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // False checks bool is false. |
| 71 | func Falsescip-go gomod fortio.org/assert . `fortio.org/assert`/False().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, b bool, msg ...string) { |
| 72 | if b { |
| 73 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "expecting false, didn't: %v", msg) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Contains checks that needle is in haystack. |
| 78 | func Containsscip-go gomod fortio.org/assert . `fortio.org/assert`/Contains().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, haystack, needle string, msg ...string) { |
| 79 | if !stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Containsscip-go gomod github.com/golang/go/src go1.18 strings/Contains().(haystack, needle) { |
| 80 | Errorfscip-go gomod fortio.org/assert . `fortio.org/assert`/Errorf().(t, "%v doesn't contain %v: %v", haystack, needle, msg) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Fail fails the test. |
| 85 | func Failscip-go gomod fortio.org/assert . `fortio.org/assert`/Fail().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, msg string) { |
| 86 | _, file, line, _ := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Callerscip-go gomod github.com/golang/go/src go1.18 runtime/Caller().(1) |
| 87 | file = file[stringsscip-go gomod github.com/golang/go/src go1.18 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.18 strings/LastIndex().(file, "/")+1:] |
| 88 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printfscip-go gomod github.com/golang/go/src go1.18 fmt/Printf().("%s:%d %s\n", file, line, msg) |
| 89 | t.FailNowscip-go gomod github.com/golang/go/src go1.18 testing/common#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 CheckEqualsscip-go gomod fortio.org/assert . `fortio.org/assert`/CheckEquals().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, actual interface{}, expected interface{}, msg interface{}) { |
| 95 | if expected != actual { |
| 96 | _, file, line, _ := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Callerscip-go gomod github.com/golang/go/src go1.18 runtime/Caller().(1) |
| 97 | file = file[stringsscip-go gomod github.com/golang/go/src go1.18 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.18 strings/LastIndex().(file, "/")+1:] |
| 98 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printfscip-go gomod github.com/golang/go/src go1.18 fmt/Printf().("%s:%d mismatch!\nactual:\n%+v\nexpected:\n%+v\nfor %+v\n", file, line, actual, expected, msg) |
| 99 | t.Failscip-go gomod github.com/golang/go/src go1.18 testing/common#Fail().() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Assert is similar to True() under a different name and earlier |
| 104 | // fortio/stats test implementation. |
| 105 | func Assertscip-go gomod fortio.org/assert . `fortio.org/assert`/Assert().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, cond bool, msg string, rest ...interface{}) { |
| 106 | if !cond { |
| 107 | _, file, line, _ := runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.Callerscip-go gomod github.com/golang/go/src go1.18 runtime/Caller().(1) |
| 108 | file = file[stringsscip-go gomod github.com/golang/go/src go1.18 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.18 strings/LastIndex().(file, "/")+1:] |
| 109 | m := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().(msg, rest...) |
| 110 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printfscip-go gomod github.com/golang/go/src go1.18 fmt/Printf().("%s:%d assert failure: %s\n", file, line, m) |
| 111 | t.Failscip-go gomod github.com/golang/go/src go1.18 testing/common#Fail().() |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | type hasTscip-go gomod fortio.org/assert . `fortio.org/assert`/hasT# interface { |
| 116 | Tscip-go gomod fortio.org/assert . `fortio.org/assert`/hasT#T.() *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T# |
| 117 | SetTscip-go gomod fortio.org/assert . `fortio.org/assert`/hasT#SetT.(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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 TestSuitescip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite# struct { |
| 123 | tscip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#t. *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T# |
| 124 | } |
| 125 | |
| 126 | // T returns the current testing.T. |
| 127 | func (s *TestSuitescip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#) Tscip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#T().() *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T# { |
| 128 | return s.tscip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#t. |
| 129 | } |
| 130 | |
| 131 | // SetT sets the testing.T in the suite object. |
| 132 | func (s *TestSuitescip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#) SetTscip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#SetT().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 133 | s.tscip-go gomod fortio.org/assert . `fortio.org/assert`/TestSuite#t. = t |
| 134 | } |
| 135 | |
| 136 | type hasSetupTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasSetupTest# interface { |
| 137 | SetupTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasSetupTest#SetupTest.() |
| 138 | } |
| 139 | type hasTearDownscip-go gomod fortio.org/assert . `fortio.org/assert`/hasTearDown# interface { |
| 140 | TearDownTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasTearDown#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 Runscip-go gomod fortio.org/assert . `fortio.org/assert`/Run().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#, suite hasTscip-go gomod fortio.org/assert . `fortio.org/assert`/hasT#) { |
| 146 | suite.SetTscip-go gomod fortio.org/assert . `fortio.org/assert`/hasT#SetT.(t) |
| 147 | tests := []testingscip-go gomod github.com/golang/go/src go1.18 testing/.InternalTestscip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#{} |
| 148 | methodFinder := reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.18 reflect/TypeOf().(suite) |
| 149 | var setup hasSetupTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasSetupTest# |
| 150 | if s, ok := suite.(hasSetupTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasSetupTest#); ok { |
| 151 | setup = s |
| 152 | } |
| 153 | var tearDown hasTearDownscip-go gomod fortio.org/assert . `fortio.org/assert`/hasTearDown# |
| 154 | if td, ok := suite.(hasTearDownscip-go gomod fortio.org/assert . `fortio.org/assert`/hasTearDown#); ok { |
| 155 | tearDown = td |
| 156 | } |
| 157 | for i := 0; i < methodFinder.NumMethodscip-go gomod github.com/golang/go/src go1.18 reflect/Type#NumMethod().(); i++ { |
| 158 | method := methodFinder.Methodscip-go gomod github.com/golang/go/src go1.18 reflect/Type#Method().(i) |
| 159 | //nolint:staticcheck // consider fixing later for perf but this is just to run a few tests. |
| 160 | if ok, _ := regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.MatchStringscip-go gomod github.com/golang/go/src go1.18 regexp/MatchString().("^Test", method.Namescip-go gomod github.com/golang/go/src go1.18 reflect/Method#Name.); !ok { |
| 161 | continue |
| 162 | } |
| 163 | test := testingscip-go gomod github.com/golang/go/src go1.18 testing/.InternalTestscip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#{ |
| 164 | Namescip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#Name.: method.Namescip-go gomod github.com/golang/go/src go1.18 reflect/Method#Name., |
| 165 | Fscip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#F.: func(_ *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) { |
| 166 | method.Funcscip-go gomod github.com/golang/go/src go1.18 reflect/Method#Func..Callscip-go gomod github.com/golang/go/src go1.18 reflect/Value#Call().([]reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Valuescip-go gomod github.com/golang/go/src go1.18 reflect/Value#{reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.18 reflect/ValueOf().(suite)}) |
| 167 | }, |
| 168 | } |
| 169 | tests = append(tests, test) |
| 170 | } |
| 171 | for _, test := range tests { |
| 172 | if setup != nil { |
| 173 | setup.SetupTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasSetupTest#SetupTest.() |
| 174 | } |
| 175 | t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(test.Namescip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#Name., test.Fscip-go gomod github.com/golang/go/src go1.18 testing/InternalTest#F.) |
| 176 | if tearDown != nil { |
| 177 | tearDown.TearDownTestscip-go gomod fortio.org/assert . `fortio.org/assert`/hasTearDown#TearDownTest.() |
| 178 | } |
| 179 | } |
| 180 | } |