diff --git a/hello/hello.go b/hello/hello.go index b454290..64287a8 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -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/hello_test.go b/hello/hello_test.go index 326bb1c..0e56664 100644 --- a/hello/hello_test.go +++ b/hello/hello_test.go @@ -3,9 +3,20 @@ package main import "testing" func TestHello(t *testing.T) { - got := Hello("Chris") - want := "Hello, Chris" + t.Run("saying hello to people", func(t *testing.T) { + got := Hello("Chris") + want := "Hello, Chris" + 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) + }) +} +func assertCorrectMessage(t testing.TB, got, want string) { + t.Helper() if got != want { t.Errorf("got %q want %q", got, want) }