-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_context.go
More file actions
57 lines (52 loc) · 1.73 KB
/
Copy pathbase_context.go
File metadata and controls
57 lines (52 loc) · 1.73 KB
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
package samurai
import (
"context"
"testing"
)
// BaseContext is the framework-provided test context.
// It implements the Context interface, providing Testing() and Cleanup().
// It also provides Context() to access the current scope's context.Context.
//
// Users can embed *BaseContext in their own struct to create custom test contexts
// for use with RunWith. The embedded methods (Testing, Cleanup, Context) are promoted,
// so the custom type satisfies Context automatically.
//
// Example:
//
// type MyCtx struct {
// *samurai.BaseContext
// *assert.Assertions
// }
type BaseContext struct {
tt *testing.T
addCleanup func(func())
}
// Context returns the context.Context for the current test scope.
// The returned context is canceled when the test completes.
// This is equivalent to calling b.Testing().Context().
//
// Context is available in RunWith factories, enabling initialization
// that requires a context:
//
// samurai.RunWith(t, func(w samurai.W) *MyCtx {
// ctx := w.Context()
// return &MyCtx{BaseContext: w, db: connectDB(ctx)}
// }, builder)
func (b *BaseContext) Context() context.Context {
return b.tt.Context()
}
// Testing returns the underlying *testing.T for use with any assertion library.
// Always returns a valid *testing.T (never nil).
func (b *BaseContext) Testing() *testing.T {
return b.tt
}
// Cleanup registers a cleanup function that runs after the current scope completes.
// Multiple cleanups execute in LIFO order (last registered runs first),
// matching the behavior of Go's t.Cleanup and defer.
// Cleanups run even if the step panics.
func (b *BaseContext) Cleanup(fn func()) {
if fn == nil {
panic(&samuraiErr{message: "Cleanup called with nil function"})
}
b.addCleanup(fn)
}