-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
41 lines (33 loc) · 984 Bytes
/
map_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
package functools
import (
"testing"
"reflect"
)
func TestMap(t *testing.T) {
cases := []struct{
f interface{}
in interface{}
want interface{}
}{
{func(s string) int { return len(s) }, []string{"abc", "ab", "a"}, []int{3, 2, 1}},
{ToBool, []int{1, 2, 3, 0, 5, 6, 0, 8}, []bool{true, true, true, false, true, true, false, true}},
{func(n int) int { return n % 2 }, []int{1, 2, 3, 4, 5, 6}, []int{1, 0, 1, 0, 1, 0}},
{ToBool, []int{}, []bool{}},
}
for _, c := range cases {
got := Map(c.f, c.in)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("Map(func, %v) == %v want %v", c.in, got, c.want)
}
}
}
func TestMapSafe(t *testing.T) {
_, err := MapSafe(ToBool, 123)
if err == nil {
t.Errorf("FilterFuncSafe(ToBool, %v) should raise type error here", 123)
}
_, err = MapSafe(func (a, b int) bool { return a == b }, []int{1, 2, 3})
if err == nil {
t.Errorf("FilterFuncSafe(func, %v) should raise incorrect func error here", []int{1, 2, 3})
}
}