Skip to content

Commit 2f88956

Browse files
committed
lets not do closures on chapter 1
1 parent 1e68be4 commit 2f88956

File tree

6 files changed

+49
-47
lines changed

6 files changed

+49
-47
lines changed

arrays-and-slices.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ func SumAllTails(numbersToSum ...[]int) []int {
504504

505505
## Refactor
506506

507-
Our tests have some repeated code around the assertions again, so let's extract those into a function
507+
Our tests have some repeated code around the assertions again, so let's extract those into a function.
508508

509509
```go
510510
func TestSumAllTails(t *testing.T) {
@@ -531,6 +531,12 @@ func TestSumAllTails(t *testing.T) {
531531
}
532532
```
533533

534+
We could've created a new function `checkSums` like we normally do, but in this case, we're showing a new technique, assigning a function to a variable. It might look strange but, it's no different to assigning a variable to a `string`, or an `int`, functions in effect are values too.
535+
536+
It's not shown here, but this technique can be useful when you want to bind a function to other local variables in "scope" (e.g between some `{}`). It also allows you to reduce the surface area of your API.
537+
538+
By defining this function inside the test, it cannot be used by other functions in this package. Hiding variables and functions that don't need to be exported is an important design consideration.
539+
534540
A handy side-effect of this is this adds a little type-safety to our code. If
535541
a developer mistakenly adds a new test with `checkSums(t, got, "dave")` the compiler
536542
will stop them in their tracks.

hello-world.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,31 +275,33 @@ We can and should refactor our tests.
275275

276276
```go
277277
func TestHello(t *testing.T) {
278-
assertCorrectMessage := func(t testing.TB, got, want string) {
279-
t.Helper()
280-
if got != want {
281-
t.Errorf("got %q want %q", got, want)
282-
}
283-
}
284-
285278
t.Run("saying hello to people", func(t *testing.T) {
286279
got := Hello("Chris")
287280
want := "Hello, Chris"
288281
assertCorrectMessage(t, got, want)
289282
})
290-
t.Run("empty string defaults to 'World'", func(t *testing.T) {
283+
284+
t.Run("empty string defaults to 'world'", func(t *testing.T) {
291285
got := Hello("")
292286
want := "Hello, World"
293287
assertCorrectMessage(t, got, want)
294288
})
289+
290+
}
291+
292+
func assertCorrectMessage(t testing.TB, got, want string) {
293+
t.Helper()
294+
if got != want {
295+
t.Errorf("got %q want %q", got, want)
296+
}
295297
}
296298
```
297299

298300
What have we done here?
299301

300-
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.
302+
We've refactored our assertion into a new function. This reduces duplication and improves readability of our tests. We need to pass in `t *testing.T` so that we can tell the test code to fail when we need to.
301303

302-
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.
304+
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 (don't worry if words like "interface" mean nothing to you right now, it will be covered later).
303305

304306
`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. Comments in Go are a great way to add additional information to your code, or in this case, a quick way to tell the compiler to ignore a line. You can comment out the `t.Helper()` code by adding two forward slashes `//` at the beginning of the line. You should see that line turn grey or change to another color than the rest of your code to indicate it's now commented out.
305307

hello-world/v5/hello_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ package main
33
import "testing"
44

55
func TestHello(t *testing.T) {
6-
7-
assertCorrectMessage := func(t testing.TB, got, want string) {
8-
t.Helper()
9-
if got != want {
10-
t.Errorf("got %q want %q", got, want)
11-
}
12-
}
13-
146
t.Run("saying hello to people", func(t *testing.T) {
157
got := Hello("Chris")
168
want := "Hello, Chris"
@@ -24,3 +16,10 @@ func TestHello(t *testing.T) {
2416
})
2517

2618
}
19+
20+
func assertCorrectMessage(t testing.TB, got, want string) {
21+
t.Helper()
22+
if got != want {
23+
t.Errorf("got %q want %q", got, want)
24+
}
25+
}

hello-world/v6/hello_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ package main
33
import "testing"
44

55
func TestHello(t *testing.T) {
6-
7-
assertCorrectMessage := func(t testing.TB, got, want string) {
8-
t.Helper()
9-
if got != want {
10-
t.Errorf("got %q want %q", got, want)
11-
}
12-
}
13-
146
t.Run("to a person", func(t *testing.T) {
157
got := Hello("Chris", "")
168
want := "Hello, Chris"
@@ -36,3 +28,10 @@ func TestHello(t *testing.T) {
3628
})
3729

3830
}
31+
32+
func assertCorrectMessage(t testing.TB, got, want string) {
33+
t.Helper()
34+
if got != want {
35+
t.Errorf("got %q want %q", got, want)
36+
}
37+
}

hello-world/v7/hello_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,34 @@ package main
33
import "testing"
44

55
func TestHello(t *testing.T) {
6-
7-
assertCorrectMessage := func(got, want string) {
8-
t.Helper()
9-
if got != want {
10-
t.Errorf("got %q want %q", got, want)
11-
}
12-
}
13-
146
t.Run("saying hello to people", func(t *testing.T) {
157
got := Hello("Chris", "")
168
want := "Hello, Chris"
17-
assertCorrectMessage(got, want)
9+
assertCorrectMessage(t, got, want)
1810
})
1911

2012
t.Run("say hello world when an empty string is supplied", func(t *testing.T) {
2113
got := Hello("", "")
2214
want := "Hello, World"
23-
assertCorrectMessage(got, want)
15+
assertCorrectMessage(t, got, want)
2416
})
2517

2618
t.Run("say hello in Spanish", func(t *testing.T) {
2719
got := Hello("Elodie", spanish)
2820
want := "Hola, Elodie"
29-
assertCorrectMessage(got, want)
21+
assertCorrectMessage(t, got, want)
3022
})
3123

3224
t.Run("say hello in French", func(t *testing.T) {
3325
got := Hello("Lauren", french)
3426
want := "Bonjour, Lauren"
35-
assertCorrectMessage(got, want)
27+
assertCorrectMessage(t, got, want)
3628
})
29+
}
3730

31+
func assertCorrectMessage(t testing.TB, got, want string) {
32+
t.Helper()
33+
if got != want {
34+
t.Errorf("got %q want %q", got, want)
35+
}
3836
}

hello-world/v8/hello_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@ package main
33
import "testing"
44

55
func TestHello(t *testing.T) {
6-
7-
assertCorrectMessage := func(t testing.TB, got, want string) {
8-
t.Helper()
9-
if got != want {
10-
t.Errorf("got %q want %q", got, want)
11-
}
12-
}
13-
146
t.Run("to a person", func(t *testing.T) {
157
got := Hello("Chris", "")
168
want := "Hello, Chris"
@@ -34,5 +26,11 @@ func TestHello(t *testing.T) {
3426
want := "Bonjour, Lauren"
3527
assertCorrectMessage(t, got, want)
3628
})
29+
}
3730

31+
func assertCorrectMessage(t testing.TB, got, want string) {
32+
t.Helper()
33+
if got != want {
34+
t.Errorf("got %q want %q", got, want)
35+
}
3836
}

0 commit comments

Comments
 (0)