Compare commits

..

No commits in common. "3569bcd48594d9141e713b9ca5f91a0f3c401e80" and "39be6e1d4d37fed10f56636b02ffb82b89bad141" have entirely different histories.

9 changed files with 0 additions and 177 deletions

View File

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

View File

@ -1,29 +0,0 @@
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
View File

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

View File

@ -1,33 +0,0 @@
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", ""))
}

View File

@ -1,28 +0,0 @@
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)
})
}

View File

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

View File

@ -1,21 +0,0 @@
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
}

View File

@ -1,16 +0,0 @@
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)
}

View File

@ -1,32 +0,0 @@
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
}