Commit 984800d
Changed files (8)
adder.go
@@ -1,5 +1,5 @@
package main
func Add(x, y int) int {
- return x + y
+ return x + y
}
adder_test.go
@@ -4,16 +4,16 @@ import "testing"
import "fmt"
func TestAdder(t *testing.T) {
- sum := Add(2, 2)
- expected := 4
+ sum := Add(2, 2)
+ expected := 4
- if sum != expected {
- t.Errorf("expected '%d' but got '%d'", expected, sum)
- }
+ if sum != expected {
+ t.Errorf("expected '%d' but got '%d'", expected, sum)
+ }
}
func ExampleAdd() {
- sum := Add(1, 5)
- fmt.Println(sum)
- // Output: 6
+ sum := Add(1, 5)
+ fmt.Println(sum)
+ // Output: 6
}
hello.go
@@ -9,25 +9,25 @@ const spanishHelloPrefix = "Hola, "
const frenchHelloPrefix = "Bonjour, "
func Hello(name string, language string) string {
- if name == "" {
- name = "World"
- }
+ if name == "" {
+ name = "World"
+ }
- return greetingPrefix(language) + name
+ return greetingPrefix(language) + name
}
func greetingPrefix(language string) (prefix string) {
- switch language {
- case french:
- prefix = frenchHelloPrefix
- case spanish:
- prefix = spanishHelloPrefix
- default:
- prefix = englishHelloPrefix
- }
- return
+ switch language {
+ case french:
+ prefix = frenchHelloPrefix
+ case spanish:
+ prefix = spanishHelloPrefix
+ default:
+ prefix = englishHelloPrefix
+ }
+ return
}
func main() {
- fmt.Println(Hello("world", ""))
+ fmt.Println(Hello("world", ""))
}
hello_test.go
@@ -3,36 +3,36 @@ package main
import "testing"
func TestHello(t *testing.T) {
- assertCorrectMessage := func(t *testing.T, 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("mo", "")
- want := "Hello, mo"
-
- 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 Spanish", func(t *testing.T) {
- got := Hello("Elodie", "Spanish")
- want := "Hola, Elodie"
- assertCorrectMessage(t, got, want)
- })
-
- t.Run("in French", func(t *testing.T) {
- got := Hello("Elodie", "French")
- want := "Bonjour, Elodie"
- assertCorrectMessage(t, got, want)
- })
+ assertCorrectMessage := func(t *testing.T, 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("mo", "")
+ want := "Hello, mo"
+
+ 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 Spanish", func(t *testing.T) {
+ got := Hello("Elodie", "Spanish")
+ want := "Hola, Elodie"
+ assertCorrectMessage(t, got, want)
+ })
+
+ t.Run("in French", func(t *testing.T) {
+ got := Hello("Elodie", "French")
+ want := "Bonjour, Elodie"
+ assertCorrectMessage(t, got, want)
+ })
}
repeat.go
@@ -3,5 +3,5 @@ package main
import "strings"
func Repeat(character string, times int) string {
- return strings.Repeat(character, times)
+ return strings.Repeat(character, times)
}
repeat_test.go
@@ -4,32 +4,32 @@ import "fmt"
import "testing"
func TestRepeat(t *testing.T) {
- t.Run("default", func(t *testing.T) {
- repeated := Repeat("a", 5)
- expected := "aaaaa"
+ t.Run("default", func(t *testing.T) {
+ repeated := Repeat("a", 5)
+ expected := "aaaaa"
- if repeated != expected {
- t.Errorf("expected %q but got %q", expected, repeated)
- }
- })
+ if repeated != expected {
+ t.Errorf("expected %q but got %q", expected, repeated)
+ }
+ })
- t.Run("custom interval", func(t *testing.T) {
- repeated := Repeat("a", 10)
- expected := "aaaaaaaaaa"
+ t.Run("custom interval", func(t *testing.T) {
+ repeated := Repeat("a", 10)
+ expected := "aaaaaaaaaa"
- if repeated != expected {
- t.Errorf("expected %q but got %q", expected, repeated)
- }
- })
+ if repeated != expected {
+ t.Errorf("expected %q but got %q", expected, repeated)
+ }
+ })
}
func BenchmarkRepeat(b *testing.B) {
- for i := 0; i < b.N; i++ {
- Repeat("a", 5)
- }
+ for i := 0; i < b.N; i++ {
+ Repeat("a", 5)
+ }
}
func ExampleRepeat() {
- fmt.Println(Repeat("x", 10))
- // Output: xxxxxxxxxx
+ fmt.Println(Repeat("x", 10))
+ // Output: xxxxxxxxxx
}
sum.go
@@ -1,9 +1,9 @@
package main
func Sum(numbers []int) int {
- sum := 0
- for _, number := range numbers {
- sum += number
- }
- return sum
+ sum := 0
+ for _, number := range numbers {
+ sum += number
+ }
+ return sum
}
sum_test.go
@@ -3,23 +3,23 @@ package main
import "testing"
func TestSum(test *testing.T) {
- test.Run("collection of 5 numbers", func(test *testing.T) {
- numbers := []int{1, 2, 3, 4, 5}
- got := Sum(numbers)
- want := 15
+ test.Run("collection of 5 numbers", func(test *testing.T) {
+ numbers := []int{1, 2, 3, 4, 5}
+ got := Sum(numbers)
+ want := 15
- if got != want {
- test.Errorf("got %d want %d given, %v", got, want, numbers)
- }
- })
+ if got != want {
+ test.Errorf("got %d want %d given, %v", got, want, numbers)
+ }
+ })
- test.Run("collection of any size", func(test *testing.T) {
- numbers := []int{1, 2, 3}
- got := Sum(numbers)
- want := 6
+ test.Run("collection of any size", func(test *testing.T) {
+ numbers := []int{1, 2, 3}
+ got := Sum(numbers)
+ want := 6
- if got != want {
- test.Errorf("got %d want %d given, %v", got, want, numbers)
- }
- })
+ if got != want {
+ test.Errorf("got %d want %d given, %v", got, want, numbers)
+ }
+ })
}