-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.go
More file actions
184 lines (159 loc) · 4.17 KB
/
tester.go
File metadata and controls
184 lines (159 loc) · 4.17 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Package tstr provides testing framework for Go programs that simplifies testing with test dependencies.
package tstr
import (
"errors"
"fmt"
"os"
"reflect"
"testing"
"github.com/go-tstr/tstr/strerr"
)
const (
ErrMissingTestFn = strerr.Error("missing Opt for test function")
ErrOverwritingTestFn = strerr.Error("trying to overwrite test function")
ErrMissingNameField = strerr.Error("missing field Name in test case struct")
ErrWrongTestCaseType = strerr.Error("wrong test case type")
)
// TestingM contains required methods from *testing.M.
type TestingM interface {
Run() int
}
// TestingT contains required methods from *testing.T.
type TestingT interface {
Run(name string, fn func(*testing.T)) bool
}
// exit allows monkey-patching os.Exit in tests.
var exit = os.Exit
// Run runs the test with the given options.
// Options are applied in the order they are passed.
// One of the options must provide the test function.
//
// Options that provide the test function:
// - WithM
// - WithFn
// - WithTable
func Run(opts ...Opt) error {
t := NewTester(opts...)
if err := t.Init(); err != nil {
return err
}
return t.Run()
}
// RunMain is a convinience wrapper around Run that can be used inside TestMain.
// RunMain applies automatically WithM option which calls m.Run.
// Also os.Exit is called with non-zero exit code if Run returns any error.
//
// Example:
//
// func TestMain(m *testing.M) {
// tstr.RunMain(m, tstr.WithDeps(MyDependency()))
// }
func RunMain(m TestingM, opts ...Opt) {
err := Run(append(opts, WithM(m))...)
if err == nil {
return
}
exitCode := 1
var eErr ExitError
if errors.As(err, &eErr) {
exitCode = int(eErr)
}
fmt.Println(err)
exit(exitCode)
}
type Tester struct {
opts []Opt
deps []Dependency
test func() error
}
// NewTester creates a new Tester with the given options.
// In most cases you should use Run function instead of creating Tester manually,
// Using NewTester can be useful if you need more control over the test execution
// or if you want to reuse same Tester instance.
func NewTester(opts ...Opt) *Tester {
return &Tester{
opts: opts,
}
}
// Init applies all options to the Tester.
func (t *Tester) Init() error {
for _, opt := range t.opts {
if err := opt(t); err != nil {
return fmt.Errorf("failed to apply option: %w", err)
}
}
if t.test == nil {
return ErrMissingTestFn
}
return nil
}
// Run starts the test dependencies, executes the test function and finally stops the dependencies.
func (t *Tester) Run() error {
r := NewRunner(t.deps...)
if err := r.Start(); err != nil {
return errors.Join(err, r.Stop())
}
err := t.test()
return errors.Join(err, r.Stop())
}
func (t *Tester) setTest(fn func() error) error {
if t.test != nil {
return ErrOverwritingTestFn
}
t.test = fn
return nil
}
type Opt func(*Tester) error
// WithDeps adds dependencies to the tester.
func WithDeps(deps ...Dependency) Opt {
return func(t *Tester) error {
t.deps = append(t.deps, deps...)
return nil
}
}
type ExitError int
func (e ExitError) Error() string { return fmt.Sprintf("exit status %d", e) }
// WithM uses the given testing.M as the test function.
func WithM(m TestingM) Opt {
return func(t *Tester) error {
return t.setTest(func() error {
if code := m.Run(); code != 0 {
return ExitError(code)
}
return nil
})
}
}
// WithFn uses the given function as the test function.
func WithFn(fn func()) Opt {
return func(t *Tester) error {
return t.setTest(func() error {
fn()
return nil
})
}
}
// WithTable runs the given test function for each test case in the table.
func WithTable[T any](tt TestingT, cases []T, test func(*testing.T, T)) Opt {
return func(t *Tester) error {
if len(cases) > 0 {
el := reflect.ValueOf(&cases[0]).Elem()
if el.Kind() != reflect.Struct {
return fmt.Errorf("%w: expected struct, got %s", ErrWrongTestCaseType, el.Kind())
}
field := el.FieldByName("Name")
if !field.IsValid() {
return ErrMissingNameField
}
}
return t.setTest(func() error {
for _, tc := range cases {
name := reflect.ValueOf(&tc).Elem().FieldByName("Name").String()
tt.Run(name, func(t *testing.T) {
test(t, tc)
})
}
return nil
})
}
}