Compare commits

..

5 Commits

Author SHA1 Message Date
3569bcd485 Add package arrays 2022-01-03 17:17:14 +01:00
cb26678c66 Add iteration package 2022-01-03 17:17:02 +01:00
c37e1c26e1 Add integers package 2022-01-03 13:09:37 +01:00
323f4aa606 Put go.mod in the right place 2022-01-03 13:09:25 +01:00
619c6b215e Add hello package with tests 2022-01-03 12:50:52 +01:00
9 changed files with 177 additions and 0 deletions

9
arrays/sum.go Normal file
View File

@ -0,0 +1,9 @@
package main
func Sum(numbers []int) int {
sum := 0
for _, n := range numbers {
sum += n
}
return sum
}

29
arrays/sum_test.go Normal file
View File

@ -0,0 +1,29 @@
package main
import "testing"
func TestSum(t *testing.T) {
t.Run("collection of 5 numbers", func(t *testing.T) {
numbers := []int{1, 2, 3, 4, 5}
got := Sum(numbers)
want := 15
if got != want {
t.Errorf("got %d want %d given, %v", got, want, numbers)
}
})
t.Run("collection of any size", func(t *testing.T) {
numbers := []int{1, 2, 3}
got := Sum(numbers)
want := 6
if got != want {
t.Errorf("got %d want %d given, %v", got, want, numbers)
}
})
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/halfdan/learn-go-with-tests
go 1.17

33
hello/hello.go Normal file
View File

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

28
hello/hello_test.go Normal file
View File

@ -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)
})
}

6
integers/adder.go Normal file
View File

@ -0,0 +1,6 @@
package integers
// Add takes two integers and return the sum of them.
func Add(a, b int) int {
return a + b
}

21
integers/adder_test.go Normal file
View File

@ -0,0 +1,21 @@
package integers
import (
"fmt"
"testing"
)
func TestAdder(t *testing.T) {
sum := Add(2, 2)
expected := 4
if sum != expected {
t.Errorf("expected %v, got %v", expected, sum)
}
}
func ExampleAdd() {
sum := Add(1, 5)
fmt.Println(sum)
// Output: 6
}

16
iteration/repeat.go Normal file
View File

@ -0,0 +1,16 @@
package iteration
import "strings"
func Repeat(character string, count int) string {
var repeated string
for i := 0; i < count; i++ {
repeated += character
}
return repeated
}
func StringsRepeat(s string, count int) string {
return strings.Repeat(s, count)
}

32
iteration/repeat_test.go Normal file
View File

@ -0,0 +1,32 @@
package iteration
import (
"fmt"
"testing"
)
func TestRepeat(t *testing.T) {
repeated := Repeat("a", 5)
expected := "aaaaa"
if repeated != expected {
t.Errorf("expected %v, got %v", expected, repeated)
}
}
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat("a", 5)
}
}
func BenchmarkStringsRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
StringsRepeat("a", 5)
}
}
func ExampleRepeat() {
data := Repeat("a", 5)
fmt.Println(data)
// Outputs: aaaaa
}