summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2019-10-19 16:09:03 -0600
committermo khan <mo.khan@gmail.com>2019-10-19 16:09:03 -0600
commitfc4bba16caff7c472e1b33ddb71c583c59b96605 (patch)
tree7c86111e3b6a8000b973385f379f6cc64659fa36
parent5cd525aca63a7b1bf09e0e679174a10f176ccc98 (diff)
extract assertion function
-rw-r--r--hello.go7
-rw-r--r--hello_test.go24
2 files changed, 25 insertions, 6 deletions
diff --git a/hello.go b/hello.go
index b454290..64287a8 100644
--- a/hello.go
+++ b/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_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)
+ })
}