Skip to content

Commit ae5891d

Browse files
committed
Mark assertion functions as helpers
This way test traces will point to the testing code where the assertion failed. Previously it was always showing the line in the assertion helpers which was not very helpful.
1 parent 8c868b0 commit ae5891d

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/assert/assertfakes/fake_testing_errf.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/assert/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "fmt"
1010
// testing.T, testing.TB and similar.
1111
type TestingErrf interface {
1212
Errorf(format string, args ...any)
13+
Helper()
1314
}
1415

1516
func fromMsgAndArgs(msgAndArgs ...any) string {
@@ -22,5 +23,5 @@ func fromMsgAndArgs(msgAndArgs ...any) string {
2223
panic("The first argument in msgAndArgs must be a string format value.")
2324
}
2425

25-
return fmt.Sprintf("\n("+fmtStr+")", msgAndArgs[1:]...)
26+
return fmt.Sprintf(" ("+fmtStr+")", msgAndArgs[1:]...)
2627
}

src/assert/equal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package assert
33
// Equal checks whether expected and actual are actually equal and fails the test
44
// if they are not.
55
func Equal[V comparable](t TestingErrf, expected, actual V, msgAndArgs ...any) {
6+
t.Helper()
7+
68
if expected == actual {
79
return
810
}

src/assert/equal_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ func TestEqual(t *testing.T) {
1717
if fakeT.ErrorfCallCount() != 0 {
1818
t.Errorf("expected Errorf not to be called for int64 and const expression")
1919
}
20+
if fakeT.HelperCallCount() != 1 {
21+
t.Errorf("expected Helper() to be called on the testing type")
22+
}
2023

2124
assert.Equal(fakeT, 10, actual)
2225
if fakeT.ErrorfCallCount() != 1 {

src/library/library_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ func TestSearch(t *testing.T) {
357357
// assertTrack checks that expected is the same as actual but skips
358358
// checking for the actual track ID as it may not be known beforehand.
359359
func assertTrack(t *testing.T, expected, actual TrackInfo) {
360+
t.Helper()
361+
360362
if actual.Artist != expected.Artist {
361363
t.Errorf("Expected Artist `%s` but found `%s`",
362364
expected.Artist, actual.Artist)
@@ -394,6 +396,8 @@ func assertTrack(t *testing.T, expected, actual TrackInfo) {
394396

395397
// assertAlbum asserts that `actual` is the same as `expected`.
396398
func assertAlbum(t *testing.T, expected, actual Album) {
399+
t.Helper()
400+
397401
assert.Equal(t, expected.Name, actual.Name, "album name")
398402
assert.Equal(t, expected.ID, actual.ID, "album ID")
399403
assert.Equal(t, expected.Artist, actual.Artist, "album artist")
@@ -408,6 +412,8 @@ func assertAlbum(t *testing.T, expected, actual Album) {
408412

409413
// assertArtist asserts that `actual` is the same as `expected`.
410414
func assertArtist(t *testing.T, expected, actual Artist) {
415+
t.Helper()
416+
411417
assert.Equal(t, expected.Name, actual.Name, "artist name")
412418
assert.Equal(t, expected.ID, actual.ID, "artist ID")
413419
assert.Equal(t, expected.AlbumCount, actual.AlbumCount, "albums count")

0 commit comments

Comments
 (0)