Skip to content

Commit e4cb36b

Browse files
committed
✨ add value checker.
1 parent b65476a commit e4cb36b

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

value_checker.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package workflowmodel
2+
3+
type StringValueChecker interface {
4+
CheckValue(s string) bool
5+
}
6+
7+
type StringEqualValueChecker struct {
8+
ExpectedValue string
9+
}
10+
11+
func (c *StringEqualValueChecker) CheckValue(s string) bool {
12+
return s == c.ExpectedValue
13+
}
14+
15+
type StringInValueChecker struct {
16+
ExpectedValues []string
17+
}
18+
19+
func (c *StringInValueChecker) CheckValue(s string) bool {
20+
for _, v := range c.ExpectedValues {
21+
if s == v {
22+
return true
23+
}
24+
}
25+
return false
26+
}
27+
28+
type NodeStatusValueChecker interface {
29+
CheckValue(status NodeStatus) bool
30+
}
31+
32+
type NodeStatusEqualValueChecker struct {
33+
ExpectedValue NodeStatus
34+
}
35+
36+
func (c *NodeStatusEqualValueChecker) CheckValue(s NodeStatus) bool {
37+
return s == c.ExpectedValue
38+
}
39+
40+
type NodeStatusInValueChecker struct {
41+
ExpectedValues []NodeStatus
42+
}
43+
44+
func (c *NodeStatusInValueChecker) CheckValue(s NodeStatus) bool {
45+
for _, v := range c.ExpectedValues {
46+
if s == v {
47+
return true
48+
}
49+
}
50+
return false
51+
}

value_checker_test.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package workflowmodel_test
2+
3+
import (
4+
"github.com/perillaroc/workflow-model-go"
5+
"testing"
6+
)
7+
8+
func TestStringEqualValueChecker_CheckValue(t *testing.T) {
9+
tests := []struct {
10+
expectedValue string
11+
data string
12+
result bool
13+
}{
14+
{"20190315", "20190315", true},
15+
{"20190315", "20190314", false},
16+
}
17+
18+
for _, test := range tests {
19+
checker := workflowmodel.StringEqualValueChecker{
20+
ExpectedValue: test.expectedValue,
21+
}
22+
if checker.CheckValue(test.data) != test.result {
23+
t.Errorf("StringEqualValueChecker expected value %s, value %s, result %t",
24+
test.expectedValue, test.data, test.result)
25+
}
26+
}
27+
}
28+
29+
func TestStringInValueChecker_CheckValue(t *testing.T) {
30+
tests := []struct {
31+
expectedValues []string
32+
data string
33+
result bool
34+
}{
35+
{[]string{"active", "complete"}, "active", true},
36+
{[]string{"active", "complete"}, "aborted", false},
37+
}
38+
39+
for _, test := range tests {
40+
checker := workflowmodel.StringInValueChecker{
41+
ExpectedValues: test.expectedValues,
42+
}
43+
if checker.CheckValue(test.data) != test.result {
44+
t.Errorf("StringInValueChecker expected value %s, value %s, result %t",
45+
test.expectedValues, test.data, test.result)
46+
}
47+
}
48+
}
49+
50+
func TestNodeStatusEqualValueChecker_CheckValue(t *testing.T) {
51+
tests := []struct {
52+
expectedValue workflowmodel.NodeStatus
53+
data workflowmodel.NodeStatus
54+
result bool
55+
}{
56+
{workflowmodel.Active, workflowmodel.Active, true},
57+
{workflowmodel.Complete, workflowmodel.Aborted, false},
58+
}
59+
60+
for _, test := range tests {
61+
checker := workflowmodel.NodeStatusEqualValueChecker{
62+
ExpectedValue: test.expectedValue,
63+
}
64+
if checker.CheckValue(test.data) != test.result {
65+
t.Errorf("NodeStatusEqualValueChecker expected value %s, value %s, result %t",
66+
test.expectedValue, test.data, test.result)
67+
}
68+
}
69+
}
70+
71+
func TestNodeStatusInValueChecker_CheckValue(t *testing.T) {
72+
tests := []struct {
73+
expectedValues []workflowmodel.NodeStatus
74+
data workflowmodel.NodeStatus
75+
result bool
76+
}{
77+
{
78+
[]workflowmodel.NodeStatus{workflowmodel.Active, workflowmodel.Complete, workflowmodel.Submitted},
79+
workflowmodel.Active,
80+
true,
81+
},
82+
{
83+
[]workflowmodel.NodeStatus{workflowmodel.Complete, workflowmodel.Queued},
84+
workflowmodel.Aborted,
85+
false},
86+
}
87+
88+
for _, test := range tests {
89+
checker := workflowmodel.NodeStatusInValueChecker{
90+
ExpectedValues: test.expectedValues,
91+
}
92+
if checker.CheckValue(test.data) != test.result {
93+
t.Errorf("NodeStatusInValueChecker expected values %s, value %s, result %t",
94+
test.expectedValues, test.data, test.result)
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)