github.com/jwowillo/pipe@v1.2.0/consume_test.go (about)

     1  package pipe_test
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/jwowillo/pipe"
     8  )
     9  
    10  func TestProcessAndConsume(t *testing.T) {
    11  	p := pipe.New(pipe.StageFunc(func(x pipe.Item) pipe.Item {
    12  		return x.(int) - 1
    13  	}))
    14  	var actual []pipe.Item
    15  	f := pipe.ConsumerFunc(func(x pipe.Item) {
    16  		actual = append(actual, x)
    17  	})
    18  	pipe.ProcessAndConsume(p, f, 1, 2, 3)
    19  	sort.Slice(actual, func(i, j int) bool {
    20  		return actual[i].(int) < actual[j].(int)
    21  	})
    22  	if actual[0] != 0 || actual[1] != 1 || actual[2] != 2 {
    23  		t.Errorf(
    24  			"pipe.ProcessAndConsume(p, f, %v) = %v, want %v",
    25  			[]int{1, 2, 3}, actual, []int{0, 1, 2},
    26  		)
    27  	}
    28  }