From 619c6b215e4ddb006699b7d035702a6dd9de3089 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Mon, 3 Jan 2022 12:50:52 +0100 Subject: [PATCH] Add hello package with tests --- hello/go.mod | 3 +++ hello/hello.go | 33 +++++++++++++++++++++++++++++++++ hello/hello_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 hello/go.mod create mode 100644 hello/hello.go create mode 100644 hello/hello_test.go diff --git a/hello/go.mod b/hello/go.mod new file mode 100644 index 0000000..afdf576 --- /dev/null +++ b/hello/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.17 diff --git a/hello/hello.go b/hello/hello.go new file mode 100644 index 0000000..8f2b2f3 --- /dev/null +++ b/hello/hello.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +const spanish = "Spanish" +const french = "French" + +const englishHelloPrefix = "Hello, " +const spanishHelloPrefix = "Hola, " +const 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 french: + prefix = frenchHelloPrefix + case spanish: + prefix = spanishHelloPrefix + default: + prefix = englishHelloPrefix + } + return +} +func main() { + fmt.Println(Hello("world", "")) +} diff --git a/hello/hello_test.go b/hello/hello_test.go new file mode 100644 index 0000000..047f622 --- /dev/null +++ b/hello/hello_test.go @@ -0,0 +1,28 @@ +package main + +import "testing" + +func TestHello(t *testing.T) { + assertCorrectMessage := func(t testing.TB, got, want string) { + t.Helper() + if got != want { + t.Errorf("got %q want %q", got, want) + } + } + + t.Run("saying hello to people", func(t *testing.T) { + got := Hello("Chris", "") + want := "Hello, Chris" + assertCorrectMessage(t, got, want) + }) + t.Run("empty string defaults to 'World'", func(t *testing.T) { + 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) + }) +}