-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRaiseActionTests.cs
160 lines (148 loc) · 5.94 KB
/
RaiseActionTests.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using DSM.Common;
using DSM.Metadata.Actions;
using DSM.Metadata.States;
using System.Threading.Tasks;
namespace DSM.Tests
{
[TestClass]
public partial class RaiseActionTests : TestBase
{
[TestMethod]
[TestScaffold]
public async Task ConditionalTransition(ScaffoldFactoryDelegate factory, string _)
{
var machine = new StateMachine<(int x, string y)>
{
Id = "raise",
States =
{
new AtomicState<(int x, string y)>
{
Id = "start",
OnEntry = new OnEntryExit<(int x, string y)>
{
Actions =
{
new If<(int x, string y)>
{
ConditionFunction = d => d.x > 10,
Actions =
{
new Raise<(int x, string y)> { Message = "greater" }
},
ElseIfs =
{
new ElseIf<(int x, string y)>
{
ConditionFunction = d => d.x < 10,
Actions =
{
new Raise<(int x, string y)> { Message = "lesser" }
}
}
},
Else = new Else<(int x, string y)>
{
Actions =
{
new Raise<(int x, string y)> { Message = "equal" }
}
}
}
}
},
Transitions =
{
new Transition<(int x, string y)>
{
Message = "greater",
Target = "greater-state"
},
new Transition<(int x, string y)>
{
Message = "lesser",
Target = "lesser-state"
},
new Transition<(int x, string y)>
{
Message = "equal",
Target = "equal-state"
}
}
},
new AtomicState<(int x, string y)>
{
Id = "greater-state",
OnEntry = new OnEntryExit<(int x, string y)>
{
Actions =
{
new Assign<(int x, string y)> { To = d => d.y, Value = "is-greater" }
}
},
Transitions =
{
new Transition<(int x, string y)>
{
Target = "done"
}
}
},
new AtomicState<(int x, string y)>
{
Id = "lesser-state",
OnEntry = new OnEntryExit<(int x, string y)>
{
Actions =
{
new Assign<(int x, string y)> { To = d => d.y, Value = "is-lesser" }
}
},
Transitions =
{
new Transition<(int x, string y)>
{
Target = "done"
}
}
},
new AtomicState<(int x, string y)>
{
Id = "equal-state",
OnEntry = new OnEntryExit<(int x, string y)>
{
Actions =
{
new Assign<(int x, string y)> { To = d => d.y, Value = "is-equal" }
}
},
Transitions =
{
new Transition<(int x, string y)>
{
Target = "done"
}
}
},
new FinalState<(int x, string y)>
{
Id = "done"
}
}
};
var data = (5, string.Empty);
var tuple = factory(machine, null, null);
var context = tuple.Item1;
data = await context.RunAsync(data); // notice we need the return value here... data is ValueTuple (value type)
Assert.AreEqual("is-lesser", data.Item2);
data = (15, string.Empty);
data = await context.RunAsync(data);
Assert.AreEqual("is-greater", data.Item2);
data = (10, string.Empty);
data = await context.RunAsync(data);
Assert.AreEqual("is-equal", data.Item2);
}
}
}