// go:build unit package u import ( "strconv" "testing" "github.com/google/go-cmp/cmp" ) func TestFirst(t *testing.T) { testCases := []struct { name string slice []int first int exist bool }{ { name: "multiple items slice", slice: []int{1, 2, 3}, first: 1, exist: true, }, { name: "single item slice", slice: []int{1}, first: 1, exist: true, }, { name: "empty slice", slice: []int{}, first: 0, exist: false, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { first, empty := First(tc.slice) if first != tc.first { t.Errorf("Error first, want %d got %d", tc.first, first) } if empty != tc.exist { t.Errorf("Error empty, want %t got %t", tc.exist, empty) } }) } } func TestSubList(t *testing.T) { testCases := []struct { name string slice []int size int want [][]int }{ { name: "sigle size sub list", slice: []int{1, 2, 3}, size: 1, want: [][]int{{1}, {2}, {3}}, }, { name: "multiple size sub list", slice: []int{1, 2, 3, 4}, size: 2, want: [][]int{{1, 2}, {3, 4}}, }, { name: "uneven multiple size sub list", slice: []int{1, 2, 3, 4, 5}, size: 2, want: [][]int{{1, 2}, {3, 4}, {5}}, }, { name: "empty sub list", slice: []int{}, size: 2, want: [][]int{{}}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { subList := ChunkBy(tc.slice, tc.size) if diff := cmp.Diff(tc.want, subList); diff != "" { t.Errorf("Wrong result given - wanted + got\n %s", diff) } }) } } func TestFirstOrZero(t *testing.T) { testCases := []struct { name string slice []int first int }{ { name: "multiple items slice", slice: []int{1, 2, 3}, first: 1, }, { name: "single item slice", slice: []int{1}, first: 1, }, { name: "empty slice", slice: []int{}, first: 0, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { first := FirstOrZero(tc.slice) if first != tc.first { t.Errorf("Error first, want %d got %d", tc.first, first) } }) } } func TestMap(t *testing.T) { testCases := []struct { name string in []int out []string }{ { name: "empty", in: []int{}, out: []string{}, }, { name: "not empty", in: []int{1, 2, 3}, out: []string{"1", "2", "3"}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { out := Map(tc.in, func(v int) string { return strconv.Itoa(v) }) if diff := cmp.Diff(tc.out, out); diff != "" { t.Errorf("Map error:\n%s", diff) } }) } }