helloworld

This commit is contained in:
Michael Thomson 2024-05-25 00:30:38 -04:00
parent 4f4cc61f00
commit 80e3792b30
No known key found for this signature in database
4 changed files with 48 additions and 18 deletions

View File

@ -1,16 +0,0 @@
package main
import "fmt"
const englishHelloPrefix = "Hello, "
func Hello(name string) string {
if name == "" {
name = "World"
}
return englishHelloPrefix + name
}
func main() {
fmt.Println(Hello("world"))
}

36
helloworld/hello.go Normal file
View File

@ -0,0 +1,36 @@
package main
import "fmt"
const (
spanish = "Spanish"
french = "French"
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
return greetingPrefix(language) + name
}
func greetingPrefix(language string) (prefix string) {
switch language {
case spanish:
prefix = spanishHelloPrefix
case french:
prefix = frenchHelloPrefix
default:
prefix = englishHelloPrefix
}
return
}
func main() {
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("Michael", "French")
want := "Bonjour, Michael"
assertCorrectMessage(t, got, want)
})
}
func assertCorrectMessage(t testing.TB, got, want string) {