Skip to content

Commit 1852b39

Browse files
authored
Refactor fail method in matchers (#6517)
Accept a format and parameters just like `fmt.Sprintf` given almost all calls to `e.fail` follow this format.
1 parent 7376a33 commit 1852b39

File tree

4 files changed

+136
-132
lines changed

4 files changed

+136
-132
lines changed

exporters/zipkin/internal/matchers/expectation.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,49 @@ func (e *Expectation) ToEqual(expected interface{}) {
2828
e.verifyExpectedNotNil(expected)
2929

3030
if !reflect.DeepEqual(e.actual, expected) {
31-
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
31+
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
3232
}
3333
}
3434

3535
func (e *Expectation) NotToEqual(expected interface{}) {
3636
e.verifyExpectedNotNil(expected)
3737

3838
if reflect.DeepEqual(e.actual, expected) {
39-
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
39+
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
4040
}
4141
}
4242

4343
func (e *Expectation) ToBeNil() {
4444
if e.actual != nil {
45-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
45+
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
4646
}
4747
}
4848

4949
func (e *Expectation) NotToBeNil() {
5050
if e.actual == nil {
51-
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
51+
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
5252
}
5353
}
5454

5555
func (e *Expectation) ToBeTrue() {
5656
switch a := e.actual.(type) {
5757
case bool:
5858
if !a {
59-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
59+
e.fatalf("Expected\n\t%v\nto be true", e.actual)
6060
}
6161
default:
62-
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
62+
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
6363
}
6464
}
6565

6666
func (e *Expectation) ToBeFalse() {
6767
switch a := e.actual.(type) {
6868
case bool:
6969
if a {
70-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
70+
e.fatalf("Expected\n\t%v\nto be false", e.actual)
7171
}
7272
default:
73-
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
73+
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
7474
}
7575
}
7676

@@ -80,25 +80,25 @@ func (e *Expectation) NotToPanic() {
8080
func() {
8181
defer func() {
8282
if recovered := recover(); recovered != nil {
83-
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
83+
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
8484
}
8585
}()
8686

8787
a()
8888
}()
8989
default:
90-
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
90+
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
9191
}
9292
}
9393

9494
func (e *Expectation) ToSucceed() {
9595
switch actual := e.actual.(type) {
9696
case error:
9797
if actual != nil {
98-
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
98+
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
9999
}
100100
default:
101-
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
101+
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
102102
}
103103
}
104104

@@ -107,20 +107,20 @@ func (e *Expectation) ToMatchError(expected interface{}) {
107107

108108
actual, ok := e.actual.(error)
109109
if !ok {
110-
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
110+
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
111111
}
112112

113113
switch expected := expected.(type) {
114114
case error:
115115
if !reflect.DeepEqual(actual, expected) {
116-
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
116+
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
117117
}
118118
case string:
119119
if actual.Error() != expected {
120-
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
120+
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
121121
}
122122
default:
123-
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
123+
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
124124
}
125125
}
126126

@@ -131,7 +131,7 @@ func (e *Expectation) ToContain(expected interface{}) {
131131
switch actualKind {
132132
case reflect.Array, reflect.Slice:
133133
default:
134-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
134+
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
135135
return
136136
}
137137

@@ -156,7 +156,7 @@ func (e *Expectation) ToContain(expected interface{}) {
156156
}
157157

158158
if !contained {
159-
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
159+
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
160160
return
161161
}
162162
}
@@ -169,7 +169,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
169169
switch actualKind {
170170
case reflect.Array, reflect.Slice:
171171
default:
172-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
172+
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
173173
return
174174
}
175175

@@ -187,7 +187,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
187187

188188
for j := 0; j < actualValue.Len(); j++ {
189189
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
190-
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
190+
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
191191
return
192192
}
193193
}
@@ -201,20 +201,20 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
201201
switch expectedKind {
202202
case reflect.Array, reflect.Slice:
203203
default:
204-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
204+
e.fatalf("Expected\n\t%v\nto be an array", expected)
205205
return
206206
}
207207

208208
actualValue := reflect.ValueOf(e.actual)
209209
actualKind := actualValue.Kind()
210210

211211
if actualKind != expectedKind {
212-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
212+
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
213213
return
214214
}
215215

216216
if actualValue.Len() != expectedValue.Len() {
217-
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
217+
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
218218
return
219219
}
220220

@@ -237,7 +237,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
237237
}
238238

239239
if !found {
240-
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
240+
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
241241
}
242242
}
243243
}
@@ -246,44 +246,44 @@ func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interfac
246246
if actual, ok := e.actual.(time.Time); ok {
247247
ct, ok := compareTo.(time.Time)
248248
if !ok {
249-
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
249+
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
250250
return
251251
}
252252

253253
switch matcher {
254254
case Before:
255255
if !actual.Before(ct) {
256-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
256+
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
257257
}
258258
case BeforeOrSameTime:
259259
if actual.After(ct) {
260-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
260+
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
261261
}
262262
case After:
263263
if !actual.After(ct) {
264-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
264+
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
265265
}
266266
case AfterOrSameTime:
267267
if actual.Before(ct) {
268-
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
268+
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
269269
}
270270
default:
271-
e.fail("Cannot compare times with unexpected temporal matcher")
271+
e.fatalf("Cannot compare times with unexpected temporal matcher")
272272
}
273273

274274
return
275275
}
276276

277-
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
277+
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
278278
}
279279

280280
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
281281
if expected == nil {
282-
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
282+
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
283283
}
284284
}
285285

286-
func (e *Expectation) fail(msg string) {
286+
func (e *Expectation) fatalf(format string, a ...any) {
287287
// Prune the stack trace so that it's easier to see relevant lines
288288
stack := strings.Split(string(debug.Stack()), "\n")
289289
var prunedStack []string
@@ -294,5 +294,6 @@ func (e *Expectation) fail(msg string) {
294294
}
295295
}
296296

297+
msg := fmt.Sprintf(format, a...)
297298
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
298299
}

0 commit comments

Comments
 (0)