Skip to content

Commit

Permalink
add tests for ff
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Koss committed Nov 6, 2023
1 parent e339a9a commit 6c1f021
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
15 changes: 0 additions & 15 deletions pkg/ff/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,3 @@ func Compose[T1, T2, T3 any](fn1 func(T1) T2, fn2 func(T2) T3) func(T1) T3 {
return fn2(fn1(x))
}
}

// FIXME: signarure about to be changed
func ComposeErr[T1, T2, T3 any](fn1 func(T1) (T2, error), fn2 func(T2) (T3, error), errHandler func(error)) func(T1) T3 {
return func(x T1) T3 {
res, err := fn1(x)
if err != nil {
errHandler(err)
}
res2, err := fn2(res)
if err != nil {
errHandler(err)
}
return res2
}
}
55 changes: 55 additions & 0 deletions pkg/ff/ff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ff

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

func TestCompose(t *testing.T) {
fn1 := func(x int) int {
return x + 1
}

fn2 := func(x int) string {
return strconv.Itoa(x)
}

composedFn := Compose(fn1, fn2)

result := composedFn(5)
require.Equal(t, "6", result, "Unexpected result for Compose")

result = composedFn(10)
require.Equal(t, "11", result, "Unexpected result for Compose")
}

func TestMap(t *testing.T) {
a := []int{1, 2, 3, 4, 5}

fn := func(x int) string {
return strconv.Itoa(x)
}

piper := Map(a, fn).Do()

// Iterate through the values and test the output
expected := []string{"1", "2", "3", "4", "5"}
for i, val := range piper {
require.Equal(t, expected[i], val, "Unexpected result for Map")
}
}

func TestReduce(t *testing.T) {
a := []int{1, 2, 3, 4, 5}

sum := func(result *int, x *int) int {
return *result + *x
}

result := Reduce(a, sum)

expected := 15
require.Equal(t, expected, result, "Unexpected result for Reduce")
}
2 changes: 1 addition & 1 deletion pkg/pipe/prefixpipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Reduce[SrcT any, DstT any](p Piper[SrcT], fn func(*DstT, *SrcT) DstT, initV
default:
res := fn(&init, &data[0])
for i := range data[1:] {
res = fn(&res, &data[i])
res = fn(&res, &data[i+1])
}
return res
}
Expand Down

0 comments on commit 6c1f021

Please sign in to comment.