Multiple language greetings

This commit is contained in:
Timothy Warren 2023-09-29 10:25:17 -04:00
parent 2402ace394
commit 2c9a6e3049
2 changed files with 28 additions and 5 deletions

View File

@ -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", ""))
}

View File

@ -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) {