forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.js
130 lines (97 loc) · 3.8 KB
/
basics.js
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
import test from 'ava';
import StateMachine from '../src/app';
//-------------------------------------------------------------------------------------------------
test('version', t => {
t.is(StateMachine.version, '3.0.1');
});
//-------------------------------------------------------------------------------------------------
test('state machine', t => {
var fsm = new StateMachine({
init: 'green',
transitions: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
})
t.is(fsm.state, 'green')
fsm.warn(); t.is(fsm.state, 'yellow')
fsm.panic(); t.is(fsm.state, 'red')
fsm.calm(); t.is(fsm.state, 'yellow')
fsm.clear(); t.is(fsm.state, 'green')
});
//-----------------------------------------------------------------------------
test('state machine factory', t => {
var Alarm = StateMachine.factory({
init: 'green',
transitions: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
}),
a = new Alarm(),
b = new Alarm();
t.is(a.state, 'green')
t.is(b.state, 'green')
a.warn(); t.is(a.state, 'yellow'); t.is(b.state, 'green')
a.panic(); t.is(a.state, 'red'); t.is(b.state, 'green')
a.calm(); t.is(a.state, 'yellow'); t.is(b.state, 'green')
a.clear(); t.is(a.state, 'green'); t.is(b.state, 'green')
b.warn(); t.is(a.state, 'green'); t.is(b.state, 'yellow')
b.panic(); t.is(a.state, 'green'); t.is(b.state, 'red')
b.calm(); t.is(a.state, 'green'); t.is(b.state, 'yellow')
b.clear(); t.is(a.state, 'green'); t.is(b.state, 'green')
});
//-----------------------------------------------------------------------------
test('state machine - applied to existing object', t => {
var obj = { name: 'alarm' }
StateMachine.apply(obj, {
init: 'green',
transitions: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
});
t.is(obj.name, 'alarm');
t.is(obj.state, 'green');
obj.warn(); t.is(obj.state, 'yellow')
obj.panic(); t.is(obj.state, 'red')
obj.calm(); t.is(obj.state, 'yellow')
obj.clear(); t.is(obj.state, 'green')
});
//-----------------------------------------------------------------------------
test('state machine factory - applied to existing class', t => {
function Alarm(name) {
this.name = name
this._fsm(); // manual step needed to construct this FSM instance
}
StateMachine.factory(Alarm, {
init: 'green',
transitions: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
});
var a = new Alarm('A'),
b = new Alarm('B');
t.is(a.name, 'A')
t.is(b.name, 'B')
t.is(a.state, 'green')
t.is(b.state, 'green')
a.warn(); t.is(a.state, 'yellow'); t.is(b.state, 'green')
a.panic(); t.is(a.state, 'red'); t.is(b.state, 'green')
a.calm(); t.is(a.state, 'yellow'); t.is(b.state, 'green')
a.clear(); t.is(a.state, 'green'); t.is(b.state, 'green')
b.warn(); t.is(a.state, 'green'); t.is(b.state, 'yellow')
b.panic(); t.is(a.state, 'green'); t.is(b.state, 'red')
b.calm(); t.is(a.state, 'green'); t.is(b.state, 'yellow')
b.clear(); t.is(a.state, 'green'); t.is(b.state, 'green')
});
//-----------------------------------------------------------------------------