forked from Inspiaaa/UnityHFSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecorder.cs
176 lines (141 loc) · 6.19 KB
/
Recorder.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using NUnit.Framework;
using System.Collections.Generic;
using System.Text;
using FSM;
namespace FSM.Tests
{
public class Recorder<TStateId> {
private enum StateAction {
ENTER, LOGIC, EXIT
}
private enum TransitionAction {
ENTER, SHOULD_TRANSITION
}
private abstract class Event {
public abstract bool Equals(Event other);
}
private class StateEvent : Event {
public TStateId state;
public StateAction action;
public StateEvent(TStateId state, StateAction action) {
this.state = state;
this.action = action;
}
public override bool Equals(Event other) {
if (! (other is StateEvent)) {
return false;
}
StateEvent otherStateEvent = (StateEvent) other;
return state.Equals(otherStateEvent.state) && action == otherStateEvent.action;
}
public override string ToString() => $"({state}, {action})";
}
private class TransitionEvent : Event {
public TStateId from, to;
public TransitionAction action;
public TransitionEvent(TStateId from, TStateId to, TransitionAction action) {
this.from = from;
this.to = to;
this.action = action;
}
public override bool Equals(Event other) {
if (! (other is TransitionEvent)) {
return false;
}
TransitionEvent otherTransitionEvent = (TransitionEvent) other;
return from.Equals(otherTransitionEvent.from)
&& to.Equals(otherTransitionEvent.to)
&& action == otherTransitionEvent.action;
}
public override string ToString() => $"({from}->{to}, {action})";
}
public class RecorderQuery {
private Recorder<TStateId> recorder;
public RecorderQuery(Recorder<TStateId> recorder) {
this.recorder = recorder;
}
private void CheckNext(Event expectedEvent) {
if (recorder.recordedEvents.Count == 0) {
Assert.Fail($"No recorded steps left. {expectedEvent} has not happened yet.");
}
Event nextEvent = recorder.recordedEvents.Dequeue();
bool doesNextEventMatch = expectedEvent.Equals(nextEvent);
if (! doesNextEventMatch) {
Assert.Fail(
$"Next event {nextEvent} does not match the expected event {expectedEvent}.\n"
+ "Remaining steps: " + recorder.CreateTraceback());
}
}
public RecorderQuery Enter(TStateId stateName) {
CheckNext(new StateEvent(stateName, StateAction.ENTER));
return this;
}
public RecorderQuery Logic(TStateId stateName) {
CheckNext(new StateEvent(stateName, StateAction.LOGIC));
return this;
}
public RecorderQuery Exit(TStateId stateName) {
CheckNext(new StateEvent(stateName, StateAction.EXIT));
return this;
}
public RecorderQuery TransitionEnter(TStateId from, TStateId to) {
CheckNext(new TransitionEvent(from, to, TransitionAction.ENTER));
return this;
}
public RecorderQuery ShouldTransition(TStateId from, TStateId to) {
CheckNext(new TransitionEvent(from, to, TransitionAction.SHOULD_TRANSITION));
return this;
}
public void All() {
if (recorder.recordedEvents.Count != 0) {
Assert.Fail($"Too many events happened. Remaining steps: " + recorder.CreateTraceback());
}
}
public void Empty() {
if (recorder.recordedEvents.Count != 0) {
Assert.Fail("Expected nothing to happen. Recorded steps: " + recorder.CreateTraceback());
}
}
}
private Queue<Event> recordedEvents;
private StateWrapper<TStateId, string> tracker;
// Fluent interface for checking the validity of the recorded events.
public RecorderQuery Expect => new RecorderQuery(this);
// Creates a new StateBase whose OnEnter / OnLogic / OnExit events are tracked.
public StateBase<TStateId> TrackedState => Track(new StateBase<TStateId>(false));
public Recorder() {
recordedEvents = new Queue<Event>();
tracker = new StateWrapper<TStateId, string>(
beforeOnEnter: s => RecordEnter(s.name),
beforeOnLogic: s => RecordLogic(s.name),
beforeOnExit: s => RecordExit(s.name)
);
}
public void RecordEnter(TStateId state)
=> recordedEvents.Enqueue(new StateEvent(state, StateAction.ENTER));
public void RecordLogic(TStateId state)
=> recordedEvents.Enqueue(new StateEvent(state, StateAction.LOGIC));
public void RecordExit(TStateId state)
=> recordedEvents.Enqueue(new StateEvent(state, StateAction.EXIT));
public void RecordTransitionEnter(TStateId from, TStateId to)
=> recordedEvents.Enqueue(new TransitionEvent(from, to, TransitionAction.ENTER));
public void RecordTransitionShouldTransition(TStateId from, TStateId to)
=> recordedEvents.Enqueue(new TransitionEvent(from, to, TransitionAction.SHOULD_TRANSITION));
public StateBase<TStateId> Track(StateBase<TStateId> state) {
return tracker.Wrap(state);
}
private string CreateTraceback() {
StringBuilder builder = new StringBuilder();
builder.AppendLine();
foreach (var ev in recordedEvents) {
builder.AppendLine(ev.ToString());
}
return builder.ToString();
}
public void DiscardAll() {
recordedEvents.Clear();
}
}
public class Recorder : Recorder<string> {
}
}