From 2c9a6e30490cfa8cb9599d1d5954fd79e733922d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 29 Sep 2023 10:25:17 -0400 Subject: [PATCH] Multiple language greetings --- hello/hello.go | 19 ++++++++++++++++--- hello/hello_test.go | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/hello/hello.go b/hello/hello.go index f349f14..ddb3c5c 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -2,16 +2,29 @@ package main import "fmt" +const french = "French" +const spanish = "Spanish" const englishHelloPrefix = "Hello, " +const frenchHelloPrefix = "Bonjour, " +const spanishHelloPrefix = "Hola, " -func Hello(name string) string { +func Hello(name string, language string) string { if name == "" { name = "World" } - return englishHelloPrefix + name + prefix := englishHelloPrefix + + switch language { + case french: + prefix = frenchHelloPrefix + case spanish: + prefix = spanishHelloPrefix + } + + return prefix + name } func main() { - fmt.Println(Hello("world")) + fmt.Println(Hello("world", "")) } diff --git a/hello/hello_test.go b/hello/hello_test.go index df57e21..cfe1280 100644 --- a/hello/hello_test.go +++ b/hello/hello_test.go @@ -4,15 +4,25 @@ import "testing" func TestHello(t *testing.T) { t.Run("saying hello to people", func(t *testing.T) { - got := Hello("Chris") + 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("") + 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("Elise", "French") + want := "Bonjour, Elise" + assertCorrectMessage(t, got, want) + }) } func assertCorrectMessage(t testing.TB, got, want string) {