-
Notifications
You must be signed in to change notification settings - Fork 2
/
take_while_chan_test.go
58 lines (46 loc) · 1007 Bytes
/
take_while_chan_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
package pipe
import (
"fmt"
"testing"
)
func TestTakeWhileChan(t *testing.T) {
take := true
in := make(chan int, 5)
out := TakeWhileChan(func(item int) bool {
return take
}, in).(chan int)
in <- 7
in <- 4
take = false
in <- 5
in <- 6
<-out
<-out
if _, ok := <-out; ok {
t.Fatal("takewhile pipe should have closed the channel after turning it off")
}
close(in)
}
func TestTakeWhileChanTypeCoercion(t *testing.T) {
strLenLessThan := func(length int) func(fmt.Stringer) bool {
return func(x fmt.Stringer) bool {
return len(x.String()) < length
}
}
in := make(chan testStringer, 5)
out := TakeWhileChan(strLenLessThan(2), in).(chan testStringer)
in <- 8
in <- 9
in <- 10
in <- 9
if result := <-out; result != 8 {
t.Fatal("Expected:", 8, "\nGot:", result)
}
if result := <-out; result != 9 {
t.Fatal("Expected:", 9, "\nGot:", result)
}
if _, ok := <-out; ok {
t.Fatal("takewhile pipe should have closed the channel after turning it off")
}
close(in)
}