-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice_transform_test.go
82 lines (68 loc) · 2.78 KB
/
slice_transform_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gofn
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_MapSlice(t *testing.T) {
// Nil/Empty maps
assert.Equal(t, []float32{}, MapSlice[int, float32]([]int(nil), func(v int) float32 { return float32(v) }))
assert.Equal(t, []float32{}, MapSlice([]int{}, func(v int) float32 { return float32(v) }))
// []int -> []int
assert.Equal(t, []int{2, 4, 6}, MapSlice([]int{1, 2, 3}, func(v int) int { return v * 2 }))
// []int -> []float32
assert.Equal(t, []float32{2, 4, 6}, MapSlice([]int{1, 2, 3}, func(v int) float32 { return float32(v * 2) }))
}
func Test_MapSliceEx(t *testing.T) {
// Nil/Empty maps
r1, e := MapSliceEx[int, float32]([]int(nil), func(v int) (float32, error) { return float32(v), nil })
assert.Nil(t, e)
assert.Equal(t, []float32{}, r1)
r2, e := MapSliceEx([]int{}, func(v int) (float32, error) { return float32(v), nil })
assert.Nil(t, e)
assert.Equal(t, []float32{}, r2)
// []string -> []int
r3, e := MapSliceEx([]string{"1", "2", "3"}, ParseInt[int])
assert.Nil(t, e)
assert.Equal(t, []int{1, 2, 3}, r3)
// []string -> []int with error
_, e = MapSliceEx([]string{"1", "a", "3"}, ParseInt[int])
assert.ErrorIs(t, e, strconv.ErrSyntax)
}
func Test_MapSliceToMap(t *testing.T) {
// Nil/Empty maps
assert.Equal(t, map[int]bool{}, MapSliceToMap[int, int, bool]([]int(nil), func(v int) (int, bool) { return v, v > 0 }))
assert.Equal(t, map[int]bool{}, MapSliceToMap([]int{}, func(v int) (int, bool) { return v, v > 0 }))
// []int -> map[int]string
assert.Equal(t, map[int]string{1: "2", 2: "4", 3: "6"}, MapSliceToMap([]int{1, 2, 3},
func(v int) (int, string) { return v, strconv.Itoa(v * 2) }))
}
func Test_MapSliceToMapEx(t *testing.T) {
// Nil/Empty maps
r1, e := MapSliceToMapEx[int, int, bool]([]int(nil), func(v int) (int, bool, error) { return v, v > 0, nil })
assert.Nil(t, e)
assert.Equal(t, map[int]bool{}, r1)
r2, e := MapSliceToMapEx([]int{}, func(v int) (int, bool, error) { return v, v > 0, nil })
assert.Nil(t, e)
assert.Equal(t, map[int]bool{}, r2)
// []string -> []int
r3, e := MapSliceToMapEx([]string{"1", "2", "3"}, func(v string) (string, int, error) {
u, e := ParseInt[int](v)
return v, u, e
})
assert.Nil(t, e)
assert.Equal(t, map[string]int{"1": 1, "2": 2, "3": 3}, r3)
// []string -> []int with error
_, e = MapSliceToMapEx([]string{"1", "x", "3"}, func(v string) (string, int, error) {
u, e := ParseInt[int](v)
return v, u, e
})
assert.ErrorIs(t, e, strconv.ErrSyntax)
}
func Test_MapSliceToMapKeys(t *testing.T) {
// Nil/Empty maps
assert.Equal(t, map[int]struct{}{}, MapSliceToMapKeys[int]([]int(nil), struct{}{}))
assert.Equal(t, map[int]int{}, MapSliceToMapKeys([]int{}, 0))
// []int -> map[int]string
assert.Equal(t, map[int]string{1: "x", 2: "x", 3: "x"}, MapSliceToMapKeys([]int{1, 2, 3, 2}, "x"))
}