@@ -19,21 +19,14 @@ package ktesting
19
19
import (
20
20
"errors"
21
21
"fmt"
22
- "regexp"
23
22
"testing"
24
23
"time"
25
24
26
25
"github.com/onsi/gomega"
27
- "github.com/stretchr/testify/assert"
28
26
)
29
27
30
- func TestAsync (t * testing.T ) {
31
- for name , tc := range map [string ]struct {
32
- cb func (TContext )
33
- expectNoFail bool
34
- expectError string
35
- expectDuration time.Duration
36
- }{
28
+ func TestAssert (t * testing.T ) {
29
+ for name , tc := range map [string ]testcase {
37
30
"eventually-timeout" : {
38
31
cb : func (tCtx TContext ) {
39
32
Eventually (tCtx , func (tCtx TContext ) int {
@@ -165,30 +158,114 @@ The function passed to Consistently returned the following error:
165
158
expectError : `Timed out while waiting on TryAgainAfter after x.y s.
166
159
told to try again after 1ms: intermittent error` ,
167
160
},
161
+
162
+ "expect-equal" : {
163
+ cb : func (tCtx TContext ) {
164
+ tCtx .Expect (1 ).To (gomega .Equal (42 ))
165
+ },
166
+ expectError : `Expected
167
+ <int>: 1
168
+ to equal
169
+ <int>: 42` ,
170
+ },
171
+
172
+ "expect-no-error-success" : {
173
+ cb : func (tCtx TContext ) {
174
+ tCtx .ExpectNoError (nil )
175
+ },
176
+ expectNoFail : true ,
177
+ },
178
+ "expect-no-error-normal-error" : {
179
+ cb : func (tCtx TContext ) {
180
+ tCtx .ExpectNoError (errors .New ("fake error" ))
181
+ },
182
+ expectError : `Unexpected error: fake error` ,
183
+ expectLog : `<klog header>: Unexpected error:
184
+ <*errors.errorString | 0xXXXX>:
185
+ fake error
186
+ {s: "fake error"}
187
+ ` ,
188
+ },
189
+ "expect-no-error-failure" : {
190
+ cb : func (tCtx TContext ) {
191
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" }))
192
+ },
193
+ expectError : `doing something: fake error` ,
194
+ },
195
+ "expect-no-error-explanation-string" : {
196
+ cb : func (tCtx TContext ) {
197
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" }), "testing error checking" )
198
+ },
199
+ expectError : `testing error checking: doing something: fake error` ,
200
+ },
201
+ "expect-no-error-explanation-printf" : {
202
+ cb : func (tCtx TContext ) {
203
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" }), "testing %s %d checking" , "error" , 42 )
204
+ },
205
+ expectError : `testing error 42 checking: doing something: fake error` ,
206
+ },
207
+ "expect-no-error-explanation-callback" : {
208
+ cb : func (tCtx TContext ) {
209
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" }), func () string { return "testing error checking" })
210
+ },
211
+ expectError : `testing error checking: doing something: fake error` ,
212
+ },
213
+ "expect-no-error-backtrace" : {
214
+ cb : func (tCtx TContext ) {
215
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" , FullStackTrace : "abc\n xyz" }))
216
+ },
217
+ expectError : `doing something: fake error` ,
218
+ expectLog : `<klog header>: Failed at:
219
+ abc
220
+ xyz
221
+ ` ,
222
+ },
223
+ "expect-no-error-backtrace-and-explanation" : {
224
+ cb : func (tCtx TContext ) {
225
+ tCtx .ExpectNoError (fmt .Errorf ("doing something: %w" , FailureError {Msg : "fake error" , FullStackTrace : "abc\n xyz" }), "testing error checking" )
226
+ },
227
+ expectError : `testing error checking: doing something: fake error` ,
228
+ expectLog : `<klog header>: testing error checking
229
+ <klog header>: Failed at:
230
+ abc
231
+ xyz
232
+ ` ,
233
+ },
234
+
235
+ "output" : {
236
+ cb : func (tCtx TContext ) {
237
+ tCtx .Log ("Log" , "a" , "b" , 42 )
238
+ tCtx .Logf ("Logf %s %s %d" , "a" , "b" , 42 )
239
+ tCtx .Error ("Error" , "a" , "b" , 42 )
240
+ tCtx .Errorf ("Errorf %s %s %d" , "a" , "b" , 42 )
241
+ },
242
+ expectLog : `<klog header>: Log a b 42
243
+ <klog header>: Logf a b 42
244
+ ` ,
245
+ expectError : `Error a b 42
246
+ Errorf a b 42` ,
247
+ },
248
+ "fatal" : {
249
+ cb : func (tCtx TContext ) {
250
+ tCtx .Fatal ("Error" , "a" , "b" , 42 )
251
+ // not reached
252
+ tCtx .Log ("Log" )
253
+ },
254
+ expectError : `Error a b 42` ,
255
+ },
256
+ "fatalf" : {
257
+ cb : func (tCtx TContext ) {
258
+ tCtx .Fatalf ("Error %s %s %d" , "a" , "b" , 42 )
259
+ // not reached
260
+ tCtx .Log ("Log" )
261
+ },
262
+ expectError : `Error a b 42` ,
263
+ },
168
264
} {
169
265
tc := tc
170
266
t .Run (name , func (t * testing.T ) {
171
267
t .Parallel ()
172
- tCtx := Init (t )
173
- var err error
174
- tCtx , finalize := WithError (tCtx , & err )
175
- start := time .Now ()
176
- func () {
177
- defer finalize ()
178
- tc .cb (tCtx )
179
- }()
180
- duration := time .Since (start )
181
- assert .InDelta (t , tc .expectDuration .Seconds (), duration .Seconds (), 0.1 , fmt .Sprintf ("callback invocation duration %s" , duration ))
182
- assert .Equal (t , ! tc .expectNoFail , tCtx .Failed (), "Failed()" )
183
- if tc .expectError == "" {
184
- assert .NoError (t , err )
185
- } else if assert .NotNil (t , err ) {
186
- t .Logf ("Result:\n %s" , err .Error ())
187
- errMsg := err .Error ()
188
- errMsg = regexp .MustCompile (`[[:digit:]]+\.[[:digit:]]+s` ).ReplaceAllString (errMsg , "x.y s" )
189
- errMsg = regexp .MustCompile (`0x[[:xdigit:]]+` ).ReplaceAllString (errMsg , "0xXXXX" )
190
- assert .Equal (t , tc .expectError , errMsg )
191
- }
268
+ tc .run (t )
192
269
})
193
270
}
194
271
}
0 commit comments