-
Notifications
You must be signed in to change notification settings - Fork 3
/
TestMain.cs
82 lines (69 loc) · 2.29 KB
/
TestMain.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
using System;
using SimpleStateMachine;
public class MachineState : StateMachine {
private void ComplexHandler(string value) {
Console.WriteLine("Left Paused State");
}
private void Initialize() {
States = new State[] {
new State("PAUSE", "Paused", new[] { "RUN", "STOP" }) {
OnEnter = (enter => { Console.WriteLine("Entered Paused State");}),
OnExit = ComplexHandler
},
new State("RUN", "Running", new[] { "PAUSE" }) {
OnEnter = (enter => { Console.WriteLine("Entered Running State"); }),
OnExit = (exit => { Console.WriteLine("Left Running State"); })
},
//A final state
new State("STOP", "Finished", null) {
OnEnter = (enter => { Console.WriteLine("Entered Final State"); })
}
};
}
public MachineState() {
Initialize();
InitialState = "PAUSE"; //This starts the workflow
}
public MachineState(string currentState) {
Initialize();
this.currentState = currentState; //This continues a preexisting workflow
}
}
public class Test {
public static void Main(string[] args) {
MachineState csm = new MachineState();
csm.State = "RUN";
csm.State = "RUN"; //This should have no effect
try {
csm.State = "STOP"; //STOP isn't a valid transition
}
catch (ArgumentException) {
Console.WriteLine("Could not transition to invalid state!");
}
csm.State = "PAUSE";
csm.State = "STOP";
Console.WriteLine();
Console.WriteLine("Test continuation");
Console.WriteLine();
csm = new MachineState("RUN");
csm.State = "PAUSE";
csm.State = "STOP";
Console.ReadLine();
}
//Should Yield:
//Entered Paused State
//Left Paused State
//Entered Running State
//Could not transition to invalid state!
//Left Running State
//Entered Paused State
//Left Paused State
//Entered Final State
//
//Test continuation
//
//Left Running State
//Entered Paused State
//Left Paused State
//Entered Final State
}