-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.ls
190 lines (140 loc) · 4.47 KB
/
tests.ls
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
require! {
backbone4000: Backbone
helpers: h
underscore: _
'./index': abstractman
ubigraph: ubigraph
bluebird: p
}
exports.basic = (test) ->
data = {}
SM = abstractman.StateMachine.extend4000 do
state: 'init'
states:
init:
children: { +A, +B }
visit: ->
data.initVisit = true
@changeState 'A'
leave: ->
data.initLeave = true
A:
children: { +B }
visit: ->
data.Avisit = true
@changeState 'B'
leave: ->
data.Aleave = true
B:
visit: ->
test.deepEqual data, { initVisit: true, initLeave: true, Avisit: true, Aleave: true }
test.done()
sm = new SM()
sm.on 'changestate', (newStateName, oldStateName, event) ->
console.log 'changestate', oldStateName, '->', newStateName, (event or "")
exports.promise = (test) ->
events = []
SM = abstractman.PromiseStateMachine.extend4000 do
state: 'init'
states:
init:
children: { +A, +B }
A:
children: { +B, 'error' }
B:
child: 'A'
error: true
state_init: -> new p (resolve,reject) ~>
events.push 'init'
h.wait 100, -> resolve state: 'B', event: 8
state_B: (fromState, data) -> new p (resolve,reject) ~>
events.push 'b'
test.equals data, 8
test.equals fromState, 'init'
resolve 'A'
state_A: (fromState, data) -> new p (resolve,reject) ~>
events.push 'a'
test.equals data, void
test.equals fromState, 'B'
reject new Error 'some error'
state_error: (fromState, data) ->
test.equal String(data), "Error: some error"
test.deepEqual [ 'init', 'b', 'a' ], events
test.done()
sm = new SM()
sm.on 'changestate', (newStateName, oldStateName, event) ->
console.log 'changestate', oldStateName, '->', newStateName, (event or "")
exports.promiseImplicit = (test) ->
events = []
SM = abstractman.PromiseStateMachine.extend4000 do
state: 'init'
states:
init:
children: { +A, +B }
A:
children: { +B }
B:
child: 'A'
error: true
state_init: -> new p (resolve,reject) ~>
events.push 'init'
h.wait 100, -> resolve state: 'B', event: 8
state_B: (fromState, data) -> new p (resolve,reject) ~>
events.push 'b'
test.equals data, 8
test.equals fromState, 'init'
resolve { test: 3 }
state_A: (fromState, data) -> new p (resolve,reject) ~>
events.push 'a'
test.deepEqual data, { test: 3 }
test.equals fromState, "B"
reject new Error 'some error'
state_error: (fromState, data) ->
test.equal String(data), "Error: some error"
test.deepEqual [ 'init', 'b', 'a' ], events
test.done()
sm = new SM()
sm.on 'changestate', (newStateName, oldStateName, event) ->
console.log 'changestate', oldStateName, '->', newStateName, (event or "")
exports.stateMbasic = (test) ->
Machine = abstractman.GraphStateMachine.extend4000 do
name: 'test machine'
defaults:
state: 'state_a'
statea = Machine.defineState name: 'state_a', children: [ 'state_b' ]
Machine.defineState name: 'state_b', children: [ 'state_c' ]
Machine.defineState name: 'state_c', children: [ 'state_a' ]
stated = statea.defineChild name: 'state_d'
stated.addChild 'state_a'
machine = new Machine()
#machine.states.state_a.visit()
machine.changeState 'state_b', { bla: 1 }
test.done()
exports.stateMchange = (test) ->
Machine = abstractman.GraphStateMachine.extend4000 do
name: 'test machine'
defaults:
state: 'state_a'
statea = Machine.defineState name: 'state_a', children: [ 'state_b' ]
Machine.defineState name: 'state_b', children: [ 'state_c' ]
Machine.defineState name: 'state_c', children: [ 'state_a' ]
stated = statea.defineChild name: 'state_d'
stated.addChild 'state_a'
machine = new Machine()
console.log 'will set state'
machine.set state: 'state_b'
console.log machine.state.name
test.done()
exports.stateMdefine = (test) ->
Machine = abstractman.GraphStateMachine.extend4000 do
name: 'test machine'
defaults:
state: 'state_a'
states:
state_a:
child: 'state_b'
state_b: true
machine = new Machine()
machine.states.state_a.visit()
machine.states.state_a.changeState 'state_b', { bla: 2 }
test.done()