diff options
| author | mo khan <mo.khan@gmail.com> | 2019-10-19 16:09:03 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2019-10-19 16:09:03 -0600 |
| commit | fc4bba16caff7c472e1b33ddb71c583c59b96605 (patch) | |
| tree | 7c86111e3b6a8000b973385f379f6cc64659fa36 | |
| parent | 5cd525aca63a7b1bf09e0e679174a10f176ccc98 (diff) | |
extract assertion function
| -rw-r--r-- | hello.go | 7 | ||||
| -rw-r--r-- | hello_test.go | 24 |
2 files changed, 25 insertions, 6 deletions
@@ -2,8 +2,13 @@ package main import "fmt" +const englishHelloPrefix = "Hello, " + func Hello(name string) string { - return "Hello, " + name + if name == "" { + name = "World" + } + return englishHelloPrefix + name } func main() { diff --git a/hello_test.go b/hello_test.go index 544ad26..41bcfb1 100644 --- a/hello_test.go +++ b/hello_test.go @@ -3,10 +3,24 @@ package main import "testing" func TestHello(t *testing.T) { - got := Hello("mo") - want := "Hello, mo" - - if got != want { - t.Errorf("got %q want %q", got, want) + assertCorrectMessage := func(t *testing.T, got, want string) { + t.Helper() + if got != want { + t.Errorf("got %q want %q", got, want) + } } + + t.Run("saying hello to people", func(t *testing.T) { + got := Hello("mo") + want := "Hello, mo" + + assertCorrectMessage(t, got, want) + }) + + t.Run("say 'Hello, World' when an empty string is supplied", func(t *testing.T) { + got := Hello("") + want := "Hello, World" + + assertCorrectMessage(t, got, want) + }) } |
