-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathfold_test.go
69 lines (61 loc) · 1.24 KB
/
fold_test.go
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
package flow_test
import (
"strconv"
"testing"
"github.com/reugn/go-streams"
ext "github.com/reugn/go-streams/extension"
"github.com/reugn/go-streams/flow"
"github.com/reugn/go-streams/internal/assert"
)
func TestFold(t *testing.T) {
tests := []struct {
name string
foldFlow streams.Flow
ptr bool
}{
{
name: "values",
foldFlow: flow.NewFold(
"",
func(a int, b string) string {
return b + strconv.Itoa(a)
}),
ptr: false,
},
{
name: "pointers",
foldFlow: flow.NewFold(
"",
func(a *int, b string) string {
return b + strconv.Itoa(*a)
}),
ptr: true,
},
}
input := []int{1, 2, 3, 4, 5}
expected := []string{"1", "12", "123", "1234", "12345"}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := make(chan any, 5)
out := make(chan any, 5)
source := ext.NewChanSource(in)
sink := ext.NewChanSink(out)
if tt.ptr {
ingestSlice(ptrSlice(input), in)
close(in)
source.
Via(tt.foldFlow).
To(sink)
} else {
ingestSlice(input, in)
close(in)
source.
Via(tt.foldFlow).
Via(flow.NewPassThrough()). // Via coverage
To(sink)
}
output := readSlice[string](out)
assert.Equal(t, expected, output)
})
}
}