-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjoin_test.go
52 lines (48 loc) · 1.29 KB
/
join_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
package pipeline
import (
"context"
"fmt"
"strconv"
"testing"
)
func TestJoin(t *testing.T) {
t.Parallel()
// Emit 10 numbers
want := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
ins := Emit(want...)
// Join two steps, one that converts the number to a string, the other that converts it back to a number
join := Join(NewProcessor(func(_ context.Context, i int) (string, error) {
return strconv.Itoa(i), nil
}, nil), NewProcessor(func(_ context.Context, i string) (int, error) {
return strconv.Atoi(i)
}, nil))
// Compare inputs and outputs
var idx int
for got := range Process(context.Background(), join, ins) {
if want[idx] != got {
t.Fatalf("[%d] = %d, want = %d", idx, got, want[idx])
}
idx++
}
}
func JoinExample() {
// Emit 10 numbers
inputs := Emit(0, 1, 2, 3, 4, 5)
// Join two steps, one that converts the number to a string, the other that converts it back to a number
convertToStringThenBackToInt := Process(context.Background(), Join(NewProcessor(func(_ context.Context, i int) (string, error) {
return strconv.Itoa(i), nil
}, nil), NewProcessor(func(_ context.Context, i string) (int, error) {
return strconv.Atoi(i)
}, nil)), inputs)
// Print the output
for o := range convertToStringThenBackToInt {
fmt.Println(o)
}
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
}