github.com/jwowillo/pipe@v1.2.0/produce_test.go (about) 1 package pipe_test 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/jwowillo/pipe" 8 ) 9 10 func TestProduceAndProcess(t *testing.T) { 11 p := pipe.New(pipe.StageFunc(func(x pipe.Item) pipe.Item { 12 return x.(int) - 1 13 })) 14 list := []pipe.Item{1, 2, 3} 15 i := 0 16 f := pipe.ProducerFunc(func() (pipe.Item, bool) { 17 if i >= len(list) { 18 return nil, false 19 } 20 v := list[i] 21 i++ 22 return v, true 23 }) 24 actual := pipe.ProduceAndProcess(p, f) 25 sort.Slice(actual, func(i, j int) bool { 26 return actual[i].(int) < actual[j].(int) 27 }) 28 if actual[0] != 0 || actual[1] != 1 || actual[2] != 2 { 29 t.Errorf( 30 "pipe.ProduceAndProcess(p, f) = %v, want %v", 31 actual, []int{0, 1, 2}, 32 ) 33 } 34 } 35 36 func TestProduceProcessAndConsume(t *testing.T) { 37 p := pipe.New(pipe.StageFunc(func(x pipe.Item) pipe.Item { 38 return x.(int) - 1 39 })) 40 list := []pipe.Item{1, 2, 3} 41 i := 0 42 pf := pipe.ProducerFunc(func() (pipe.Item, bool) { 43 if i >= len(list) { 44 return nil, false 45 } 46 v := list[i] 47 i++ 48 return v, true 49 }) 50 var actual []pipe.Item 51 cf := pipe.ConsumerFunc(func(x pipe.Item) { 52 actual = append(actual, x) 53 }) 54 pipe.ProduceProcessAndConsume(p, pf, cf) 55 sort.Slice(actual, func(i, j int) bool { 56 return actual[i].(int) < actual[j].(int) 57 }) 58 if actual[0] != 0 || actual[1] != 1 || actual[2] != 2 { 59 t.Errorf( 60 "pipe.ProduceProcessAndConsume(p, pf, cf) = %v, "+ 61 "want %v", 62 actual, []int{0, 1, 2}, 63 ) 64 } 65 }