package main import ( "reflect" "testing" ) func TestAdder(t *testing.T) { assertCorrectMessage := func(t testing.TB, sum, expect int) { if sum != expect { t.Errorf("expected %d but got %d", sum, expect) } } t.Run("2 + 2", func(t *testing.T) { numbers := []int{2, 2} sum := Add(numbers) expect := 4 assertCorrectMessage(t, sum, expect) }) t.Run("1 + 1", func(t *testing.T) { numbers := []int{1, 1} sum := Add(numbers) expect := 2 assertCorrectMessage(t, sum, expect) }) t.Run("1 + 2 + 3 + 4 + 5", func(t *testing.T) { numbers := []int{1, 2, 3, 4, 5} sum := Add(numbers) expect := 15 assertCorrectMessage(t, sum, expect) }) } func TestAdderSlice(t *testing.T) { got := SumAll([]int{1, 2}, []int{0, 9}) want := []int{3, 9} // this is not type safe 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 some of some slices", 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) }) }