Skip to content

Commit 3868418

Browse files
committed
impr: refine error messages
1 parent 41a5c9b commit 3868418

File tree

3 files changed

+46
-45
lines changed

3 files changed

+46
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Test(t *testing.T) {
3232
t.Run("fail", func(t *testing.T) {
3333
got, want := "olleh", "hello"
3434
be.Equal(t, got, want)
35-
// want "hello", got "olleh"
35+
// got: "olleh"; want: "hello"
3636
})
3737
}
3838
```

be.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ func Equal[T any](tb testing.TB, got T, wants ...T) {
4747
// There are no matches, report the failure.
4848
if len(wants) == 1 {
4949
// There is only one want, report it directly.
50-
tb.Errorf("want %#v, got %#v", wants[0], got)
50+
tb.Errorf("got: %#v; want: %#v", got, wants[0])
5151
return
5252
}
5353
// There are multiple wants, report a summary.
54-
tb.Errorf("want any of the %v, got %#v", wants, got)
54+
tb.Errorf("got: %#v; want any of: %v", got, wants)
5555
}
5656

5757
// Err asserts that the got error matches any of the wanted values.
@@ -71,7 +71,7 @@ func Err(tb testing.TB, got error, wants ...any) {
7171
// If no wants are given, we expect got to be a non-nil error.
7272
if len(wants) == 0 {
7373
if got == nil {
74-
tb.Error("want error, got <nil>")
74+
tb.Error("got: <nil>; want: error")
7575
}
7676
return
7777
}
@@ -104,14 +104,14 @@ func Err(tb testing.TB, got error, wants ...any) {
104104
return
105105
}
106106
// There are multiple wants, report a summary.
107-
tb.Errorf("want any of the %v, got %T(%v)", wants, got, got)
107+
tb.Errorf("got: %T(%v); want any of: %v", got, got, wants)
108108
}
109109

110110
// True asserts that got is true.
111111
func True(tb testing.TB, got bool) {
112112
tb.Helper()
113113
if !got {
114-
tb.Error("not true")
114+
tb.Error("got: false; want: true")
115115
}
116116
}
117117

@@ -159,7 +159,7 @@ func isNil(v any) bool {
159159
// Otherwise, returns an error message.
160160
func checkErr(got error, want any) string {
161161
if want != nil && got == nil {
162-
return "want error, got <nil>"
162+
return "got: <nil>; want: error"
163163
}
164164

165165
switch w := want.(type) {
@@ -169,16 +169,16 @@ func checkErr(got error, want any) string {
169169
}
170170
case string:
171171
if !strings.Contains(got.Error(), w) {
172-
return fmt.Sprintf("want %q, got %q", w, got.Error())
172+
return fmt.Sprintf("got: %q; want: %q", got.Error(), w)
173173
}
174174
case error:
175175
if !errors.Is(got, w) {
176-
return fmt.Sprintf("want %T(%v), got %T(%v)", w, w, got, got)
176+
return fmt.Sprintf("got: %T(%v); want: %T(%v)", got, got, w, w)
177177
}
178178
case reflect.Type:
179179
target := reflect.New(w).Interface()
180180
if !errors.As(got, target) {
181-
return fmt.Sprintf("want %s, got %T", w, got)
181+
return fmt.Sprintf("got: %T; want: %s", got, w)
182182
}
183183
default:
184184
return fmt.Sprintf("unsupported want type: %T", want)

be_test.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -116,61 +116,61 @@ func TestEqual(t *testing.T) {
116116
}{
117117
"integer": {
118118
got: 42, want: 84,
119-
msg: "want 84, got 42",
119+
msg: "got: 42; want: 84",
120120
},
121121
"int32 vs int64": {
122122
got: int32(42), want: int64(42),
123-
msg: "want 42, got 42",
123+
msg: "got: 42; want: 42",
124124
},
125125
"int vs string": {
126126
got: 42, want: "42",
127-
msg: `want "42", got 42`,
127+
msg: `got: 42; want: "42"`,
128128
},
129129
"string": {
130130
got: "hello", want: "world",
131-
msg: `want "world", got "hello"`,
131+
msg: `got: "hello"; want: "world"`,
132132
},
133133
"bool": {
134134
got: true, want: false,
135-
msg: "want false, got true",
135+
msg: "got: true; want: false",
136136
},
137137
"struct": {
138138
got: intType{42}, want: intType{84},
139-
msg: "want be_test.intType{val:84}, got be_test.intType{val:42}",
139+
msg: "got: be_test.intType{val:42}; want: be_test.intType{val:84}",
140140
},
141141
"pointer": {
142142
got: &val1, want: &val2,
143143
},
144144
"byte slice": {
145145
got: []byte("abc"), want: []byte("abd"),
146-
msg: `want []byte{0x61, 0x62, 0x64}, got []byte{0x61, 0x62, 0x63}`,
146+
msg: `got: []byte{0x61, 0x62, 0x63}; want: []byte{0x61, 0x62, 0x64}`,
147147
},
148148
"int slice": {
149149
got: []int{42, 84}, want: []int{84, 42},
150-
msg: `want []int{84, 42}, got []int{42, 84}`,
150+
msg: `got: []int{42, 84}; want: []int{84, 42}`,
151151
},
152152
"int slice vs any slice": {
153153
got: []int{42, 84}, want: []any{42, 84},
154-
msg: `want []interface {}{42, 84}, got []int{42, 84}`,
154+
msg: `got: []int{42, 84}; want: []interface {}{42, 84}`,
155155
},
156156
"time.Time": {
157157
got: now, want: now.Add(time.Second),
158158
},
159159
"nil vs non-nil": {
160160
got: nil, want: 42,
161-
msg: "want 42, got <nil>",
161+
msg: "got: <nil>; want: 42",
162162
},
163163
"non-nil vs nil": {
164164
got: 42, want: nil,
165-
msg: "want <nil>, got 42",
165+
msg: "got: 42; want: <nil>",
166166
},
167167
"nil vs empty": {
168168
got: []int(nil), want: []int{},
169-
msg: "want []int{}, got []int(nil)",
169+
msg: "got: []int(nil); want: []int{}",
170170
},
171171
"map": {
172172
got: map[string]int{"a": 42}, want: map[string]int{"a": 84},
173-
msg: `want map[string]int{"a":84}, got map[string]int{"a":42}`,
173+
msg: `got: map[string]int{"a":42}; want: map[string]int{"a":84}`,
174174
},
175175
"chan": {
176176
got: make(chan int), want: make(chan int),
@@ -188,7 +188,7 @@ func TestEqual(t *testing.T) {
188188
t.Error("should not be fatal")
189189
}
190190
if tc.msg != "" && tb.msg != tc.msg {
191-
t.Errorf("expected '%s', got '%s'", tc.msg, tb.msg)
191+
t.Errorf("got: %q; want: %q", tb.msg, tc.msg)
192192
}
193193
})
194194
}
@@ -235,7 +235,7 @@ func TestEqual(t *testing.T) {
235235
}
236236
wantMsg := "no wants given"
237237
if tb.msg != wantMsg {
238-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
238+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
239239
}
240240
})
241241
t.Run("multiple wants", func(t *testing.T) {
@@ -265,9 +265,9 @@ func TestEqual(t *testing.T) {
265265
if tb.fatal {
266266
t.Error("should not be fatal")
267267
}
268-
wantMsg := "want any of the [11 12 13], got 42"
268+
wantMsg := "got: 42; want any of: [11 12 13]"
269269
if tb.msg != wantMsg {
270-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
270+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
271271
}
272272
})
273273
})
@@ -294,7 +294,7 @@ func TestErr(t *testing.T) {
294294
}
295295
wantMsg := "unexpected error: oops"
296296
if tb.msg != wantMsg {
297-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
297+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
298298
}
299299
})
300300
})
@@ -309,9 +309,9 @@ func TestErr(t *testing.T) {
309309
if tb.fatal {
310310
t.Error("should not be fatal")
311311
}
312-
wantMsg := `want error, got <nil>`
312+
wantMsg := `got: <nil>; want: error`
313313
if tb.msg != wantMsg {
314-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
314+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
315315
}
316316
})
317317
t.Run("same error", func(t *testing.T) {
@@ -342,9 +342,9 @@ func TestErr(t *testing.T) {
342342
if tb.fatal {
343343
t.Error("should not be fatal")
344344
}
345-
wantMsg := "want *errors.errorString(error 2), got *errors.errorString(error 1)"
345+
wantMsg := "got: *errors.errorString(error 1); want: *errors.errorString(error 2)"
346346
if tb.msg != wantMsg {
347-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
347+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
348348
}
349349
})
350350
t.Run("different type", func(t *testing.T) {
@@ -358,9 +358,9 @@ func TestErr(t *testing.T) {
358358
if tb.fatal {
359359
t.Error("should not be fatal")
360360
}
361-
wantMsg := "want be_test.errType(oops), got *errors.errorString(oops)"
361+
wantMsg := "got: *errors.errorString(oops); want: be_test.errType(oops)"
362362
if tb.msg != wantMsg {
363-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
363+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
364364
}
365365
})
366366
})
@@ -383,9 +383,9 @@ func TestErr(t *testing.T) {
383383
if tb.fatal {
384384
t.Error("should not be fatal")
385385
}
386-
wantMsg := `want "day", got "the night is dark"`
386+
wantMsg := `got: "the night is dark"; want: "day"`
387387
if tb.msg != wantMsg {
388-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
388+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
389389
}
390390
})
391391
})
@@ -408,9 +408,9 @@ func TestErr(t *testing.T) {
408408
if tb.fatal {
409409
t.Error("should not be fatal")
410410
}
411-
wantMsg := "want *fs.PathError, got be_test.errType"
411+
wantMsg := "got: be_test.errType; want: *fs.PathError"
412412
if tb.msg != wantMsg {
413-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
413+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
414414
}
415415
})
416416
})
@@ -426,7 +426,7 @@ func TestErr(t *testing.T) {
426426
}
427427
wantMsg := "unsupported want type: int"
428428
if tb.msg != wantMsg {
429-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
429+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
430430
}
431431
})
432432
t.Run("no wants", func(t *testing.T) {
@@ -449,9 +449,9 @@ func TestErr(t *testing.T) {
449449
if tb.fatal {
450450
t.Error("should not be fatal")
451451
}
452-
wantMsg := "want error, got <nil>"
452+
wantMsg := "got: <nil>; want: error"
453453
if tb.msg != wantMsg {
454-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
454+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
455455
}
456456
})
457457
})
@@ -482,9 +482,9 @@ func TestErr(t *testing.T) {
482482
if tb.fatal {
483483
t.Error("should not be fatal")
484484
}
485-
wantMsg := "want any of the [failed 42 *fs.PathError], got be_test.errType(oops)"
485+
wantMsg := "got: be_test.errType(oops); want any of: [failed 42 *fs.PathError]"
486486
if tb.msg != wantMsg {
487-
t.Errorf("expected '%s', got '%s'", wantMsg, tb.msg)
487+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
488488
}
489489
})
490490
})
@@ -507,8 +507,9 @@ func TestTrue(t *testing.T) {
507507
if tb.fatal {
508508
t.Error("should not be fatal")
509509
}
510-
if tb.msg != "not true" {
511-
t.Errorf("expected 'not true', got '%s'", tb.msg)
510+
wantMsg := "got: false; want: true"
511+
if tb.msg != wantMsg {
512+
t.Errorf("got: %q; want: %q", tb.msg, wantMsg)
512513
}
513514
})
514515
t.Run("expression", func(t *testing.T) {

0 commit comments

Comments
 (0)