forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact_test.go
78 lines (62 loc) · 1.69 KB
/
compact_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
package funk
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompact(t *testing.T) {
var emptyFunc func() bool
emptyFuncPtr := &emptyFunc
nonEmptyFunc := func() bool { return true }
nonEmptyFuncPtr := &nonEmptyFunc
nonEmptyMap := map[int]int{1: 2}
nonEmptyMapPtr := &nonEmptyMap
var emptyMap map[int]int
emptyMapPtr := &emptyMap
var emptyChan chan bool
nonEmptyChan := make(chan bool, 1)
nonEmptyChan <- true
emptyChanPtr := &emptyChan
nonEmptyChanPtr := &nonEmptyChan
var emptyString string
emptyStringPtr := &emptyString
nonEmptyString := "42"
nonEmptyStringPtr := &nonEmptyString
testCases := []struct {
Arr interface{}
Result interface{}
}{
// Check with nils
{
[]interface{}{42, nil, (*int)(nil)},
[]interface{}{42},
},
// Check with functions
{
[]interface{}{42, emptyFuncPtr, emptyFunc, nonEmptyFuncPtr},
[]interface{}{42, nonEmptyFuncPtr},
},
// Check with slices, maps, arrays and channels
{
[]interface{}{
42, [2]int{}, map[int]int{}, []string{}, nonEmptyMapPtr, emptyMap,
emptyMapPtr, nonEmptyMap, nonEmptyChan, emptyChan, emptyChanPtr, nonEmptyChanPtr,
},
[]interface{}{42, nonEmptyMapPtr, nonEmptyMap, nonEmptyChan, nonEmptyChanPtr},
},
// Check with strings, numbers and booleans
{
[]interface{}{true, 0, float64(0), "", "42", emptyStringPtr, nonEmptyStringPtr, false},
[]interface{}{true, "42", nonEmptyStringPtr},
},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)
result := Compact(tc.Arr)
if !is.Equal(result, tc.Result) {
t.Errorf("%#v doesn't equal to %#v", result, tc.Result)
}
})
}
}