-
Notifications
You must be signed in to change notification settings - Fork 32
/
mocktester.go
166 lines (145 loc) · 3.77 KB
/
mocktester.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
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
package mocktesting
import (
"fmt"
"log"
"os"
"sync"
"testing"
)
// MockTester is a mock tester. This is used for making sure a test fails
// using the testing.TB interface and is useful for testing error cases in other test helpers.
type MockTester struct {
name string
mu sync.RWMutex
output []string
skipped, failed bool
// print errors is whether or not to print errors
printErrors bool
// whether or not to print logs
printLogs bool
// outputHandler specifies how to tread output
outputHandler func(...interface{})
// The testing.TB interface has an unexported method "to prevent users implementing the
// interface and so future additions to it will not violate Go 1 compatibility."
//
// This may cause problems across Go versions, but let's ignore them and
// work around that restriction by embedding a T so we satisfy the unexported methods of the interface.
*testing.T
}
// NewMockTester creates a new process tester.
func NewMockTester(name string) *MockTester {
return &MockTester{
name: name,
mu: sync.RWMutex{},
output: nil,
skipped: false,
printErrors: true,
printLogs: true,
outputHandler: log.New(os.Stderr, "", 0).Println,
failed: false,
T: nil,
}
}
// SetOutputHandler sets the output handler for the test output.
func (t *MockTester) SetOutputHandler(handler func(...interface{})) {
t.mu.Lock()
defer t.mu.Unlock()
t.outputHandler = handler
}
var _ testing.TB = (*MockTester)(nil)
// Error registers an error.
func (t *MockTester) Error(args ...interface{}) {
t.Log(args...)
t.Fail()
}
// Errorf formats the error.
func (t *MockTester) Errorf(format string, args ...interface{}) {
t.Logf(format, args...)
t.Fail()
}
// Fail fails the process.
func (t *MockTester) Fail() {
t.mu.Lock()
t.failed = true
t.mu.Unlock()
}
// FailNow immediately fails the process.
func (t *MockTester) FailNow() {
t.Fail()
}
// Failed indicates whether or not the process failed.
func (t *MockTester) Failed() bool {
t.mu.RLock()
failed := t.failed
t.mu.RUnlock()
return failed
}
// Fatal fails instantly.
func (t *MockTester) Fatal(args ...interface{}) {
t.Log(args...)
t.FailNow()
}
// Fatalf formats a fail.
func (t *MockTester) Fatalf(format string, args ...interface{}) {
t.Logf(format, args...)
t.FailNow()
}
// Log logs the process.
func (t *MockTester) Log(args ...interface{}) {
t.mu.Lock()
defer t.mu.Unlock()
formattedOut := fmt.Sprintln(args...)
t.output = append(t.output, formattedOut)
if t.printErrors {
t.outputHandler(fmt.Sprintln(formattedOut))
}
}
// Logf format logs.
func (t *MockTester) Logf(format string, args ...interface{}) {
t.mu.Lock()
defer t.mu.Unlock()
// Ensure message ends with newline.
if len(format) > 0 && format[len(format)-1] != '\n' {
format += "\n"
}
formattedOut := fmt.Sprintf(format, args...)
t.output = append(t.output, formattedOut)
if t.printLogs {
t.outputHandler(fmt.Sprintln(formattedOut))
}
}
// Name gets the test name.
func (t *MockTester) Name() string {
return t.name
}
// Skip skips the test.
func (t *MockTester) Skip(args ...interface{}) {
t.Log(args...)
t.SkipNow()
}
// SkipNow skips the test immediately.
func (t *MockTester) SkipNow() {
t.mu.Lock()
t.skipped = true
t.mu.Unlock()
}
// Skipf skips the test with formaatting.
func (t *MockTester) Skipf(format string, args ...interface{}) {
t.Logf(format, args...)
t.SkipNow()
}
// Skipped gets whether or not a process was skipped.
func (t *MockTester) Skipped() bool {
t.mu.Lock()
skipped := t.skipped
t.mu.Unlock()
return skipped
}
// Helper is used to implement the test helper.
func (t *MockTester) Helper() {}
// Output gets the output of the test.
func (t *MockTester) Output() []string {
t.mu.RLock()
defer t.mu.RUnlock()
return t.output
}