summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2019-10-19 16:16:42 -0600
committermo khan <mo.khan@gmail.com>2019-10-19 16:16:42 -0600
commitfeca17c66dbdfbe2054bc1cad6220fd7a4769ae0 (patch)
tree8cf7d526af23fd3a39ba6fc1291a76567bf5ac37
parentfc4bba16caff7c472e1b33ddb71c583c59b96605 (diff)
extract greeting prefix function
-rw-r--r--hello.go23
-rw-r--r--hello_test.go16
2 files changed, 34 insertions, 5 deletions
diff --git a/hello.go b/hello.go
index 64287a8..4a0a1bd 100644
--- a/hello.go
+++ b/hello.go
@@ -2,15 +2,32 @@ package main
import "fmt"
+const spanish = "Spanish"
+const french = "French"
const englishHelloPrefix = "Hello, "
+const spanishHelloPrefix = "Hola, "
+const frenchHelloPrefix = "Bonjour, "
-func Hello(name string) string {
+func Hello(name string, language string) string {
if name == "" {
name = "World"
}
- return englishHelloPrefix + name
+
+ return greetingPrefix(language) + name
+}
+
+func greetingPrefix(language string) (prefix string) {
+ switch language {
+ case french:
+ prefix = frenchHelloPrefix
+ case spanish:
+ prefix = spanishHelloPrefix
+ default:
+ prefix = englishHelloPrefix
+ }
+ return
}
func main() {
- fmt.Println(Hello("world"))
+ fmt.Println(Hello("world", ""))
}
diff --git a/hello_test.go b/hello_test.go
index 41bcfb1..17a7822 100644
--- a/hello_test.go
+++ b/hello_test.go
@@ -11,16 +11,28 @@ func TestHello(t *testing.T) {
}
t.Run("saying hello to people", func(t *testing.T) {
- got := Hello("mo")
+ 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("")
+ got := Hello("", "")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
+
+ t.Run("in Spanish", func(t *testing.T) {
+ got := Hello("Elodie", "Spanish")
+ want := "Hola, Elodie"
+ assertCorrectMessage(t, got, want)
+ })
+
+ t.Run("in French", func(t *testing.T) {
+ got := Hello("Elodie", "French")
+ want := "Bonjour, Elodie"
+ assertCorrectMessage(t, got, want)
+ })
}