forked from SimpleStateMachine/SimpleStateMachineLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateTests.cs
More file actions
228 lines (198 loc) · 7.67 KB
/
StateTests.cs
File metadata and controls
228 lines (198 loc) · 7.67 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SimpleStateMachineLibrary;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Tests
{
[TestClass]
public class StateTests
{
[TestMethod]
[TestCategory("State")]
public void AllTestsWithLogger()
{
ForTest.stateMachine = new StateMachine(ForTest.GetConsoleLogger());
AllTests();
}
[TestMethod]
[TestCategory("State")]
public void AllTestsWithoutLogger()
{
ForTest.stateMachine = new StateMachine();
AllTests();
}
public void AllTests()
{
AddStateTest();
GetStateTest();
DeleteStateTest();
EntryExitStateTest();
}
[TestMethod]
[TestCategory("State")]
public void AddStateTest()
{
StateMachine stateMachine = ForTest.stateMachine;
string stateName = "State1";
Assert.IsFalse(stateMachine.StateExists(stateName));
State state1 = stateMachine.AddState(stateName);
Assert.IsNotNull(state1);
Assert.IsTrue(stateMachine.StateExists(stateName));
Assert.ThrowsException<ArgumentException>(() =>
{
state1 = stateMachine.AddState(stateName);
});
state1 = stateMachine.TryAddState(out bool result, stateName);
Assert.IsNull(state1);
Assert.IsFalse(result);
stateMachine.DeleteState(stateName);
}
[TestMethod]
[TestCategory("State")]
public void GetStateTest()
{
StateMachine stateMachine = ForTest.stateMachine;
string stateName = "State1";
State state1;
bool result;
Assert.IsFalse(stateMachine.StateExists(stateName));
//get state
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.GetState(stateName);
Assert.IsNotNull(state1);
Assert.IsTrue(stateMachine.StateExists(stateName));
stateMachine.DeleteState(stateName);
Assert.ThrowsException<KeyNotFoundException>(() =>
{
state1 = stateMachine.GetState(stateName);
});
//try get state
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.TryGetState(stateName, out result);
Assert.IsNotNull(state1);
Assert.IsTrue(result);
Assert.IsTrue(stateMachine.StateExists(stateName));
stateMachine.DeleteState(stateName);
state1 = stateMachine.TryGetState(stateName, out result);
Assert.IsNull(state1);
Assert.IsFalse(result);
}
[TestMethod]
[TestCategory("State")]
public void DeleteStateTest()
{
StateMachine stateMachine = ForTest.stateMachine;
string stateName = "State1";
State state1;
bool result;
//delete state by name
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.DeleteState(stateName);
Assert.IsNotNull(state1);
Assert.IsFalse(stateMachine.StateExists(stateName));
Assert.ThrowsException<KeyNotFoundException>(() =>
{
state1 = stateMachine.DeleteState(stateName);
});
//delete state by object
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.DeleteState(state1);
Assert.IsNotNull(state1);
Assert.IsFalse(stateMachine.StateExists(stateName));
Assert.ThrowsException<KeyNotFoundException>(() =>
{
state1 = stateMachine.DeleteState(state1);
});
//try delete state by name
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.TryDeleteState(stateName, out result);
Assert.IsNotNull(state1);
Assert.IsTrue(result);
state1 = stateMachine.TryDeleteState(stateName, out result);
Assert.IsNull(state1);
Assert.IsFalse(result);
//try delete state by object
state1 = stateMachine.AddState(stateName);
state1 = stateMachine.TryDeleteState(state1, out result);
Assert.IsNotNull(state1);
Assert.IsTrue(result);
state1 = stateMachine.TryDeleteState(state1, out result);
Assert.IsNull(state1);
Assert.IsFalse(result);
//delete state from object
state1 = stateMachine.AddState(stateName);
state1 = state1.Delete();
Assert.IsNotNull(state1);
Assert.IsFalse(stateMachine.StateExists(stateName));
Assert.ThrowsException<KeyNotFoundException>(() =>
{
state1 = state1.Delete();
});
//try delete state from object
state1 = stateMachine.AddState(stateName);
state1 = state1.TryDelete(out result);
Assert.IsNotNull(state1);
Assert.IsTrue(result);
Assert.IsFalse(stateMachine.StateExists(stateName));
state1 = state1.TryDelete(out result);
Assert.IsNull(state1);
Assert.IsFalse(result);
}
[TestMethod]
[TestCategory("State")]
public void EntryExitStateTest()
{
StateMachine stateMachine = ForTest.stateMachine;
int eventCont;
string stateName1 = "State1";
State state1;
state1 = stateMachine.AddState(stateName1);
eventCont = 0;
state1.SetAsStartState();
stateMachine.Start();
Assert.AreEqual(eventCont, 0);
stateMachine.DeleteState(stateName1);
state1 = stateMachine.AddState(stateName1);
eventCont = 0;
state1.OnEntry(MethodOnEntry);
state1.OnExit(MethodOnExit);
state1.SetAsStartState();
stateMachine.Start();
Assert.AreEqual(eventCont, 2);
stateMachine.DeleteState(stateName1);
state1 = stateMachine.AddState(stateName1, MethodOnEntry, MethodOnExit);
eventCont = 0;
state1.SetAsStartState();
stateMachine.Start();
Assert.AreEqual(eventCont, 2);
stateMachine.DeleteState(stateName1);
state1 = stateMachine.AddState(stateName1).OnEntry(MethodOnEntry).OnExit(MethodOnExit);
eventCont = 0;
state1.SetAsStartState();
stateMachine.Start();
Assert.AreEqual(eventCont, 2);
stateMachine.DeleteState(stateName1);
state1 = stateMachine.AddState(stateName1, MethodOnEntry, MethodOnExit).OnEntry(MethodOnEntry).OnExit(MethodOnExit).OnEntry(MethodOnEntry).OnExit(MethodOnExit);
eventCont = 0;
state1.OnEntry(MethodOnEntry);
state1.OnEntry(MethodOnEntry);
state1.OnExit(MethodOnExit);
state1.OnExit(MethodOnExit);
state1.SetAsStartState();
stateMachine.Start();
Assert.AreEqual(eventCont, 10);
stateMachine.DeleteState(stateName1);
void MethodOnEntry(State state, Dictionary<string, object> parameters)
{
Assert.AreEqual(state.Name, stateName1);
eventCont++;
}
void MethodOnExit(State state, Dictionary<string, object> parameters)
{
Assert.AreEqual(state.Name, stateName1);
eventCont++;
}
}
}
}