forked from tbranyen/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_advanced.js
More file actions
220 lines (162 loc) · 9.11 KB
/
test_advanced.js
File metadata and controls
220 lines (162 loc) · 9.11 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
//-----------------------------------------------------------------------------
module("advanced");
//-----------------------------------------------------------------------------
test("multiple 'from' states for the same event", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: ['green', 'yellow'], to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: ['yellow', 'red'], to: 'green' },
]});
equals(fsm.current, 'green', "initial state should be green");
ok(fsm.can('warn'), "should be able to warn from green state")
ok(fsm.can('panic'), "should be able to panic from green state")
ok(fsm.cannot('calm'), "should NOT be able to calm from green state")
ok(fsm.cannot('clear'), "should NOT be able to clear from green state")
fsm.warn(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equals(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equals(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equals(fsm.current, 'green', "clear event should transition from yellow to green");
fsm.panic(); equals(fsm.current, 'red', "panic event should transition from green to red");
fsm.clear(); equals(fsm.current, 'green', "clear event should transition from red to green");
});
//-----------------------------------------------------------------------------
test("multiple 'to' states for the same event", function() {
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied' },
{ name: 'eat', from: 'satisfied', to: 'full' },
{ name: 'eat', from: 'full', to: 'sick' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },
]});
equals(fsm.current, 'hungry');
ok(fsm.can('eat'));
ok(fsm.can('rest'));
fsm.eat();
equals(fsm.current, 'satisfied');
fsm.eat();
equals(fsm.current, 'full');
fsm.eat();
equals(fsm.current, 'sick');
fsm.rest();
equals(fsm.current, 'hungry');
});
//-----------------------------------------------------------------------------
test("no-op transitions (github issue #5) with multiple from states", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: ['green', 'yellow'], to: 'red' },
{ name: 'noop', from: ['green', 'yellow'] }, // NOTE: 'to' not specified
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: ['yellow', 'red'], to: 'green' },
]});
equals(fsm.current, 'green', "initial state should be green");
ok(fsm.can('warn'), "should be able to warn from green state")
ok(fsm.can('panic'), "should be able to panic from green state")
ok(fsm.can('noop'), "should be able to noop from green state")
ok(fsm.cannot('calm'), "should NOT be able to calm from green state")
ok(fsm.cannot('clear'), "should NOT be able to clear from green state")
fsm.noop(); equals(fsm.current, 'green', "noop event should not transition");
fsm.warn(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
ok(fsm.cannot('warn'), "should NOT be able to warn from yellow state")
ok(fsm.can('panic'), "should be able to panic from yellow state")
ok(fsm.can('noop'), "should be able to noop from yellow state")
ok(fsm.cannot('calm'), "should NOT be able to calm from yellow state")
ok(fsm.can('clear'), "should be able to clear from yellow state")
fsm.noop(); equals(fsm.current, 'yellow', "noop event should not transition");
fsm.panic(); equals(fsm.current, 'red', "panic event should transition from yellow to red");
ok(fsm.cannot('warn'), "should NOT be able to warn from red state")
ok(fsm.cannot('panic'), "should NOT be able to panic from red state")
ok(fsm.cannot('noop'), "should NOT be able to noop from red state")
ok(fsm.can('calm'), "should be able to calm from red state")
ok(fsm.can('clear'), "should be able to clear from red state")
});
//-----------------------------------------------------------------------------
test("callbacks are called when appropriate for multiple 'from' and 'to' transitions", function() {
var called = [];
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied' },
{ name: 'eat', from: 'satisfied', to: 'full' },
{ name: 'eat', from: 'full', to: 'sick' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },
],
callbacks: {
onchangestate: function(event,from,to) { called.push('onchange from ' + from + ' to ' + to); },
onenterhungry: function() { called.push('onenterhungry'); },
onleavehungry: function() { called.push('onleavehungry'); },
onentersatisfied: function() { called.push('onentersatisfied'); },
onleavesatisfied: function() { called.push('onleavesatisfied'); },
onenterfull: function() { called.push('onenterfull'); },
onleavefull: function() { called.push('onleavefull'); },
onentersick: function() { called.push('onentersick'); },
onleavesick: function() { called.push('onleavesick'); },
onbeforeeat: function() { called.push('onbeforeeat'); },
onaftereat: function() { called.push('onaftereat'); },
onbeforerest: function() { called.push('onbeforerest'); },
onafterrest: function() { called.push('onafterrest'); }
}
});
called = [];
fsm.eat();
deepEqual(called, ['onbeforeeat', 'onleavehungry', 'onentersatisfied', 'onchange from hungry to satisfied', 'onaftereat']);
called = [];
fsm.eat();
deepEqual(called, ['onbeforeeat', 'onleavesatisfied', 'onenterfull', 'onchange from satisfied to full', 'onaftereat']);
called = [];
fsm.eat();
deepEqual(called, ['onbeforeeat', 'onleavefull', 'onentersick', 'onchange from full to sick', 'onaftereat']);
called = [];
fsm.rest();
deepEqual(called, ['onbeforerest', 'onleavesick', 'onenterhungry', 'onchange from sick to hungry', 'onafterrest']);
});
//-----------------------------------------------------------------------------
test("callbacks are called when appropriate for prototype based state machine", function() {
myFSM = function() {
this.called = [];
this.startup();
};
myFSM.prototype = {
onchangestate: function(event,from,to) { this.called.push('onchange from ' + from + ' to ' + to); },
onentergreen: function() { this.called.push('onentergreen'); },
onleavegreen: function() { this.called.push('onleavegreen'); },
onenteryellow : function() { this.called.push('onenteryellow'); },
onleaveyellow: function() { this.called.push('onleaveyellow'); },
onenterred: function() { this.called.push('onenterred'); },
onleavered: function() { this.called.push('onleavered'); },
onbeforestartup: function() { this.called.push('onbeforestartup'); },
onafterstartup: function() { this.called.push('onafterstartup'); },
onbeforewarn: function() { this.called.push('onbeforewarn'); },
onafterwarn: function() { this.called.push('onafterwarn'); },
onbeforepanic: function() { this.called.push('onbeforepanic'); },
onafterpanic: function() { this.called.push('onafterpanic'); },
onbeforeclear: function() { this.called.push('onbeforeclear'); },
onafterclear: function() { this.called.push('onafterclear'); }
};
StateMachine.create({
target: myFSM.prototype,
events: [
{ name: 'startup', from: 'none', to: 'green' },
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
});
var a = new myFSM();
var b = new myFSM();
equal(a.current, 'green', 'start with correct state');
equal(b.current, 'green', 'start with correct state');
deepEqual(a.called, ['onbeforestartup', 'onentergreen', 'onchange from none to green', 'onafterstartup']);
deepEqual(b.called, ['onbeforestartup', 'onentergreen', 'onchange from none to green', 'onafterstartup']);
a.warn();
equal(a.current, 'yellow', 'maintain independent current state');
equal(b.current, 'green', 'maintain independent current state');
deepEqual(a.called, ['onbeforestartup', 'onentergreen', 'onchange from none to green', 'onafterstartup', 'onbeforewarn', 'onleavegreen', 'onenteryellow', 'onchange from green to yellow', 'onafterwarn']);
deepEqual(b.called, ['onbeforestartup', 'onentergreen', 'onchange from none to green', 'onafterstartup']);
});