underscore/chain_test.go
Ruidy 2798562b46
Enable Chaining (#6)
* feat: chain filter map reduce simple Type

* docs: chain documentation

Co-authored-by: Ruidy <rnemausat@newstore.com>
2021-12-31 18:21:00 +01:00

33 lines
652 B
Go

package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestChain(t *testing.T) {
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
isEven := func(n int) bool { return n%2 == 0 }
toSquare := func(n int) int { return n * n }
sum := func(n, acc int) int { return n + acc }
want := []int{2, 4, 6, 8}
assert.Equal(t, want, u.NewChain(nums).
Filter(isEven).
Value)
want = []int{4, 16, 36, 64}
assert.Equal(t, want, u.NewChain(nums).
Filter(isEven).
Map(toSquare).
Value)
w := 120
assert.Equal(t, w, u.NewChain(nums).
Filter(isEven).
Map(toSquare).
Reduce(sum, 0))
}