-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpect.go
182 lines (148 loc) · 4.34 KB
/
expect.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Package expect provides functions and types to write fluent, readable expectations for use in unit tests.
// Package expect provides an easy to use and easy to extend framework to write expectations using common
// comparisons as well as providing custom expcters.
package expect
import (
"fmt"
)
// Expectation defines an interface for types that perform an expectation.
type Expectation interface {
Expect(t TB)
}
// ExpectFunc is a convenience type to satisfy Expection with a bare function.
type ExpectFunc func(TB)
func (f ExpectFunc) Expect(t TB) {
t.Helper()
f(t)
}
// TB is basically a copy from testing.TB. It is used here to allow other implementations (testing.TB
// contains an unexported private method) to be used (i.e. mocks while testing matchers). All methods of this
// interface work exactly the same as their counterparts from testing.TB type.
type TB interface {
Cleanup(func())
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
Failed() bool
Fatal(args ...any)
Fatalf(format string, args ...any)
Helper()
Log(args ...any)
Logf(format string, args ...any)
Name() string
Setenv(key, value string)
Skip(args ...any)
SkipNow()
Skipf(format string, args ...any)
Skipped() bool
TempDir() string
}
// Expectations implements a context for running expectations.
type Expectations struct {
t TB
}
// Using creates a new Expectations value using t to interact with the test runner.
func Using(t TB) *Expectations {
t.Helper()
return &Expectations{t: t}
}
// That runs all expecters using got as the actual value. You may run That multiple times and with the same
// or different value for got.
func (e *Expectations) That(expectations ...Expectation) *Expectations {
e.t.Helper()
for _, expecter := range expectations {
expecter.Expect(e.t)
}
return e
}
// WithMessage creates a new Expectations value that prefixes all messages with the message produced by
// applying args to format.
func (e *Expectations) WithMessage(format string, args ...any) *Expectations {
e.t.Helper()
if len(args) > 0 {
format = fmt.Sprintf(format, args...)
}
return Using(&prefixedTB{TB: e.t, prefix: format + ": "})
}
type prefixedTB struct {
TB
prefix string
}
func (p *prefixedTB) args(args []any) []any {
p.TB.Helper()
a := make([]any, len(args)+1)
copy(a[1:], args)
a[0] = p.prefix
return a
}
func (p *prefixedTB) format(format string, args []any) string {
p.TB.Helper()
return fmt.Sprint(p.prefix, fmt.Sprintf(format, args...))
}
func (p *prefixedTB) Error(args ...any) {
p.TB.Helper()
p.TB.Error(p.args(args)...)
}
func (p *prefixedTB) Errorf(format string, args ...any) {
p.TB.Helper()
p.TB.Errorf(p.format(format, args))
}
func (p *prefixedTB) Fatal(args ...any) {
p.TB.Helper()
p.TB.Fatal(p.args(args)...)
}
func (p *prefixedTB) Fatalf(format string, args ...any) {
p.TB.Helper()
p.TB.Fatalf(p.format(format, args))
}
func (p *prefixedTB) Log(args ...any) {
p.TB.Helper()
p.TB.Log(p.args(args)...)
}
func (p *prefixedTB) Logf(format string, args ...any) {
p.TB.Helper()
p.TB.Logf(p.format(format, args))
}
func (p *prefixedTB) Skip(args ...any) {
p.TB.Helper()
p.TB.Log(p.args(args)...)
}
func (p *prefixedTB) Skipf(format string, args ...any) {
p.TB.Helper()
p.TB.Skipf(p.format(format, args))
}
// That is a convenience function that runs all expecters on got reporting to t.
func That(t TB, expectations ...Expectation) {
t.Helper()
Using(t).That(expectations...)
}
// WithMessage is a convenience function to create an Expectations values with a pre-set prefix.
func WithMessage(t TB, format string, args ...any) *Expectations {
t.Helper()
return Using(t).WithMessage(format, args...)
}
// Fail is an Expectation that always fails.
var Fail Expectation = ExpectFunc(func(t TB) { t.Error("test failed") })
// FailNow is a decorator for an Expectation that converts calls to t.Error, t.Errorf and t.Fail to
// corresponding calls of t.Fatal, t.Fatalf, t.FailNow thus causing the test to fail immediately.
func FailNow(expectations ...Expectation) Expectation {
return ExpectFunc(func(t TB) {
wrapped := &failNowTB{TB: t}
for _, e := range expectations {
e.Expect(wrapped)
}
})
}
type failNowTB struct {
TB
}
func (f *failNowTB) Error(args ...any) {
f.TB.Fatal(args...)
}
func (f *failNowTB) Errorf(format string, args ...any) {
f.TB.Fatalf(format, args...)
}
func (f *failNowTB) Fail() {
f.TB.FailNow()
}