-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
100 lines (93 loc) · 3.05 KB
/
example_test.go
File metadata and controls
100 lines (93 loc) · 3.05 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
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
package samurai
import (
"context"
"fmt"
"strings"
)
// ExampleRun demonstrates the test tree structure built by Run.
// Run is called inside a Go test function: func TestXxx(t *testing.T).
// This example shows path discovery — the first phase of the two-phase model.
// In real tests, Run handles both discovery and execution automatically.
func ExampleRun() {
paths, _ := collectScopedPaths(func(s *Scope) {
s.Test("with database", func(_ context.Context, _ W) {
// setup: create DB, register cleanup
}, func(s *Scope) {
s.Test("can query", func(_ context.Context, _ W) {})
s.Test("can insert", func(_ context.Context, _ W) {})
})
})
for _, p := range paths {
fmt.Println(strings.Join(p.segments, "/"))
}
// Output:
// with database/can query
// with database/can insert
}
// ExampleRunWith demonstrates the generic RunWith variant with a custom context.
// RunWith lets callbacks receive a custom type (e.g. an assertion helper)
// instead of the default *BaseContext.
func ExampleRunWith() {
type myCtx struct{ *BaseContext }
paths, _ := collectScopedPaths(func(s *TestScope[*myCtx]) {
s.Test("setup", func(_ context.Context, _ *myCtx) {
// factory creates *myCtx from *BaseContext once per test path
}, func(s *TestScope[*myCtx]) {
s.Test("check A", func(_ context.Context, _ *myCtx) {})
s.Test("check B", func(_ context.Context, _ *myCtx) {})
})
})
for _, p := range paths {
fmt.Println(strings.Join(p.segments, "/"))
}
// Output:
// setup/check A
// setup/check B
}
// ExampleRun_skip demonstrates the Skip method.
// Skip marks all tests in a scope as skipped — their callbacks never execute.
// Skip propagates to nested scopes. Call order relative to Test does not matter.
func ExampleRun_skip() {
paths, _ := collectScopedPaths(func(s *Scope) {
s.Test("stable feature", func(_ context.Context, _ W) {}, func(s *Scope) {
s.Test("works", func(_ context.Context, _ W) {})
})
s.Test("WIP feature", func(_ context.Context, _ W) {}, func(s *Scope) {
s.Skip()
s.Test("not ready", func(_ context.Context, _ W) {})
})
})
for _, p := range paths {
path := strings.Join(p.segments, "/")
if p.skipped {
fmt.Println(path + " (skipped)")
} else {
fmt.Println(path)
}
}
// Output:
// stable feature/works
// WIP feature/not ready (skipped)
}
// ExampleRun_nested demonstrates deeply nested test paths with multiple branches.
// Each leaf path executes independently with fresh parent setup.
func ExampleRun_nested() {
paths, _ := collectScopedPaths(func(s *Scope) {
s.Test("database", func(_ context.Context, _ W) {}, func(s *Scope) {
s.Test("users", func(_ context.Context, _ W) {}, func(s *Scope) {
s.Test("create", func(_ context.Context, _ W) {})
s.Test("delete", func(_ context.Context, _ W) {})
})
s.Test("posts", func(_ context.Context, _ W) {}, func(s *Scope) {
s.Test("list", func(_ context.Context, _ W) {})
})
})
})
for _, p := range paths {
fmt.Println(strings.Join(p.segments, "/"))
}
// Output:
// database/users/create
// database/users/delete
// database/posts/list
}