Compare commits

..

8 Commits

Author SHA1 Message Date
080f31017d Add structs package 2022-01-05 17:56:56 +01:00
a6a921cedc Add pointers package 2022-01-05 17:56:49 +01:00
01e7a6fa26 Add arrays package 2022-01-05 17:56:38 +01:00
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
13 changed files with 386 additions and 0 deletions

23
arrays/sum.go Normal file
View File

@@ -0,0 +1,23 @@
package main
func Sum(numbers []int) int {
sum := 0
for _, n := range numbers {
sum += n
}
return sum
}
func SumAllTails(numbersToSum ...[]int) []int {
sums := make([]int, 0, len(numbersToSum))
for _, numbers := range numbersToSum {
if len(numbers) == 0 {
sums = append(sums, 0)
} else {
tail := numbers[1:]
sums = append(sums, Sum(tail))
}
}
return sums
}

62
arrays/sum_test.go Normal file
View File

@@ -0,0 +1,62 @@
package main
import (
"reflect"
"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)
}
})
}
//func TestSumAll(t *testing.T) {
//got := SumAll([]int{1, 2}, []int{0, 9})
//want := []int{3, 9}
//if !reflect.DeepEqual(got, want) {
//t.Errorf("got %v want %v", got, want)
//}
//}
func TestSumAllTails(t *testing.T) {
checkSums := func(t testing.TB, got, want []int) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
t.Run("make the sums of tails of", func(t *testing.T) {
got := SumAllTails([]int{1, 2}, []int{0, 9})
want := []int{2, 9}
checkSums(t, got, want)
})
t.Run("safely sum empty slices", func(t *testing.T) {
got := SumAllTails([]int{}, []int{3, 4, 5})
want := []int{0, 9}
checkSums(t, got, want)
})
}

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
}

35
pointers/wallet.go Normal file
View File

@@ -0,0 +1,35 @@
package pointers
import (
"errors"
"fmt"
)
type Bitcoin int
type Wallet struct {
balance Bitcoin
}
func (w *Wallet) Deposit(a Bitcoin) {
w.balance += a
}
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
func (w *Wallet) Withdraw(a Bitcoin) error {
if a > w.balance {
return ErrInsufficientFunds
}
w.balance -= a
return nil
}
func (w Wallet) Balance() Bitcoin {
return w.balance
}
func (b Bitcoin) String() string {
return fmt.Sprintf("%d BTC", b)
}

52
pointers/wallet_test.go Normal file
View File

@@ -0,0 +1,52 @@
package pointers
import (
"testing"
)
func TestWallet(t *testing.T) {
assertBalance := func(t testing.TB, w Wallet, want Bitcoin) {
t.Helper()
balance := w.Balance()
if balance != want {
t.Errorf("got %s want %s", balance, want)
}
}
t.Run("Deposit", func(t *testing.T) {
wallet := Wallet{}
wallet.Deposit(Bitcoin(10))
want := Bitcoin(10)
assertBalance(t, wallet, want)
})
t.Run("Withdraw", func(t *testing.T) {
wallet := Wallet{balance: Bitcoin(20)}
wallet.Withdraw(Bitcoin(10))
want := Bitcoin(10)
assertBalance(t, wallet, want)
})
assertError := func(t testing.TB, got, want error) {
t.Helper()
if got == nil {
t.Fatal("wanted an error but didn't get one")
}
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
t.Run("Withdraw insufficient funds", func(t *testing.T) {
startingBalance := Bitcoin(20)
wallet := Wallet{startingBalance}
err := wallet.Withdraw(Bitcoin(100))
assertError(t, err, ErrInsufficientFunds)
assertBalance(t, wallet, startingBalance)
})
}

37
structs/shapes.go Normal file
View File

@@ -0,0 +1,37 @@
package structs
import "math"
type Shape interface {
Area() float64
}
type Rectangle struct {
Width float64
Height float64
}
type Circle struct {
Radius float64
}
type Triangle struct {
Base float64
Height float64
}
func Perimiter(r Rectangle) float64 {
return 2 * (r.Width + r.Height)
}
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
func (t Triangle) Area() float64 {
return t.Base * t.Height / 2
}

38
structs/shapes_test.go Normal file
View File

@@ -0,0 +1,38 @@
package structs
import "testing"
func TestPerimiter(t *testing.T) {
rectangle := Rectangle{10.0, 10.0}
got := Perimiter(rectangle)
want := 40.0
if got != want {
t.Errorf("got %.2f want %.2f", got, want)
}
}
func TestArea(t *testing.T) {
areaTests := []struct {
name string
shape Shape
hasArea float64
}{
{name: "Rectangle", shape: Rectangle{Width: 12, Height: 6}, hasArea: 72.0},
{name: "Circle", shape: Circle{Radius: 10}, hasArea: 314.1592653589793},
{name: "Triangle", shape: Triangle{Base: 12, Height: 6}, hasArea: 36.0},
}
for _, tt := range areaTests {
// using tt.name from the case to use it as the `t.Run` test name
t.Run(tt.name, func(t *testing.T) {
got := tt.shape.Area()
if got != tt.hasArea {
t.Errorf("%#v got %g want %g", tt.shape, got, tt.hasArea)
}
})
}
}