cerrado @ b71c6c0e5b8dd00d44e40ac0551902a23cbe19d5

  1// go:build unit
  2
  3package u
  4
  5import (
  6	"strconv"
  7	"testing"
  8
  9	"github.com/google/go-cmp/cmp"
 10)
 11
 12func TestFirst(t *testing.T) {
 13	testCases := []struct {
 14		name  string
 15		slice []int
 16		first int
 17		exist bool
 18	}{
 19		{
 20			name:  "multiple items slice",
 21			slice: []int{1, 2, 3},
 22			first: 1,
 23			exist: true,
 24		},
 25		{
 26			name:  "single item slice",
 27			slice: []int{1},
 28			first: 1,
 29			exist: true,
 30		},
 31		{
 32			name:  "empty slice",
 33			slice: []int{},
 34			first: 0,
 35			exist: false,
 36		},
 37	}
 38	for _, tc := range testCases {
 39		t.Run(tc.name, func(t *testing.T) {
 40
 41			first, empty := First(tc.slice)
 42
 43			if first != tc.first {
 44				t.Errorf("Error first, want %d got %d", tc.first, first)
 45			}
 46
 47			if empty != tc.exist {
 48				t.Errorf("Error empty, want %t got %t", tc.exist, empty)
 49			}
 50
 51		})
 52	}
 53}
 54
 55func TestSubList(t *testing.T) {
 56	testCases := []struct {
 57		name  string
 58		slice []int
 59		size  int
 60		want  [][]int
 61	}{
 62		{
 63			name:  "sigle size sub list",
 64			slice: []int{1, 2, 3},
 65			size:  1,
 66			want:  [][]int{{1}, {2}, {3}},
 67		},
 68		{
 69			name:  "multiple size sub list",
 70			slice: []int{1, 2, 3, 4},
 71			size:  2,
 72			want:  [][]int{{1, 2}, {3, 4}},
 73		},
 74		{
 75			name:  "uneven multiple size sub list",
 76			slice: []int{1, 2, 3, 4, 5},
 77			size:  2,
 78			want:  [][]int{{1, 2}, {3, 4}, {5}},
 79		},
 80		{
 81			name:  "empty sub list",
 82			slice: []int{},
 83			size:  2,
 84			want:  [][]int{{}},
 85		},
 86	}
 87	for _, tc := range testCases {
 88		t.Run(tc.name, func(t *testing.T) {
 89
 90			subList := ChunkBy(tc.slice, tc.size)
 91
 92			if diff := cmp.Diff(tc.want, subList); diff != "" {
 93				t.Errorf("Wrong result given - wanted + got\n %s", diff)
 94			}
 95		})
 96	}
 97}
 98
 99func TestFirstOrZero(t *testing.T) {
100	testCases := []struct {
101		name  string
102		slice []int
103		first int
104	}{
105		{
106			name:  "multiple items slice",
107			slice: []int{1, 2, 3},
108			first: 1,
109		},
110		{
111			name:  "single item slice",
112			slice: []int{1},
113			first: 1,
114		},
115		{
116			name:  "empty slice",
117			slice: []int{},
118			first: 0,
119		},
120	}
121	for _, tc := range testCases {
122		t.Run(tc.name, func(t *testing.T) {
123
124			first := FirstOrZero(tc.slice)
125
126			if first != tc.first {
127				t.Errorf("Error first, want %d got %d", tc.first, first)
128			}
129
130		})
131	}
132}
133
134func TestMap(t *testing.T) {
135	testCases := []struct {
136		name string
137		in   []int
138		out  []string
139	}{
140		{
141			name: "empty",
142			in:   []int{},
143			out:  []string{},
144		},
145		{
146			name: "not empty",
147			in:   []int{1, 2, 3},
148			out:  []string{"1", "2", "3"},
149		},
150	}
151
152	for _, tc := range testCases {
153		t.Run(tc.name, func(t *testing.T) {
154			out := Map(tc.in, func(v int) string { return strconv.Itoa(v) })
155
156			if diff := cmp.Diff(tc.out, out); diff != "" {
157				t.Errorf("Map error:\n%s", diff)
158			}
159		})
160	}
161}