From ca7d449b449eeeb7d5a7ffb73de59e97cf73cd3d Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 27 Feb 2024 09:27:03 -0500 Subject: [PATCH] Hello world done --- learn_go_with_tests/helloworld/hello.go | 45 ++++++++++++++++++-- learn_go_with_tests/helloworld/hello_test.go | 32 +++++++++++++- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/learn_go_with_tests/helloworld/hello.go b/learn_go_with_tests/helloworld/hello.go index e5e584e..6d78942 100644 --- a/learn_go_with_tests/helloworld/hello.go +++ b/learn_go_with_tests/helloworld/hello.go @@ -1,12 +1,49 @@ +// Provides a Hello world example with tests package main -import "fmt" +import ( + "fmt" +) + +// Constants can be grouped in a block. +// Add a line between related consts for +// readbility +const ( + french = "French" + spanish = "Spanish" + + englishHelloPrefix = "Hello, " + spanishHelloPrefix = "Hola, " + frenchHelloPrefix = "Bonjour, " +) // Hello prints the string "Hello, world" -func Hello(name string) string { - return "Hello, " + name +func Hello(name string, language string) string { + if name == "" { + name = "World" + } + + return greetingPrefix(language) + name +} + +// greetingPrefix Using a named return value will initialize a "zero value" +// variable which as you can see below can be assigned to. +// this will display in Go Doc for your function. +// Lower case letter functions are "private" functions. They are internal to implementation. +func greetingPrefix(language string) (prefix string) { + switch language { + case french: + prefix = frenchHelloPrefix + case spanish: + prefix = spanishHelloPrefix + default: + prefix = englishHelloPrefix + } + // we use names return value in function signature + return + } func main() { - fmt.Println(Hello("world")) + fmt.Println(Hello("world", "")) } diff --git a/learn_go_with_tests/helloworld/hello_test.go b/learn_go_with_tests/helloworld/hello_test.go index 2ea7cbe..fcfc523 100644 --- a/learn_go_with_tests/helloworld/hello_test.go +++ b/learn_go_with_tests/helloworld/hello_test.go @@ -3,9 +3,37 @@ package main import "testing" func TestHello(t *testing.T) { - got := Hello("Drew") - expected := "Hello, Drew" + t.Run("saying hello to people", func(t *testing.T) { + got := Hello("Drew", "") + expected := "Hello, Drew" + assertCorrectMessage(t, got, expected) + }) + + t.Run("say 'Hello, world' when an empty string is supplied", func(t *testing.T) { + got := Hello("", "") + expected := "Hello, World" + assertCorrectMessage(t, got, expected) + }) + + t.Run("in Spanish", func(t *testing.T) { + got := Hello("Elodie", "Spanish") + expected := "Hola, Elodie" + assertCorrectMessage(t, got, expected) + }) + + t.Run("in French", func(t *testing.T) { + got := Hello("Drew", "French") + expected := "Bonjour, Drew" + assertCorrectMessage(t, got, expected) + }) +} + +func assertCorrectMessage(t testing.TB, got, expected string) { + // Needed to tell the test suite that this method is a helper. + // By doing this when it fails the line number reported will be in + // our function call rather than inside our test helper. + t.Helper() if got != expected { t.Errorf("got %q, expected %q", got, expected) }