Commit fc4bba1

mo khan <mo.khan@gmail.com>
2019-10-19 22:09:03
extract assertion function
1 parent 5cd525a
Changed files (2)
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() {
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)
+  })
 }