helloworld
This commit is contained in:
parent
4f4cc61f00
commit
80e3792b30
@ -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
36
helloworld/hello.go
Normal 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", ""))
|
||||||
|
}
|
@ -4,15 +4,25 @@ import "testing"
|
|||||||
|
|
||||||
func TestHello(t *testing.T) {
|
func TestHello(t *testing.T) {
|
||||||
t.Run("saying hello to people", func(t *testing.T) {
|
t.Run("saying hello to people", func(t *testing.T) {
|
||||||
got := Hello("Chris")
|
got := Hello("Chris", "")
|
||||||
want := "Hello, Chris"
|
want := "Hello, Chris"
|
||||||
assertCorrectMessage(t, got, want)
|
assertCorrectMessage(t, got, want)
|
||||||
})
|
})
|
||||||
t.Run("say 'Hello, World' when an empty string is supplied", func(t *testing.T) {
|
t.Run("say 'Hello, World' when an empty string is supplied", func(t *testing.T) {
|
||||||
got := Hello("")
|
got := Hello("", "")
|
||||||
want := "Hello, World"
|
want := "Hello, World"
|
||||||
assertCorrectMessage(t, got, want)
|
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) {
|
func assertCorrectMessage(t testing.TB, got, want string) {
|
Loading…
x
Reference in New Issue
Block a user