-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith_dependency.go
80 lines (72 loc) · 1.67 KB
/
with_dependency.go
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
package testit
import (
"testing"
"github.com/stretchr/testify/require"
)
type (
Hook[D any] struct {
*assertions
T *testing.T
Dependencies *D
After func()
}
setup[D any] struct {
hooks []func(h *Hook[D])
}
DTest[D any] struct {
*require.Assertions
T *testing.T
Dependencies *D
}
)
func Setup[D any](s func(t *Hook[D])) setup[D] {
return setup[D]{
hooks: []func(h *Hook[D]){s},
}
}
// Hook is a function to add a hook to the test setup.
// The hook allows you to perform setup and teardown actions before and after the test case.
// You can run teardown actions by assigning a function to the After field of the CHook struct.
// Example:
//
// setup := testit.SetupWithTestCase(func(t *testit.Hook[dependencies]) {
// println("setup")
// t.After = func() {
// println("teardown")
// }
// })
func (th setup[D]) Hook(hook ...func(t *Hook[D])) setup[D] {
th.hooks = append(th.hooks, hook...)
return th
}
func (th setup[D]) Expect(steps ...func(t DTest[D])) func(*testing.T) {
return func(t *testing.T) {
NotPanics(t, func() {
assertions := require.New(t)
dependencies := initializeMocks[D](t)
after := make([]func(), 0, len(th.hooks))
for _, pre := range th.hooks {
hook := &Hook[D]{
assertions: assertions,
Dependencies: dependencies,
T: t,
}
pre(hook)
dependencies = hook.Dependencies
if hook.After != nil {
after = append(after, hook.After)
}
}
for _, step := range steps {
step(DTest[D]{
T: t,
Assertions: assertions,
Dependencies: dependencies,
})
}
for _, afterFunc := range after {
afterFunc()
}
})
}
}