-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrial.go
More file actions
290 lines (255 loc) · 7.33 KB
/
Copy pathtrial.go
File metadata and controls
290 lines (255 loc) · 7.33 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package trial
import (
"context"
"fmt"
"os"
"reflect"
"runtime/debug"
"strings"
"testing"
"time"
)
var localTest = false
var colorEnabled bool
func init() {
// Respect NO_COLOR standard (https://no-color.org/)
if _, noColor := os.LookupEnv("NO_COLOR"); noColor {
colorEnabled = false
return
}
// Force colors if requested
if _, forceColor := os.LookupEnv("FORCE_COLOR"); forceColor {
colorEnabled = true
return
}
// Check TERM environment variable (most terminals set this)
// Empty or "dumb" means no color support
t := os.Getenv("TERM")
colorEnabled = t != "" && t != "dumb"
}
// colorRed wraps text in red ANSI color codes if colors are enabled.
func colorRed(s string) string {
if colorEnabled {
return "\033[31m" + s + "\033[0m"
}
return s
}
// colorGreen wraps text in green ANSI color codes if colors are enabled.
func colorGreen(s string) string {
if colorEnabled {
return "\033[32m" + s + "\033[0m"
}
return s
}
type (
// TestFunc a wrapper function used to setup the method being tested.
TestFunc func(in Input) (result interface{}, err error)
// CompareFunc compares actual and expected to determine equality. It should return
// a human readable string representing the differences between actual and
// expected.
// Symbols with meaning:
// "-" elements missing from actual
// "+" elements missing from expected
CompareFunc func(actual, expected interface{}) (equal bool, differences string)
testFunc[In any, Out any] func(in In) (result Out, err error)
)
// Comparer interface is implemented by an object to check for equality
// and show any differences found
type Comparer interface {
Equals(interface{}) (bool, string)
}
// Trial framework used to test different logical states
type Trial[In any, Out any] struct {
cases map[string]Case[In, Out]
testFn testFunc[In, Out]
equalFn CompareFunc
timeout time.Duration
parallel bool
}
// Cases made during the trial
type Cases[In any, Out any] map[string]Case[In, Out]
// Case made during the trial of your code
type Case[In any, Out any] struct {
Input In
Expected Out
// testing conditions
ShouldErr bool // is an error expected
ExpectedErr error // the error that was expected (nil is no error expected)
ShouldPanic bool // is a panic expected
}
func New[In any, Out any](fn func(In) (Out, error), cases map[string]Case[In, Out]) *Trial[In, Out] {
if cases == nil {
cases = make(map[string]Case[In, Out])
}
return &Trial[In, Out]{
cases: cases,
testFn: fn,
equalFn: Equal,
}
}
// EqualFn override the default comparison method used.
// see ContainsFn(x, y interface{}) (bool, string)
// deprecated
func (t *Trial[In, Out]) EqualFn(fn CompareFunc) *Trial[In, Out] {
return t.Comparer(fn)
}
// Comparer override the default comparison function.
// see Contains(x, y interface{}) (bool, string)
// see Equals(x, y interface{}) (bool, string)
func (t *Trial[In, Out]) Comparer(fn CompareFunc) *Trial[In, Out] {
t.equalFn = fn
return t
}
// Parallel enables parallel execution for subtests in SubTest().
// When enabled, each case runs as a parallel subtest using t.Parallel().
// Note: Test cases must be thread-safe when using this option.
func (t *Trial[In, Out]) Parallel() *Trial[In, Out] {
t.parallel = true
return t
}
// SubTest runs all cases as individual subtests
func (t *Trial[In, Out]) SubTest(tst testing.TB) {
if h, ok := tst.(tHelper); ok {
h.Helper()
}
for msg, test := range t.cases {
msg, test := msg, test // capture loop variables for parallel execution (Go <1.22)
tst.(*testing.T).Run(msg, func(tb *testing.T) {
tb.Helper()
if t.parallel {
tb.Parallel()
}
r := t.testCase(msg, test)
if !r.Success {
s := strings.Replace(r.Message, "\""+msg+"\"", "", 1)
s = strings.Replace(s, "FAIL:", "", 1)
tb.Error(colorRed(strings.TrimLeft(s, " \n")))
}
})
}
}
// Timeout will make sure that a test case has finished
// within the timeout or the test will fail.
func (t *Trial[In, Out]) Timeout(d time.Duration) *Trial[In, Out] {
t.timeout = d
return t
}
// Test all cases provided
func (t *Trial[In, Out]) Test(tst testing.TB) {
if h, ok := tst.(tHelper); ok {
h.Helper()
}
for msg, test := range t.cases {
r := t.testCase(msg, test)
if r.Success {
tst.Log(colorGreen(r.Message))
} else {
tst.Error(colorRed(r.Message))
}
}
}
func (t *Trial[In, Out]) testCase(msg string, test Case[In, Out]) result {
// setup
done := make(chan *result)
ctx := context.Background()
if t.timeout > time.Nanosecond {
ctx, _ = context.WithTimeout(context.Background(), t.timeout)
}
// run the test function
go func() {
r := &result{}
defer func() { // panic recovery and check
rec := recover()
r.panicCheck = rec != nil
if rec == nil && test.ShouldPanic {
r.fail("FAIL: %q did not panic", msg)
r.panicCheck = true
} else if rec != nil && !test.ShouldPanic {
r.fail("PANIC: %q %v\n%s", msg, rec, cleanStack())
} else {
r.pass("PASS: %q", msg)
}
done <- r // send result to channel
}()
r.value, r.err = t.testFn(test.Input)
}()
result := &result{}
select {
case result = <-done:
if result.panicCheck {
return *result
}
case <-ctx.Done():
result.fail("FAIL: %q timeout after %s", msg, t.timeout.String())
return *result
}
if (test.ShouldErr && result.err == nil) || (test.ExpectedErr != nil && result.err == nil) {
result.fail("FAIL: %q should error", msg)
} else if !test.ShouldErr && result.err != nil && test.ExpectedErr == nil {
result.fail("FAIL: %q unexpected error '%s'", msg, result.err.Error())
} else if test.ExpectedErr != nil && !isExpectedError(result.err, test.ExpectedErr) {
result.fail("FAIL: %q error %q does not match expected %q", msg, result.err, test.ExpectedErr)
} else if !test.ShouldErr && test.ExpectedErr == nil {
if equal, diff := t.equalFn(result.value, test.Expected); !equal {
result.fail("FAIL: %q \n%s", msg, diff)
} else {
result.pass("PASS: %q", msg)
}
}
return *result
}
// cleanStack removes unhelpful lines from a panic stack track
func cleanStack() (s string) {
for _, ln := range strings.Split(string(debug.Stack()), "\n") {
if !localTest && strings.Contains(ln, "/hydronica/trial") {
continue
}
if strings.Contains(ln, "go/src/runtime/debug/stack.go") {
continue
}
if strings.Contains(ln, "go/src/runtime/panic.go") {
continue
}
s += ln + "\n"
}
return s
}
func isExpectedError(actual, expected error) bool {
if err, ok := expected.(errCheck); ok {
return reflect.TypeOf(actual) == reflect.TypeOf(err.err)
}
return strings.Contains(actual.Error(), expected.Error())
}
type errCheck struct {
err error
}
func (e errCheck) Error() string {
return e.err.Error()
}
// ErrType can be used with ExpectedErr to check
// that the expected err is of a certain type
func ErrType(err error) error {
return errCheck{err}
}
type result struct {
Success bool
Message string
value interface{}
err error
panicCheck bool
}
func (r *result) pass(format string, args ...interface{}) {
r.Success = true
r.Message = fmt.Sprintf(format, args...)
}
func (r *result) fail(format string, args ...interface{}) {
r.Success = false
r.Message = fmt.Sprintf(format, args...)
}
func (r result) string() string {
return fmt.Sprintf("{Success: %v, Message: %s, value: %v, err: %v, paniced: %v}",
r.Success, r.Message, r.value, r.err, r.panicCheck)
}
type tHelper interface {
Helper()
}