-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter_chan_test.go
55 lines (43 loc) · 983 Bytes
/
filter_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
package pipe
import (
"fmt"
"testing"
)
func TestFilterChan(t *testing.T) {
even := func(item int) bool {
return (item % 2) == 0
}
in := make(chan int)
out := FilterChan(even, in).(chan int)
go func() {
in <- 7
in <- 4
in <- 5
in <- 2
close(in)
}()
if result := <-out; result != 4 {
t.Fatal("FilterChan(even, in) received 7,4,5,2, but output ", result)
}
if result := <-out; result != 2 {
t.Fatal("FilterChan(even, in) received 7,4,5,2, but output ", result)
}
if _, ok := <-out; ok {
t.Fatal("FilterChan(even, in) wasn't closed after in was closed")
}
}
func TestFilterChanTypeCoercion(t *testing.T) {
long_enough := func(item fmt.Stringer) bool {
return len(item.String()) > 1
}
in := make(chan testStringer)
out := FilterChan(long_enough, in).(chan testStringer)
go func() {
in <- 7
in <- 42
}()
if result := <-out; result != 42 {
t.Fatal("FilterChan(long_enough, in) received 7 and 42 but output ", out)
}
close(in)
}