Skip to content

Commit

Permalink
feat: complete hello world with greetingPrefix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonhull committed Jul 7, 2021
1 parent d61c2ff commit 4a13c61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
28 changes: 22 additions & 6 deletions src/001-hello-world/hello.go
Expand Up @@ -2,16 +2,32 @@ package main

import "fmt"

const englishHelloPrefix = "Hello, "
const (
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)

func main() {
fmt.Println(Hello(""))
fmt.Println(Hello("", ""))
}

// greetingPrefix returns a string containingthe greeting prefix for the given language
func greetingPrefix(language string) string {
switch language {
case "Spanish":
return spanishHelloPrefix
case "French":
return frenchHelloPrefix
default:
return englishHelloPrefix
}
}

// Hello returns a string
func Hello(name string) string {
if name== "" {
return englishHelloPrefix + "World"
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
return englishHelloPrefix + name
return greetingPrefix(language) + name
}
13 changes: 9 additions & 4 deletions src/001-hello-world/hello_test.go
Expand Up @@ -3,22 +3,27 @@ package main
import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(got string, want string){
assertCorrectMessage := func(got string, want string) {
t.Helper()
if got != want {
t.Errorf("Expected %q, got %q", want, got)
}
}
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
got := Hello("Chris","")
want := "Hello, Chris"
assertCorrectMessage(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(got, want)
})

t.Run("in Spanish", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
want := "Hola, Elodie"
assertCorrectMessage(got, want)
})
}

0 comments on commit 4a13c61

Please sign in to comment.