Handle empty string as "World"
This commit is contained in:
7
hello.go
7
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() {
|
||||
|
||||
@@ -3,10 +3,20 @@ package main
|
||||
import "testing"
|
||||
|
||||
func TestHello(t *testing.T) {
|
||||
t.Run("saying hello to people", func(t *testing.T) {
|
||||
got := Hello("Hoborg")
|
||||
want := "Hello, Hoborg"
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("say 'Hello, World' when an empty string is given", func(t *testing.T) {
|
||||
got := Hello("")
|
||||
want := "Hello, World"
|
||||
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user