-
Notifications
You must be signed in to change notification settings - Fork 3
/
quicktest.go
209 lines (180 loc) · 5.3 KB
/
quicktest.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
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
/// Package quicktest implements assertion and other helpers wrapped
// around the standard library's testing types.
//package quicktest
package quicktest
import (
"errors"
"fmt"
"reflect"
"testing"
)
// Checker is implemented by types used as part of Check/Assert invocations.
// The type parameter will be the type of the first argument passed
// to Check or Assert.
type Checker[T any] interface {
// Check checks that the provided argument passes the check.
// On failure, the returned error is printed along with
// the checker arguments (obtained by calling ArgNames and Args)
// and key-value pairs added by calling the note function.
//
// If Check returns ErrSilent, neither the checker arguments nor
// the error are printed; values with note are still printed.
Check(got T, note func(key string, value interface{})) error
// ArgNames returns the arguments passed to the checker.
ArgNames() []string
// Args returns the arguments corresponding to ArgNames.
Args() []interface{}
}
// Assert checks that the provided argument passes the given check
// and calls tb.Error otherwise, including any Comment arguments
// in the failure.
func Assert[T any](tb testing.TB, got T, op Checker[T], comment ...Comment) {
if !Check(tb, got, op, comment...) {
tb.FailNow()
}
}
type Comment struct {
format string
args []interface{}
}
// String outputs a string formatted according to the stored format specifier
// and args.
func (c Comment) String() string {
return fmt.Sprintf(c.format, c.args...)
}
// Check checks that the provided argument passes the given check
// and calls tb.Fatal otherwise, including any Comment arguments
// in the failure.
func Check[T any](tb testing.TB, got T, op Checker[T], comment ...Comment) bool {
var notes []string
note := func(key string, value interface{}) {
notes = append(notes, fmt.Sprintf("%s: %v", key, value))
}
err := op.Check(got, note)
if err == nil {
return true
}
tb.Errorf("assertion failed: %v; notes %v", err, notes)
return false
}
// Patch sets *dest to value, then adds a Cleanup
// to tb that will set it back to its original value at the
// end of the test.
func Patch[T any](tb testing.TB, dest *T, value T) {
old := *dest
*dest = value
tb.Cleanup(func() {
*dest = old
})
}
// IsZero checks that a value is the zero value for its type.
// Maybe this might be better with a shorter name like Z.
func IsZero[T any]() Checker[T] {
var z T
return DeepEquals[T](z)
}
// Equals checks that the argument is equal to want.
func Equals[T comparable](want T) Checker[T] {
return equalsChecker[T]{
argNames: []string{"got", "want"},
want: want,
}
}
type equalsChecker[T comparable] struct {
argNames
want T
}
func (c equalsChecker[T]) Args() []interface{} {
return []interface{}{c.want}
}
func (c equalsChecker[T]) Check(got T, note func(key string, value interface{})) error {
if got != c.want {
return errors.New("values are not equal")
}
return nil
}
//// Contains returns a checker that checks whether
//// a slice contains the given element.
//func Contains[T comparable](want T) Checker[[]T] {
// return Any(Equals(want))
//}
//// StrContains returns a checker that checks whether
//// a string contains the given sub-string.
//func StrContains(substr string) Checker[string] {
//}
//
//// Any returns a checker that uses c to check elements
//// in a slice. It succeeds if any element passes the check.
//func Any[T any](c Checker[T]) Checker[[]T]
//
//// AnyMapValue returns a checker that uses c to check the
//// value elements in a map. It succeeds if any value
//// passes the check.
//func AnyMapValue[Key comparable, Value any](c Checker[Value]) Checker[map[Key]Value]
//
//
//func CmpEquals[T any](opts ...cmpOption) func(want T) Checker[T]
func DeepEquals[T any](want T) Checker[T] {
return deepEqualsChecker[T]{
argNames: []string{"got", "want"},
want: want,
}
}
type deepEqualsChecker[T any] struct {
argNames
want T
}
func (c deepEqualsChecker[T]) Args() []interface{} {
return []interface{}{c.want}
}
func (c deepEqualsChecker[T]) Check(got T, note func(key string, value interface{})) error {
// TODO use go-cmp
if !reflect.DeepEqual(got, c.want) {
return errors.New("values are not equal")
}
return nil
}
// cmpOption represents the cmp.Option type from the github.com/google/go-cmp/cmp
// package.
type cmpOption struct {
}
//type Data interface {
// type []byte, string
//}
//
//func JSONEquals(want interface{}) Checker[[]byte]
//
//func ErrorMatches(pattern string) Checker[error]
//
//func Matches(pattern string) Checker[string]
//
//func StringerMatches(pattern string) Checker[interface{ String() string }]
//
//
//func Satisfies[T any](f func(T) bool) Checker[T]
// argNames helps implementing Checker.ArgNames.
type argNames []string
// ArgNames implements Checker.ArgNames by returning the argument names.
func (a argNames) ArgNames() []string {
return a
}
func Not[T any](c Checker[T]) Checker[T] {
return notChecker[T]{
checker: c,
}
}
type notChecker[T any] struct {
checker Checker[T]
}
func (c notChecker[T]) ArgNames() []string {
return c.checker.ArgNames()
}
func (c notChecker[T]) Args() []interface{} {
return c.checker.Args()
}
func (c notChecker[T]) Check(got T, note func(key string, value interface{})) error {
if err := c.checker.Check(got, note); err == nil {
return fmt.Errorf("unexpected success")
}
return nil
}