github.com/jwowillo/pipe@v1.2.0/pipe_test.go (about) 1 package pipe_test 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/jwowillo/pipe" 8 ) 9 10 func TestProcess(t *testing.T) { 11 p := pipe.New(pipe.StageFunc(func(x pipe.Item) pipe.Item { 12 return x.(int) - 1 13 })) 14 actual := pipe.Process(p, 1, 2, 3) 15 sort.Slice(actual, func(i, j int) bool { 16 return actual[i].(int) < actual[j].(int) 17 }) 18 if actual[0] != 0 || actual[1] != 1 || actual[2] != 2 { 19 t.Errorf( 20 "pipe.Process(p, %v) = %v, want %v", 21 []int{1, 2, 3}, actual, []int{0, 1, 2}, 22 ) 23 } 24 } 25 26 func TestPipe(t *testing.T) { 27 add := func(s string) func(pipe.Item) pipe.Item { 28 return func(x pipe.Item) pipe.Item { 29 return x.(string) + s 30 } 31 } 32 p := pipe.New( 33 pipe.StageFunc(add("a")), 34 pipe.StageFunc(add("b")), 35 pipe.StageFunc(add("c")), 36 ) 37 p.Receive("") 38 actual := p.Deliver() 39 if actual != "abc" { 40 t.Errorf("p.Deliver() = %s, want %s", actual, "abc") 41 } 42 } 43 44 func TestDeliverThenReceive(t *testing.T) { 45 p := pipe.New(pipe.StageFunc(func(x pipe.Item) pipe.Item { 46 return x.(int) - 1 47 })) 48 p.Receive(1) 49 actual := p.Deliver() 50 if actual != 0 { 51 t.Errorf("p.Deliver() = %d, want %d", actual, 0) 52 } 53 p.Receive(2) 54 actual = p.Deliver() 55 if actual != 1 { 56 t.Errorf("p.Deliver() = %d, want %d", actual, 1) 57 } 58 } 59 60 func TestNoStage(t *testing.T) { 61 p := pipe.New() 62 p.Receive(1) 63 actual := p.Deliver() 64 if actual != 1 { 65 t.Errorf("p.Deliver() = %d, want %d", actual, 1) 66 } 67 }