better tests

This commit is contained in:
Michael Thomson 2024-05-25 00:20:18 -04:00
parent 3e36394e47
commit 4f4cc61f00
No known key found for this signature in database
2 changed files with 19 additions and 3 deletions

View File

@ -2,8 +2,13 @@ package main
import "fmt" import "fmt"
const englishHelloPrefix = "Hello, "
func Hello(name string) string { func Hello(name string) string {
return "Hello, " + name if name == "" {
name = "World"
}
return englishHelloPrefix + name
} }
func main() { func main() {

View File

@ -3,9 +3,20 @@ package main
import "testing" import "testing"
func TestHello(t *testing.T) { func TestHello(t *testing.T) {
got := Hello("Chris") t.Run("saying hello to people", func(t *testing.T) {
want := "Hello, Chris" got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
})
t.Run("say 'Hello, World' when an empty string is supplied", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
})
}
func assertCorrectMessage(t testing.TB, got, want string) {
t.Helper()
if got != want { if got != want {
t.Errorf("got %q want %q", got, want) t.Errorf("got %q want %q", got, want)
} }