-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransition_test.go
103 lines (94 loc) · 2.35 KB
/
transition_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package stateswitch
import (
"fmt"
"github.com/pkg/errors"
. "github.com/onsi/ginkgo"
"github.com/onsi/gomega"
)
var _ = Describe("test_transition_find", func() {
transitions := TransitionRules{
{TransitionType: "a"},
{TransitionType: "b"},
{TransitionType: "a"},
{TransitionType: "c"},
}
tests := []struct {
name string
transitionType TransitionType
expectedResults int
}{
{transitionType: "a", expectedResults: 2},
{transitionType: "b", expectedResults: 1},
{transitionType: "c", expectedResults: 1},
{transitionType: "d", expectedResults: 0},
}
for i := range tests {
t := tests[i]
It(fmt.Sprintf("find %s expected %d", t.transitionType, t.expectedResults), func() {
found := transitions.Find(t.transitionType)
gomega.Expect(len(found)).Should(gomega.Equal(t.expectedResults))
})
}
})
var _ = Describe("IsAllowedToRun", func() {
var srcStateA State = "srcStateA"
var srcStateB State = "srcStateB"
transition := TransitionRule{
SourceStates: []State{srcStateA, srcStateB},
Condition: nil,
}
tests := []struct {
name string
state State
condition Condition
allow bool
fail bool
}{
{
name: "no condition",
state: srcStateB,
condition: nil,
allow: true,
fail: false,
},
{
name: "invalid source state",
state: "some invalid source state",
condition: nil,
allow: false,
fail: false,
},
{
name: "condition allow",
state: srcStateA,
condition: func(stateSwitch StateSwitch, args TransitionArgs) (bool, error) { return true, nil },
allow: true,
fail: false,
},
{
name: "condition don't allow",
state: srcStateA,
condition: func(stateSwitch StateSwitch, args TransitionArgs) (bool, error) { return false, nil },
allow: false,
fail: false,
},
{
name: "condition error",
state: srcStateA,
condition: func(stateSwitch StateSwitch, args TransitionArgs) (bool, error) {
return false, errors.Errorf("error")
},
allow: false,
fail: true,
},
}
for i := range tests {
t := tests[i]
It(t.name, func() {
transition.Condition = t.condition
allow, err := transition.IsAllowedToRun(&swState{state: t.state}, nil)
gomega.Expect(allow).To(gomega.Equal(t.allow))
gomega.Expect(err == nil).Should(gomega.Equal(!t.fail))
})
}
})