Testing in Go
This commit is contained in:
parent
06a7e9a858
commit
55b8428d2a
3 changed files with 61 additions and 2 deletions
3
go/go.mod
Normal file
3
go/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module hello
|
||||||
|
|
||||||
|
go 1.17
|
26
go/hello.go
26
go/hello.go
|
@ -2,6 +2,28 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
const englishHelloPrefix = "Hello, "
|
||||||
fmt.Println("Hello, World")
|
const welsh = "Welsh"
|
||||||
|
const welshHelloPrefix = "Helo, "
|
||||||
|
|
||||||
|
func Hello(name string, language string) string {
|
||||||
|
if name == "" {
|
||||||
|
name = "World"
|
||||||
|
}
|
||||||
|
|
||||||
|
return greetingPrefix(language) + name
|
||||||
|
}
|
||||||
|
|
||||||
|
func greetingPrefix(language string) (prefix string) {
|
||||||
|
prefix = englishHelloPrefix
|
||||||
|
|
||||||
|
if language == welsh {
|
||||||
|
prefix = welshHelloPrefix
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(Hello("Oliver", ""))
|
||||||
}
|
}
|
||||||
|
|
34
go/hello_test.go
Normal file
34
go/hello_test.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestHello(t *testing.T) {
|
||||||
|
assertCorrectMessage := func(t testing.TB, got string, 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("Oliver", "")
|
||||||
|
want := "Hello, Oliver"
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("in Welsh", func (t *testing.T) {
|
||||||
|
got := Hello("Oliver", "Welsh")
|
||||||
|
want := "Helo, Oliver"
|
||||||
|
|
||||||
|
assertCorrectMessage(t, got, want)
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue