Skip to content

Commit

Permalink
switched to using TB for helpers (quii#383)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Stranathan <will@cltnc.us>
Co-authored-by: Chris James <qui666@gmail.com>
  • Loading branch information
3 people authored Oct 22, 2020
1 parent 5e8cd48 commit 4462875
Show file tree
Hide file tree
Showing 90 changed files with 271 additions and 262 deletions.
2 changes: 1 addition & 1 deletion arrays-and-slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ Our tests have some repeated code around assertion again, let's extract that int
```go
func TestSumAllTails(t *testing.T) {

checkSums := func(t *testing.T, got, want []int) {
checkSums := func(t testing.TB, got, want []int) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
Expand Down
4 changes: 2 additions & 2 deletions command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ In `server_test` we earlier did checks to see if wins are recorded as we have he

```go
//server_test.go
func assertPlayerWin(t *testing.T, store *StubPlayerStore, winner string) {
func assertPlayerWin(t testing.TB, store *StubPlayerStore, winner string) {
t.Helper()

if len(store.winCalls) != 1 {
Expand Down Expand Up @@ -490,7 +490,7 @@ func (s *StubPlayerStore) GetLeague() League {
return s.league
}

func AssertPlayerWin(t *testing.T, store *StubPlayerStore, winner string) {
func AssertPlayerWin(t testing.TB, store *StubPlayerStore, winner string) {
t.Helper()

if len(store.winCalls) != 1 {
Expand Down
6 changes: 3 additions & 3 deletions command-line/v1/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func createTempFile(t *testing.T, initialData string) (*os.File, func()) {
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
Expand Down Expand Up @@ -109,14 +109,14 @@ func TestFileSystemStore(t *testing.T) {
})
}

func assertScoreEquals(t *testing.T, got, want int) {
func assertScoreEquals(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("got %d want %d", got, want)
}
}

func assertNoError(t *testing.T, err error) {
func assertNoError(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("didn't expect an error but got one, %v", err)
Expand Down
10 changes: 5 additions & 5 deletions command-line/v1/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func TestLeague(t *testing.T) {
})
}

func assertContentType(t *testing.T, response *httptest.ResponseRecorder, want string) {
func assertContentType(t testing.TB, response *httptest.ResponseRecorder, want string) {
t.Helper()
if response.Header().Get("content-type") != want {
t.Errorf("response did not have content-type of %s, got %v", want, response.HeaderMap)
}
}

func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
func getLeagueFromResponse(t testing.TB, body io.Reader) []Player {
t.Helper()
league, err := NewLeague(body)

Expand All @@ -141,14 +141,14 @@ func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
return league
}

func assertLeague(t *testing.T, got, want []Player) {
func assertLeague(t testing.TB, got, want []Player) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -170,7 +170,7 @@ func newPostWinRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
6 changes: 3 additions & 3 deletions command-line/v2/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func createTempFile(t *testing.T, initialData string) (*os.File, func()) {
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
Expand Down Expand Up @@ -109,14 +109,14 @@ func TestFileSystemStore(t *testing.T) {
})
}

func assertScoreEquals(t *testing.T, got, want int) {
func assertScoreEquals(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("got %d want %d", got, want)
}
}

func assertNoError(t *testing.T, err error) {
func assertNoError(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("didn't expect an error but got one, %v", err)
Expand Down
10 changes: 5 additions & 5 deletions command-line/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func TestLeague(t *testing.T) {
})
}

func assertContentType(t *testing.T, response *httptest.ResponseRecorder, want string) {
func assertContentType(t testing.TB, response *httptest.ResponseRecorder, want string) {
t.Helper()
if response.Header().Get("content-type") != want {
t.Errorf("response did not have content-type of %s, got %v", want, response.HeaderMap)
}
}

func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
func getLeagueFromResponse(t testing.TB, body io.Reader) []Player {
t.Helper()
league, err := NewLeague(body)

Expand All @@ -141,14 +141,14 @@ func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
return league
}

func assertLeague(t *testing.T, got, want []Player) {
func assertLeague(t testing.TB, got, want []Player) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -170,7 +170,7 @@ func newPostWinRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
6 changes: 3 additions & 3 deletions command-line/v3/file_system_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func createTempFile(t *testing.T, initialData string) (*os.File, func()) {
func createTempFile(t testing.TB, initialData string) (*os.File, func()) {
t.Helper()

tmpfile, err := ioutil.TempFile("", "db")
Expand Down Expand Up @@ -109,14 +109,14 @@ func TestFileSystemStore(t *testing.T) {
})
}

func assertScoreEquals(t *testing.T, got, want int) {
func assertScoreEquals(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("got %d want %d", got, want)
}
}

func assertNoError(t *testing.T, err error) {
func assertNoError(t testing.TB, err error) {
t.Helper()
if err != nil {
t.Fatalf("didn't expect an error but got one, %v", err)
Expand Down
10 changes: 5 additions & 5 deletions command-line/v3/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func TestLeague(t *testing.T) {
})
}

func assertContentType(t *testing.T, response *httptest.ResponseRecorder, want string) {
func assertContentType(t testing.TB, response *httptest.ResponseRecorder, want string) {
t.Helper()
if response.Header().Get("content-type") != want {
t.Errorf("response did not have content-type of %s, got %v", want, response.HeaderMap)
}
}

func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
func getLeagueFromResponse(t testing.TB, body io.Reader) []Player {
t.Helper()
league, err := NewLeague(body)

Expand All @@ -115,14 +115,14 @@ func getLeagueFromResponse(t *testing.T, body io.Reader) []Player {
return league
}

func assertLeague(t *testing.T, got, want []Player) {
func assertLeague(t testing.TB, got, want []Player) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -144,7 +144,7 @@ func newPostWinRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
2 changes: 1 addition & 1 deletion command-line/v3/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s *StubPlayerStore) GetLeague() League {
}

// AssertPlayerWin allows you to spy on the store's calls to RecordWin.
func AssertPlayerWin(t *testing.T, store *StubPlayerStore, winner string) {
func AssertPlayerWin(t testing.TB, store *StubPlayerStore, winner string) {
t.Helper()

if len(store.WinCalls) != 1 {
Expand Down
2 changes: 1 addition & 1 deletion context-aware-reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestContextAwareReader(t *testing.T) {
})
}

func assertBufferHas(t *testing.T, buf []byte, want string) {
func assertBufferHas(t testing.TB, buf []byte, want string) {
t.Helper()
got := string(buf)
if got != want {
Expand Down
4 changes: 3 additions & 1 deletion hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ We can and should refactor our tests.
```go
func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
Expand All @@ -286,6 +286,8 @@ What have we done here?

We've refactored our assertion into a function. This reduces duplication and improves readability of our tests. In Go you can declare functions inside other functions and assign them to variables. You can then call them, just like normal functions. We need to pass in `t *testing.T` so that we can tell the test code to fail when we need to.

For helper functions, it's a good idea to accept a `testing.TB` which is an interface that `*testing.T` and `*testing.B` both satisfy, so you can call helper functions from a test, or a benchmark.

`t.Helper()` is needed to tell the test suite that this method is a helper. By doing this when it fails the line number reported will be in our _function call_ rather than inside our test helper. This will help other developers track down problems easier. If you still don't understand, comment it out, make a test fail and observe the test output.

Now that we have a well-written failing test, let's fix the code, using an `if`.
Expand Down
2 changes: 1 addition & 1 deletion hello-world/v5/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
Expand Down
2 changes: 1 addition & 1 deletion hello-world/v6/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
Expand Down
2 changes: 1 addition & 1 deletion hello-world/v8/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
Expand Down
6 changes: 3 additions & 3 deletions http-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func newGetScoreRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down Expand Up @@ -601,7 +601,7 @@ func TestGETPlayers(t *testing.T) {
})
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -613,7 +613,7 @@ func newGetScoreRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
4 changes: 2 additions & 2 deletions http-server/v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestGETPlayers(t *testing.T) {
}
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -75,7 +75,7 @@ func newGetScoreRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
4 changes: 2 additions & 2 deletions http-server/v3/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestStoreWins(t *testing.T) {
})
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -91,7 +91,7 @@ func newGetScoreRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
4 changes: 2 additions & 2 deletions http-server/v4/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestStoreWins(t *testing.T) {
})
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -108,7 +108,7 @@ func newGetScoreRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
4 changes: 2 additions & 2 deletions http-server/v5/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestStoreWins(t *testing.T) {
})
}

func assertStatus(t *testing.T, got, want int) {
func assertStatus(t testing.TB, got, want int) {
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
Expand All @@ -113,7 +113,7 @@ func newPostWinRequest(name string) *http.Request {
return req
}

func assertResponseBody(t *testing.T, got, want string) {
func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
Expand Down
Loading

0 comments on commit 4462875

Please sign in to comment.