-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_test.go
62 lines (56 loc) · 1.38 KB
/
stage_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
56
57
58
59
60
61
62
package stream
import (
"github.com/chinalhr/go-stream/types"
"github.com/stretchr/testify/assert"
"testing"
)
func TestChainedStage(t *testing.T) {
var terminalStage *chainedStage
var intermediateStageWrap func(stage) stage
actualResult := []int{1, 4, 9, 16, 25}
terminalStageResult := make([]int, 0, 5)
testCases := []func(){
func() {
intermediateStageWrap = func(next stage) stage {
return newDefaultIntermediateStage(terminalStage,
beginFunc(func(size int) {
assert.Equal(t, size, 5)
next.Begin(size)
}),
acceptFunc(func(e types.T) {
assert.NotNil(t, e)
next.Accept(e.(int) * e.(int))
}),
)
}
},
func() {
terminalStage = newDefaultTerminalStage(
beginFunc(func(size int) {
assert.Equal(t, size, 5)
}),
acceptFunc(func(e types.T) {
assert.NotNil(t, e)
terminalStageResult = append(terminalStageResult, e.(int))
}),
endFunc(func() {
assert.Equal(t, terminalStageResult, actualResult)
}),
)
},
func() {
source := buildSliceIterator(1, 2, 3, 4, 5)
wrapStage := intermediateStageWrap(terminalStage)
wrapStage.Begin(source.GetSize())
for source.HasNext() && !wrapStage.CancellationRequested() {
wrapStage.Accept(source.Next())
}
wrapStage.End()
},
}
for _, testCase := range testCases {
t.Run("testCase", func(t *testing.T) {
testCase()
})
}
}