11 Commits

8 changed files with 142 additions and 20 deletions

2
go.mod
View File

@@ -1,3 +1,3 @@
module go_with_tests module ak-homelab.duckdns.org/gitea/hoborg/go_with_tests
go 1.26.1 go 1.26.1

View File

@@ -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("Hoborg"))
}

36
hello/hello.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import "fmt"
const (
spanish = "Spanish"
french = "French"
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)
func Hello(name, language string) string {
if name == "" {
name = "World"
}
prefix := greetingPrefix(language)
return prefix + 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("Hoborg", ""))
}

View File

@@ -4,13 +4,23 @@ 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("Hoborg") got := Hello("Hoborg", "")
want := "Hello, Hoborg" want := "Hello, Hoborg"
assertMessage(t, got, want) assertMessage(t, got, want)
}) })
t.Run("say 'Hello, World' when an empty string is given", func(t *testing.T) { t.Run("say 'Hello, World' when an empty string is given", func(t *testing.T) {
got := Hello("") got := Hello("", "")
want := "Hello, Worldi" want := "Hello, World"
assertMessage(t, got, want)
})
t.Run("hello in Spanish", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
want := "Hola, Elodie"
assertMessage(t, got, want)
})
t.Run("hello in French", func(t *testing.T) {
got := Hello("Émile", "French")
want := "Bonjour, Émile"
assertMessage(t, got, want) assertMessage(t, got, want)
}) })
} }

22
integers/adder_test.go Normal file
View File

@@ -0,0 +1,22 @@
package integers
import (
"fmt"
"testing"
)
func TestAdder(t *testing.T) {
sum := Add(2, 2)
// Output: 9
expected := 4
if sum != expected {
t.Errorf("expected '%d' but got '%d'", expected, sum)
}
}
// Runnable example testing
func ExampleAdd() {
fmt.Println(Add(2, 4))
// Output: 6
}

6
integers/integers.go Normal file
View File

@@ -0,0 +1,6 @@
package integers
// Add two numbers together
func Add(x, y int) int {
return x + y
}

17
iteration/iteration.go Normal file
View File

@@ -0,0 +1,17 @@
package iteration
import "strings"
const defaultRepeatCount = 5
func RepeatFive(character string) string {
return Repeat(character, defaultRepeatCount)
}
func Repeat(character string, count int) string {
if count == 0 {
count = defaultRepeatCount
}
return strings.Repeat(character, count)
}

View File

@@ -0,0 +1,47 @@
package iteration
import (
"fmt"
"testing"
)
func TestRepeat(t *testing.T) {
t.Run("RepeatFive a", func(t *testing.T) {
got := RepeatFive("a")
want := "aaaaa"
assertResult(t, got, want)
})
t.Run("RepeatFive z", func(t *testing.T) {
got := RepeatFive("z")
want := "zzzzz"
assertResult(t, got, want)
})
t.Run("repeat z, 11 times", func(t *testing.T) {
got := Repeat("z", 11)
want := "zzzzzzzzzzz"
assertResult(t, got, want)
})
}
func ExampleRepeatFive() {
fmt.Println(RepeatFive("y"))
// Output: yyyyy
}
func ExampleRepeat() {
fmt.Println(Repeat("t", 7))
// Output: ttttttt
}
func assertResult(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
func BenchmarkRepeat(b *testing.B) {
for b.Loop() {
RepeatFive("a")
}
}