-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patht.go
More file actions
97 lines (80 loc) · 2.06 KB
/
t.go
File metadata and controls
97 lines (80 loc) · 2.06 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
package testy
import (
"fmt"
"runtime"
"strings"
)
type t struct {
name string
tester Tester
failed bool
msgs []Msg
subtests chan<- subtest
subtestDone <-chan bool
}
type subtest struct {
name string
tester Tester
}
var _ TestingT = (*t)(nil)
// test returns whether this t is actually being used in a test. This is determined by the tester func being non-nil.
func (t *t) test() bool {
return t.tester != nil
}
func (t *t) run() {
defer func() {
// catch panics and mark test as failed
if err := recover(); err != nil {
// not using Fatalf since we're already in the defer that would get run and we need to clean up the channel
t.Errorf("panic: %+v", err)
t.Fail()
}
close(t.subtests)
}()
t.tester(t)
}
func (t *t) Fail() {
t.failed = true
}
func (t *t) FailNow() {
t.Fail()
if t.test() {
runtime.Goexit()
} else {
panic("before/after helper t failed")
}
}
func (t *t) Fatal(args ...interface{}) {
t.msgs = append(t.msgs, Msg{Msg: fmt.Sprintln(args...), Level: LevelError})
t.FailNow()
}
func (t *t) Fatalf(format string, args ...interface{}) {
t.msgs = append(t.msgs, Msg{Msg: fmt.Sprintf(format, args...), Level: LevelError})
t.FailNow()
}
func (t *t) Errorf(format string, args ...interface{}) {
t.msgs = append(t.msgs, Msg{Msg: fmt.Sprintf(format, args...), Level: LevelError})
t.failed = true
}
func (t *t) Helper() {
// nothing to do here, I think?
}
func (t *t) Log(args ...interface{}) {
t.msgs = append(t.msgs, Msg{Msg: fmt.Sprintln(args...), Level: LevelInfo})
}
func (t *t) Logf(format string, args ...interface{}) {
t.msgs = append(t.msgs, Msg{Msg: fmt.Sprintf(format, args...), Level: LevelInfo})
}
func (t *t) Run(name string, tester Tester) bool {
if !t.test() {
panic("attempting to run subtest on non-subtest-capable T (you can only Run in Tests, not Before/After)")
}
t.subtests <- subtest{
name: strings.Map(sanitizeName, name),
tester: tester,
}
return <-t.subtestDone
}
// Parallel does nothing for this implementation.
// TODO figure out how to support it.
func (*t) Parallel() {}