-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_test.go
50 lines (44 loc) · 1.09 KB
/
index_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package slicey_test
import (
"testing"
"github.com/micvbang/go-helpy/slicey"
)
func TestLast(t *testing.T) {
tests := map[string]struct {
vs []int
expected int
}{
"nil": {vs: nil, expected: 0},
"empty": {vs: []int{}, expected: 0},
"one": {vs: []int{42}, expected: 42},
"multiple": {vs: []int{1, 2, 3}, expected: 3},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := slicey.Last(test.vs)
if got != test.expected {
t.Errorf("expected %d, got %d", test.expected, got)
}
})
}
}
func TestLastOrDefault(t *testing.T) {
tests := map[string]struct {
vs []int
def int
expected int
}{
"nil": {vs: nil, def: 42, expected: 42},
"empty": {vs: []int{}, def: 42, expected: 42},
"one": {vs: []int{1337}, def: 42, expected: 1337},
"multiple": {vs: []int{1, 2, 3}, def: 42, expected: 3},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
got := slicey.LastOrDefault(test.vs, test.def)
if got != test.expected {
t.Errorf("expected %d, got %d", test.expected, got)
}
})
}
}