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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package funk
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestChain(t *testing.T) {
testCases := []struct {
In interface{}
Panic string
}{
// Check with array types
{In: []int{0, 1, 2}},
{In: []string{"aaa", "bbb", "ccc"}},
{In: []interface{}{0, false, "___"}},
// Check with map types
{In: map[int]string{0: "aaa", 1: "bbb", 2: "ccc"}},
{In: map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"}},
{In: map[int]interface{}{0: 0, 1: false, 2: "___"}},
// Check with invalid types
{false, "Type bool is not supported by Chain"},
{0, "Type int is not supported by Chain"},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)
if tc.Panic != "" {
is.PanicsWithValue(tc.Panic, func() {
Chain(tc.In)
})
return
}
chain := Chain(tc.In)
collection := chain.(*chainBuilder).collection
is.Equal(collection, tc.In)
})
}
}
func TestLazyChain(t *testing.T) {
testCases := []struct {
In interface{}
Panic string
}{
// Check with array types
{In: []int{0, 1, 2}},
{In: []string{"aaa", "bbb", "ccc"}},
{In: []interface{}{0, false, "___"}},
// Check with map types
{In: map[int]string{0: "aaa", 1: "bbb", 2: "ccc"}},
{In: map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"}},
{In: map[int]interface{}{0: 0, 1: false, 2: "___"}},
// Check with invalid types
{false, "Type bool is not supported by LazyChain"},
{0, "Type int is not supported by LazyChain"},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)
if tc.Panic != "" {
is.PanicsWithValue(tc.Panic, func() {
LazyChain(tc.In)
})
return
}
chain := LazyChain(tc.In)
collection := chain.(*lazyBuilder).exec()
is.Equal(collection, tc.In)
})
}
}
func TestLazyChainWith(t *testing.T) {
testCases := []struct {
In func() interface{}
Panic string
}{
// Check with array types
{In: func() interface{} { return []int{0, 1, 2} }},
{In: func() interface{} { return []string{"aaa", "bbb", "ccc"} }},
{In: func() interface{} { return []interface{}{0, false, "___"} }},
// Check with map types
{In: func() interface{} { return map[int]string{0: "aaa", 1: "bbb", 2: "ccc"} }},
{In: func() interface{} { return map[string]string{"0": "aaa", "1": "bbb", "2": "ccc"} }},
{In: func() interface{} { return map[int]interface{}{0: 0, 1: false, 2: "___"} }},
// Check with invalid types
{
In: func() interface{} { return false },
Panic: "Type bool is not supported by LazyChainWith generator",
},
{
In: func() interface{} { return 0 },
Panic: "Type int is not supported by LazyChainWith generator",
},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("test case #%d", idx+1), func(t *testing.T) {
is := assert.New(t)
if tc.Panic != "" {
is.PanicsWithValue(tc.Panic, func() {
LazyChainWith(tc.In).(*lazyBuilder).exec()
})
return
}
chain := LazyChainWith(tc.In)
collection := chain.(*lazyBuilder).exec()
is.Equal(collection, tc.In())
})
}
}
func ExampleChain() {
v := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
chain := Chain(v)
lazy := LazyChain(v)
// Without builder
a := Filter(v, func(x int) bool { return x%2 == 0 })
b := Map(a, func(x int) int { return x * 2 })
c := Reverse(a)
fmt.Printf("funk.Contains(b, 2): %v\n", Contains(b, 2)) // false
fmt.Printf("funk.Contains(b, 4): %v\n", Contains(b, 4)) // true
fmt.Printf("funk.Sum(b): %v\n", Sum(b)) // 40
fmt.Printf("funk.Head(b): %v\n", Head(b)) // 4
fmt.Printf("funk.Head(c): %v\n\n", Head(c)) // 8
// With simple chain builder
ca := chain.Filter(func(x int) bool { return x%2 == 0 })
cb := ca.Map(func(x int) int { return x * 2 })
cc := ca.Reverse()
fmt.Printf("chainB.Contains(2): %v\n", cb.Contains(2)) // false
fmt.Printf("chainB.Contains(4): %v\n", cb.Contains(4)) // true
fmt.Printf("chainB.Sum(): %v\n", cb.Sum()) // 40
fmt.Printf("chainB.Head(): %v\n", cb.Head()) // 4
fmt.Printf("chainC.Head(): %v\n\n", cc.Head()) // 8
// With lazy chain builder
la := lazy.Filter(func(x int) bool { return x%2 == 0 })
lb := la.Map(func(x int) int { return x * 2 })
lc := la.Reverse()
fmt.Printf("lazyChainB.Contains(2): %v\n", lb.Contains(2)) // false
fmt.Printf("lazyChainB.Contains(4): %v\n", lb.Contains(4)) // true
fmt.Printf("lazyChainB.Sum(): %v\n", lb.Sum()) // 40
fmt.Printf("lazyChainB.Head(): %v\n", lb.Head()) // 4
fmt.Printf("lazyChainC.Head(): %v\n", lc.Head()) // 8
}
type updatingStruct struct {
x []int
}
func (us *updatingStruct) Values() interface{} {
return us.x
}
func ExampleLazyChain() {
us := updatingStruct{}
chain := Chain(us.x).
Map(func(x int) float64 { return float64(x) * 2.5 })
lazy := LazyChain(us.x).
Map(func(x int) float64 { return float64(x) * 2.5 })
lazyWith := LazyChainWith(us.Values).
Map(func(x int) float64 { return float64(x) * 2.5 })
fmt.Printf("chain.Sum(): %v\n", chain.Sum()) // 0
fmt.Printf("lazy.Sum(): %v\n", lazy.Sum()) // 0
fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum()) // 0
us.x = append(us.x, 2)
fmt.Printf("chain.Sum(): %v\n", chain.Sum()) // 0
fmt.Printf("lazy.Sum(): %v\n", lazy.Sum()) // 0
fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum()) // 5
us.x = append(us.x, 10)
fmt.Printf("chain.Sum(): %v\n", chain.Sum()) // 0
fmt.Printf("lazy.Sum(): %v\n", lazy.Sum()) // 0
fmt.Printf("lazyWith.Sum(): %v\n\n", lazyWith.Sum()) // 30
}
|