-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework_test.go
More file actions
77 lines (64 loc) · 1.81 KB
/
Copy pathframework_test.go
File metadata and controls
77 lines (64 loc) · 1.81 KB
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
package gloo
import (
"context"
"strings"
"testing"
"github.com/destel/rill"
)
// The core types are pure interfaces; verify that minimal hand-rolled
// implementations (no patterns, no helpers) satisfy them and compose.
type countingSource struct{ n int }
func (s *countingSource) Stream(ctx context.Context) Stream[int] {
return Generate(ctx, func(_ context.Context, send func(int) bool, _ func(error)) {
for i := 1; i <= s.n; i++ {
if !send(i) {
return
}
}
})
}
type doubler struct{}
func (doubler) Execute(_ context.Context, in Stream[int]) Stream[int] {
return WrapFrom(rill.OrderedMap(in.Chan(), 1, func(v int) (int, error) { return v * 2, nil }), in)
}
type summer struct{}
func (summer) Consume(_ context.Context, in Stream[int]) (int, error) {
total := 0
err := rill.ForEach(in.Chan(), 1, func(v int) error {
total += v
return nil
})
return total, err
}
func TestCustomSourceCommandSinkRoundtrip(t *testing.T) {
ctx := context.Background()
var src Source[int] = &countingSource{n: 4}
var cmd Command[int, int] = doubler{}
var sink Sink[int, int] = summer{}
got, err := sink.Consume(ctx, cmd.Execute(ctx, src.Stream(ctx)))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := 2 + 4 + 6 + 8; got != want {
t.Errorf("sum = %d, want %d", got, want)
}
}
func TestStreamWrapsChannelOfTry(t *testing.T) {
ch := make(chan rill.Try[string], 2)
ch <- rill.Try[string]{Value: "hello"}
ch <- rill.Try[string]{Value: "world"}
close(ch)
s := Wrap(ch)
var got []string
for item := range s.Chan() {
if item.Error != nil {
t.Fatalf("unexpected error item: %v", item.Error)
}
got = append(got, item.Value)
}
if strings.Join(got, " ") != "hello world" {
t.Errorf("got %v", got)
}
// Discard must be safe (a no-op) even after the stream is fully drained.
s.Discard()
}