1 package funk
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestCompact(t *testing.T) {
11 var emptyFunc func() bool
12 emptyFuncPtr := &emptyFunc
13
14 nonEmptyFunc := func() bool { return true }
15 nonEmptyFuncPtr := &nonEmptyFunc
16
17 nonEmptyMap := map[int]int{1: 2}
18 nonEmptyMapPtr := &nonEmptyMap
19
20 var emptyMap map[int]int
21 emptyMapPtr := &emptyMap
22
23 var emptyChan chan bool
24 nonEmptyChan := make(chan bool, 1)
25 nonEmptyChan <- true
26
27 emptyChanPtr := &emptyChan
28 nonEmptyChanPtr := &nonEmptyChan
29
30 var emptyString string
31 emptyStringPtr := &emptyString
32
33 nonEmptyString := "42"
34 nonEmptyStringPtr := &nonEmptyString
35
36 testCases := []struct {
37 Arr interface{}
38 Result interface{}
39 }{
40
41 {
42 []interface{}{42, nil, (*int)(nil)},
43 []interface{}{42},
44 },
45
46
47 {
48 []interface{}{42, emptyFuncPtr, emptyFunc, nonEmptyFuncPtr},
49 []interface{}{42, nonEmptyFuncPtr},
50 },
51
52
53 {
54 []interface{}{
55 42, [2]int{}, map[int]int{}, []string{}, nonEmptyMapPtr, emptyMap,
56 emptyMapPtr, nonEmptyMap, nonEmptyChan, emptyChan, emptyChanPtr, nonEmptyChanPtr,
57 },
58 []interface{}{42, nonEmptyMapPtr, nonEmptyMap, nonEmptyChan, nonEmptyChanPtr},
59 },
60
61
62 {
63 []interface{}{true, 0, float64(0), "", "42", emptyStringPtr, nonEmptyStringPtr, false},
64 []interface{}{true, "42", nonEmptyStringPtr},
65 },
66 }
67
68 for idx, tc := range testCases {
69 t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
70 is := assert.New(t)
71 result := Compact(tc.Arr)
72
73 if !is.Equal(result, tc.Result) {
74 t.Errorf("%#v doesn't equal to %#v", result, tc.Result)
75 }
76 })
77 }
78 }
79
View as plain text