-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_action_test.go
More file actions
79 lines (69 loc) · 1.76 KB
/
Copy pathbranch_action_test.go
File metadata and controls
79 lines (69 loc) · 1.76 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package chain
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAsBranchAction(t *testing.T) {
t.Run("wraps base action", func(t *testing.T) {
baseAction := NewSimpleAction(
"double",
func(_ context.Context, input int) (int, error) {
return input * 2, nil
},
)
action := AsBranchAction(
baseAction,
func(_ context.Context, output int) (string, error) {
if output%4 == 0 {
return "evenlyDivided", nil
}
return "remaining", nil
},
"evenlyDivided",
"remaining",
)
output, err := action.Run(context.Background(), 2)
direction, directionErr := action.NextDirection(context.Background(), output)
assert.NoError(t, err)
assert.NoError(t, directionErr)
assert.Equal(t, "double", action.Name())
assert.Equal(t, 4, output)
assert.Equal(t, "evenlyDivided", direction)
assert.Equal(t, []string{"evenlyDivided", "remaining"}, action.Directions())
})
t.Run("directions returns copy", func(t *testing.T) {
baseAction := NewSimpleAction(
"pass",
func(_ context.Context, input int) (int, error) {
return input, nil
},
)
action := AsBranchAction(
baseAction,
func(_ context.Context, _ int) (string, error) {
return "left", nil
},
"left",
"right",
)
directions := action.Directions()
directions[0] = "changed"
assert.Equal(t, []string{"left", "right"}, action.Directions())
})
t.Run("allows built-in directions without custom directions", func(t *testing.T) {
baseAction := NewSimpleAction(
"pass",
func(_ context.Context, input int) (int, error) {
return input, nil
},
)
action := AsBranchAction(
baseAction,
func(_ context.Context, _ int) (string, error) {
return Success, nil
},
)
assert.Empty(t, action.Directions())
})
}