-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.go
203 lines (170 loc) · 4.36 KB
/
assert.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
package flowtest
import (
"fmt"
"reflect"
"github.com/pentops/flowtest/be"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)
type Assertion interface {
// Sub creates a named sub-assertion
Sub(name string, args ...interface{}) Assertion
// T accepts the assertion types like Equal which use generics and therefore
// can't be a method of Assertion directly.
T(outcome *be.Outcome)
// NoError asserts be the error is nil, and fails the test if not
NoError(err error)
// MustMessage is used to assert topic requests work
MustMessage(*emptypb.Empty, error)
// Equal asserts be want == got. If extraLog is set, and the first
// argument is a string it is used as a format string for the rest of the
// arguments. If the first argument is not a string, everything is just
// logged
Equal(want, got interface{})
// CodeError asserts be the error returned was non-nil and a Status error
// with the given code
CodeError(err error, code codes.Code)
// NotEmpty asserts be the given values are not nil or zero values (zero
// as in reflect.Value.IsZero)
NotEmpty(got ...interface{})
// NotNil asserts be the given values are not nil, assessing in order and
// stopping at the first nil value (i.e. you can pass thing, thing.field,
// thing.field.subfield)
NotNil(gots ...interface{})
// Nil asserts be the given values are nil
Nil(gots ...interface{})
// Fatal fails the test with the given message
Fatal(args ...interface{})
// Fatalf fails the test with the given format string
Fatalf(format string, args ...interface{})
}
type assertion struct {
name string
fatal func(args ...interface{})
helper func()
}
func (a *assertion) T(outcome *be.Outcome) {
a.helper()
if outcome != nil {
a.fail(string(*outcome))
}
}
func (a *assertion) Sub(name string, args ...interface{}) Assertion {
return &assertion{
name: fmt.Sprintf(name, args...),
helper: a.helper,
fatal: a.fatal,
}
}
func (a *assertion) fail(format string, args ...interface{}) {
a.helper()
if a.name != "" {
format = fmt.Sprintf("%s: %s", a.name, format)
}
a.fatal(fmt.Sprintf(format, args...))
}
func (a *assertion) Fatal(args ...interface{}) {
a.helper()
a.fail(fmt.Sprint(args...))
}
func (a *assertion) Fatalf(format string, args ...interface{}) {
a.helper()
a.fail(format, args...)
}
func (a *assertion) NoError(err error) {
a.helper()
if err != nil {
statErr, ok := status.FromError(err)
if ok {
a.fail("unexpected error %s\n %s\n", err.Error(), prototext.Format(statErr.Proto()))
} else {
a.fail("got error %s (%T), want no error", err, err)
}
}
}
func (a *assertion) MustMessage(m *emptypb.Empty, err error) {
a.helper()
a.NoError(err)
a.NotNil(m)
}
func (a *assertion) Equal(want, got interface{}) {
a.helper()
if got == nil || want == nil {
if got != want {
a.fail("got %v, want %v", got, want)
}
return
}
if wantProto, ok := want.(proto.Message); ok {
gotProto, ok := got.(proto.Message)
if !ok {
a.fail("want was a proto, got was (%T)", got)
return
}
if !proto.Equal(wantProto, gotProto) {
a.fail("got %v, want %v", got, want)
}
return
}
if !reflect.DeepEqual(got, want) {
a.fail("got %v, want %v", got, want)
}
}
func (a *assertion) NotEmpty(gots ...interface{}) {
a.helper()
for _, got := range gots {
if got == nil {
a.fail("got nil, want non-nil")
return
}
rv := reflect.ValueOf(got)
if rv.IsZero() {
a.fail("got zero value, want non-zero")
}
}
}
func isNil(got interface{}) bool {
if got == nil {
return true
}
rv := reflect.ValueOf(got)
if rv.Kind() != reflect.Ptr {
return false
}
return rv.IsNil()
}
func (a *assertion) NotNil(gots ...interface{}) {
a.helper()
for _, got := range gots {
if isNil(got) {
a.fail("value was nil")
return
}
}
}
func (a *assertion) Nil(gots ...interface{}) {
a.helper()
for _, got := range gots {
if !isNil(got) {
a.fail("value was not nil")
return
}
}
}
func (a *assertion) CodeError(err error, code codes.Code) {
if err == nil {
a.fail("got no error, want code %s", code)
return
}
if s, ok := status.FromError(err); !ok {
a.fail("got error %s (%T), want code %s", err, err, code)
} else {
if s.Code() != code {
a.fail("got code %s, want %s", s.Code(), code)
}
return
}
}