-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_test.go
More file actions
155 lines (134 loc) · 3.74 KB
/
lib_test.go
File metadata and controls
155 lines (134 loc) · 3.74 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
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
package streaming
import (
"context"
"errors"
"os/exec"
"strings"
"testing"
"time"
)
func TestStreaming(t *testing.T) {
type Test struct {
want []int
stream func() Stream[Void, Void, []int]
}
tests := map[string]Test{
"empty": {want: []int{}, stream: func() Stream[Void, Void, []int] {
s1 := SourceSlice([]int{})
s2 := Connect(s1, SinkSlice[int]())
return s2
}},
"simple": {want: []int{1, 2, 3}, stream: func() Stream[Void, Void, []int] {
s1 := SourceSlice([]int{1, 2, 3})
s2 := Connect(s1, SinkSlice[int]())
return s2
}},
"take less": {want: []int{1}, stream: func() Stream[Void, Void, []int] {
s1 := SourceSlice([]int{1, 2})
s2 := Connect(s1, Take[int](1))
s3 := Connect(s2, SinkSlice[int]())
return s3
}},
"take more": {want: []int{1, 2}, stream: func() Stream[Void, Void, []int] {
s1 := SourceSlice([]int{1, 2})
s2 := Connect(s1, Take[int](3))
s3 := Connect(s2, SinkSlice[int]())
return s3
}},
"leftovers": {want: []int{}, stream: func() Stream[Void, Void, []int] {
s1 := SourceSlice([]int{1, 2})
s2 := Connect(s1, SinkNull[int]([]int{}))
return s2
}},
}
for _, test := range tests {
r := Run(context.Background(), test.stream())
if r.Error != nil {
t.Fatalf("error %s", *r.Error)
}
assertEqual(t, test.want, *r.Value)
}
}
func TestError(t *testing.T) {
fooError := errors.New("foo")
s1 := Stream[Void, Void, Void](func(p Env[Void, Void]) Result[Void] {
return Error[Void](fooError)
})
s2 := Connect(s1, SinkNull[Void, Void](nil))
r := Run(context.Background(), s2)
if r.Error == nil {
t.Fatalf("expected an error")
}
if *r.Error != fooError {
t.Fatalf("expected foo error")
}
}
func TestExit(t *testing.T) {
done := make(chan Unit)
s1 := Stream[Void, int, Unit](func(env Env[Void, int]) Result[Unit] {
defer func() {
done <- unitValue
}()
if !env.Send(0) {
return Value(unitValue)
}
return Value(unitValue)
})
s2 := Connect(s1, Stream[int, Void, Unit](func(env Env[int, Void]) Result[Unit] {
if env.Recv() == nil {
t.Fatalf("expected to receive a value")
}
return Value(unitValue)
}))
Run(context.Background(), s2)
select {
case <-done:
case <-time.After(time.Second):
t.Fatalf("timed out")
}
}
func TestExec(t *testing.T) {
s1 := SourceExec(func(ctx context.Context) *exec.Cmd { return exec.CommandContext(ctx, "echo", "-n", "a b c") }, func(cmd *exec.Cmd) error { return cmd.Wait() })
s2 := Connect(s1, FuncToStreamPure(func(bs []byte) string { return string(bs) }))
s3 := Connect(s2, SinkString())
r := Run(context.Background(), s3)
if r.Error != nil {
t.Fatalf("error %s", *r.Error)
}
if *r.Value != "a b c" {
t.Fatalf("expected \"a b c\", got %q", *r.Value)
}
}
func TestSplit(t *testing.T) {
tests := map[string]string{
"empty": "",
"1 chunk": "a",
"2 chunks": "a|b",
"delimiter alone": "|",
"delimiter then chunk": "|a",
"chunk then delimiter": "a|",
}
for _, test := range tests {
s1 := SourceSlice([]byte(test))
s2 := Connect(s1, SplitBy(func(i byte) bool { return i == '|' }))
s3 := Connect(s2, FuncToStreamCtx(func(ctx context.Context, s Stream[Void, byte, Unit]) Result[string] {
return MapValue(func(bs []byte) string { return string(bs) }, Run(ctx, Connect(s, SinkSlice[byte]())))
}))
s4 := Connect(s3, SinkSlice[string]())
r := Run(context.Background(), s4)
if r.Error != nil {
t.Fatalf("error %s", *r.Error)
}
assertEqual(t, strings.Split(test, "|"), *r.Value)
}
}
func assertEqual[T comparable](t *testing.T, want []T, got []T) {
if len(want) != len(got) {
t.Fatalf("bad lengths, want %d got %d", len(want), len(got))
}
for i := range want {
if want[i] != got[i] {
t.Fatalf("bad ix %d want %v got %v", i, want[i], got[i])
}
}
}