1 package funk
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestChain(t *testing.T) {
11 testCases := []struct {
12 In interface{}
13 Panic string
14 }{
15
16 {In: []int{0, 1, 2}},
17 {In: []string{"aaa", "bbb", "ccc"}},
18 {In: []interface{}{0, false, "___"}},
19
20
21 {In: map[int]string{0: "aaa", 1: "bbb", 2: "ccc"}},
22 {In: map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"}},
23 {In: map[int]interface{}{0: 0, 1: false, 2: "___"}},
24
25
26 {false, "Type bool is not supported by Chain"},
27 {0, "Type int is not supported by Chain"},
28 }
29
30 for idx, tc := range testCases {
31 t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
32 is := assert.New(t)
33
34 if tc.Panic != "" {
35 is.PanicsWithValue(tc.Panic, func() {
36 Chain(tc.In)
37 })
38 return
39 }
40
41 chain := Chain(tc.In)
42 collection := chain.(*chainBuilder).collection
43
44 is.Equal(collection, tc.In)
45 })
46 }
47 }
48
49 func TestLazyChain(t *testing.T) {
50 testCases := []struct {
51 In interface{}
52 Panic string
53 }{
54
55 {In: []int{0, 1, 2}},
56 {In: []string{"aaa", "bbb", "ccc"}},
57 {In: []interface{}{0, false, "___"}},
58
59
60 {In: map[int]string{0: "aaa", 1: "bbb", 2: "ccc"}},
61 {In: map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"}},
62 {In: map[int]interface{}{0: 0, 1: false, 2: "___"}},
63
64
65 {false, "Type bool is not supported by LazyChain"},
66 {0, "Type int is not supported by LazyChain"},
67 }
68
69 for idx, tc := range testCases {
70 t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
71 is := assert.New(t)
72
73 if tc.Panic != "" {
74 is.PanicsWithValue(tc.Panic, func() {
75 LazyChain(tc.In)
76 })
77 return
78 }
79
80 chain := LazyChain(tc.In)
81 collection := chain.(*lazyBuilder).exec()
82
83 is.Equal(collection, tc.In)
84 })
85 }
86 }
87
88 func TestLazyChainWith(t *testing.T) {
89 testCases := []struct {
90 In func() interface{}
91 Panic string
92 }{
93
94 {In: func() interface{} { return []int{0, 1, 2} }},
95 {In: func() interface{} { return []string{"aaa", "bbb", "ccc"} }},
96 {In: func() interface{} { return []interface{}{0, false, "___"} }},
97
98
99 {In: func() interface{} { return map[int]string{0: "aaa", 1: "bbb", 2: "ccc"} }},
100 {In: func() interface{} { return map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"} }},
101 {In: func() interface{} { return map[int]interface{}{0: 0, 1: false, 2: "___"} }},
102
103
104 {
105 In: func() interface{} { return false },
106 Panic: "Type bool is not supported by LazyChainWith generator",
107 },
108 {
109 In: func() interface{} { return 0 },
110 Panic: "Type int is not supported by LazyChainWith generator",
111 },
112 }
113
114 for idx, tc := range testCases {
115 t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
116 is := assert.New(t)
117
118 if tc.Panic != "" {
119 is.PanicsWithValue(tc.Panic, func() {
120 LazyChainWith(tc.In).(*lazyBuilder).exec()
121 })
122 return
123 }
124
125 chain := LazyChainWith(tc.In)
126 collection := chain.(*lazyBuilder).exec()
127
128 is.Equal(collection, tc.In())
129 })
130 }
131 }
132
133 func ExampleChain() {
134 v := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
135 chain := Chain(v)
136 lazy := LazyChain(v)
137
138
139 a := Filter(v, func(x int) bool { return x%2 == 0 })
140 b := Map(a, func(x int) int { return x * 2 })
141 c := Reverse(a)
142 fmt.Printf("funk.Contains(b, 2): %v\n", Contains(b, 2))
143 fmt.Printf("funk.Contains(b, 4): %v\n", Contains(b, 4))
144 fmt.Printf("funk.Sum(b): %v\n", Sum(b))
145 fmt.Printf("funk.Head(b): %v\n", Head(b))
146 fmt.Printf("funk.Head(c): %v\n\n", Head(c))
147
148
149 ca := chain.Filter(func(x int) bool { return x%2 == 0 })
150 cb := ca.Map(func(x int) int { return x * 2 })
151 cc := ca.Reverse()
152 fmt.Printf("chainB.Contains(2): %v\n", cb.Contains(2))
153 fmt.Printf("chainB.Contains(4): %v\n", cb.Contains(4))
154 fmt.Printf("chainB.Sum(): %v\n", cb.Sum())
155 fmt.Printf("chainB.Head(): %v\n", cb.Head())
156 fmt.Printf("chainC.Head(): %v\n\n", cc.Head())
157
158
159 la := lazy.Filter(func(x int) bool { return x%2 == 0 })
160 lb := la.Map(func(x int) int { return x * 2 })
161 lc := la.Reverse()
162 fmt.Printf("lazyChainB.Contains(2): %v\n", lb.Contains(2))
163 fmt.Printf("lazyChainB.Contains(4): %v\n", lb.Contains(4))
164 fmt.Printf("lazyChainB.Sum(): %v\n", lb.Sum())
165 fmt.Printf("lazyChainB.Head(): %v\n", lb.Head())
166 fmt.Printf("lazyChainC.Head(): %v\n", lc.Head())
167 }
168
169 type updatingStruct struct {
170 x []int
171 }
172
173 func (us *updatingStruct) Values() interface{} {
174 return us.x
175 }
176
177 func ExampleLazyChain() {
178 us := updatingStruct{}
179 chain := Chain(us.x).
180 Map(func(x int) float64 { return float64(x) * 2.5 })
181 lazy := LazyChain(us.x).
182 Map(func(x int) float64 { return float64(x) * 2.5 })
183 lazyWith := LazyChainWith(us.Values).
184 Map(func(x int) float64 { return float64(x) * 2.5 })
185
186 fmt.Printf("chain.Sum(): %v\n", chain.Sum())
187 fmt.Printf("lazy.Sum(): %v\n", lazy.Sum())
188 fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum())
189
190 us.x = append(us.x, 2)
191 fmt.Printf("chain.Sum(): %v\n", chain.Sum())
192 fmt.Printf("lazy.Sum(): %v\n", lazy.Sum())
193 fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum())
194
195 us.x = append(us.x, 10)
196 fmt.Printf("chain.Sum(): %v\n", chain.Sum())
197 fmt.Printf("lazy.Sum(): %v\n", lazy.Sum())
198 fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum())
199 }
200
View as plain text