@@ -121,8 +121,15 @@ test("callbacks are called when appropriate for multiple 'from' and 'to' transit
121121 { name : 'rest' , from : [ 'hungry' , 'satisfied' , 'full' , 'sick' ] , to : 'hungry' } ,
122122 ] ,
123123 callbacks : {
124- onchangestate : function ( event , from , to ) { called . push ( 'onchange from ' + from + ' to ' + to ) ; } ,
125124
125+ // generic callbacks
126+ onbeforeevent : function ( event , from , to ) { called . push ( 'onbefore(' + event + ')' ) ; } ,
127+ onafterevent : function ( event , from , to ) { called . push ( 'onafter(' + event + ')' ) ; } ,
128+ onleavestate : function ( event , from , to ) { called . push ( 'onleave(' + from + ')' ) ; } ,
129+ onenterstate : function ( event , from , to ) { called . push ( 'onenter(' + to + ')' ) ; } ,
130+ onchangestate : function ( event , from , to ) { called . push ( 'onchange(' + from + ',' + to + ')' ) ; } ,
131+
132+ // specific state callbacks
126133 onenterhungry : function ( ) { called . push ( 'onenterhungry' ) ; } ,
127134 onleavehungry : function ( ) { called . push ( 'onleavehungry' ) ; } ,
128135 onentersatisfied : function ( ) { called . push ( 'onentersatisfied' ) ; } ,
@@ -132,6 +139,7 @@ test("callbacks are called when appropriate for multiple 'from' and 'to' transit
132139 onentersick : function ( ) { called . push ( 'onentersick' ) ; } ,
133140 onleavesick : function ( ) { called . push ( 'onleavesick' ) ; } ,
134141
142+ // specific event callbacks
135143 onbeforeeat : function ( ) { called . push ( 'onbeforeeat' ) ; } ,
136144 onaftereat : function ( ) { called . push ( 'onaftereat' ) ; } ,
137145 onbeforerest : function ( ) { called . push ( 'onbeforerest' ) ; } ,
@@ -141,19 +149,59 @@ test("callbacks are called when appropriate for multiple 'from' and 'to' transit
141149
142150 called = [ ] ;
143151 fsm . eat ( ) ;
144- deepEqual ( called , [ 'onbeforeeat' , 'onleavehungry' , 'onentersatisfied' , 'onchange from hungry to satisfied' , 'onaftereat' ] ) ;
152+ deepEqual ( called , [
153+ 'onbeforeeat' ,
154+ 'onbefore(eat)' ,
155+ 'onleavehungry' ,
156+ 'onleave(hungry)' ,
157+ 'onentersatisfied' ,
158+ 'onenter(satisfied)' ,
159+ 'onchange(hungry,satisfied)' ,
160+ 'onaftereat' ,
161+ 'onafter(eat)'
162+ ] ) ;
145163
146164 called = [ ] ;
147165 fsm . eat ( ) ;
148- deepEqual ( called , [ 'onbeforeeat' , 'onleavesatisfied' , 'onenterfull' , 'onchange from satisfied to full' , 'onaftereat' ] ) ;
166+ deepEqual ( called , [
167+ 'onbeforeeat' ,
168+ 'onbefore(eat)' ,
169+ 'onleavesatisfied' ,
170+ 'onleave(satisfied)' ,
171+ 'onenterfull' ,
172+ 'onenter(full)' ,
173+ 'onchange(satisfied,full)' ,
174+ 'onaftereat' ,
175+ 'onafter(eat)' ,
176+ ] ) ;
149177
150178 called = [ ] ;
151179 fsm . eat ( ) ;
152- deepEqual ( called , [ 'onbeforeeat' , 'onleavefull' , 'onentersick' , 'onchange from full to sick' , 'onaftereat' ] ) ;
180+ deepEqual ( called , [
181+ 'onbeforeeat' ,
182+ 'onbefore(eat)' ,
183+ 'onleavefull' ,
184+ 'onleave(full)' ,
185+ 'onentersick' ,
186+ 'onenter(sick)' ,
187+ 'onchange(full,sick)' ,
188+ 'onaftereat' ,
189+ 'onafter(eat)'
190+ ] ) ;
153191
154192 called = [ ] ;
155193 fsm . rest ( ) ;
156- deepEqual ( called , [ 'onbeforerest' , 'onleavesick' , 'onenterhungry' , 'onchange from sick to hungry' , 'onafterrest' ] ) ;
194+ deepEqual ( called , [
195+ 'onbeforerest' ,
196+ 'onbefore(rest)' ,
197+ 'onleavesick' ,
198+ 'onleave(sick)' ,
199+ 'onenterhungry' ,
200+ 'onenter(hungry)' ,
201+ 'onchange(sick,hungry)' ,
202+ 'onafterrest' ,
203+ 'onafter(rest)'
204+ ] ) ;
157205
158206} ) ;
159207
@@ -168,15 +216,24 @@ test("callbacks are called when appropriate for prototype based state machine",
168216
169217 myFSM . prototype = {
170218
171- onchangestate : function ( event , from , to ) { this . called . push ( 'onchange from ' + from + ' to ' + to ) ; } ,
219+ // generic callbacks
220+ onbeforeevent : function ( event , from , to ) { this . called . push ( 'onbefore(' + event + ')' ) ; } ,
221+ onafterevent : function ( event , from , to ) { this . called . push ( 'onafter(' + event + ')' ) ; } ,
222+ onleavestate : function ( event , from , to ) { this . called . push ( 'onleave(' + from + ')' ) ; } ,
223+ onenterstate : function ( event , from , to ) { this . called . push ( 'onenter(' + to + ')' ) ; } ,
224+ onchangestate : function ( event , from , to ) { this . called . push ( 'onchange(' + from + ',' + to + ')' ) ; } ,
172225
226+ // specific state callbacks
227+ onenternone : function ( ) { this . called . push ( 'onenternone' ) ; } ,
228+ onleavenone : function ( ) { this . called . push ( 'onleavenone' ) ; } ,
173229 onentergreen : function ( ) { this . called . push ( 'onentergreen' ) ; } ,
174230 onleavegreen : function ( ) { this . called . push ( 'onleavegreen' ) ; } ,
175231 onenteryellow : function ( ) { this . called . push ( 'onenteryellow' ) ; } ,
176232 onleaveyellow : function ( ) { this . called . push ( 'onleaveyellow' ) ; } ,
177233 onenterred : function ( ) { this . called . push ( 'onenterred' ) ; } ,
178234 onleavered : function ( ) { this . called . push ( 'onleavered' ) ; } ,
179235
236+ // specific event callbacks
180237 onbeforestartup : function ( ) { this . called . push ( 'onbeforestartup' ) ; } ,
181238 onafterstartup : function ( ) { this . called . push ( 'onafterstartup' ) ; } ,
182239 onbeforewarn : function ( ) { this . called . push ( 'onbeforewarn' ) ; } ,
@@ -203,16 +260,19 @@ test("callbacks are called when appropriate for prototype based state machine",
203260 equal ( a . current , 'green' , 'start with correct state' ) ;
204261 equal ( b . current , 'green' , 'start with correct state' ) ;
205262
206- deepEqual ( a . called , [ 'onbeforestartup' , 'onentergreen' , 'onchange from none to green' , 'onafterstartup' ] ) ;
207- deepEqual ( b . called , [ 'onbeforestartup' , 'onentergreen' , 'onchange from none to green' , 'onafterstartup' ] ) ;
263+ deepEqual ( a . called , [ 'onbeforestartup' , 'onbefore(startup)' , 'onleavenone' , 'onleave(none)' , 'onentergreen' , 'onenter(green)' , 'onchange(none,green)' , 'onafterstartup' , 'onafter(startup)' ] ) ;
264+ deepEqual ( b . called , [ 'onbeforestartup' , 'onbefore(startup)' , 'onleavenone' , 'onleave(none)' , 'onentergreen' , 'onenter(green)' , 'onchange(none,green)' , 'onafterstartup' , 'onafter(startup)' ] ) ;
265+
266+ a . called = [ ] ;
267+ b . called = [ ] ;
208268
209269 a . warn ( ) ;
210270
211271 equal ( a . current , 'yellow' , 'maintain independent current state' ) ;
212272 equal ( b . current , 'green' , 'maintain independent current state' ) ;
213273
214- deepEqual ( a . called , [ 'onbeforestartup ' , 'onentergreen ' , 'onchange from none to green ' , 'onafterstartup ' , 'onbeforewarn ' , 'onleavegreen ' , 'onenteryellow' , ' onchange from green to yellow', 'onafterwarn' ] ) ;
215- deepEqual ( b . called , [ 'onbeforestartup' , 'onentergreen' , 'onchange from none to green' , 'onafterstartup' ] ) ;
274+ deepEqual ( a . called , [ 'onbeforewarn ' , 'onbefore(warn) ' , 'onleavegreen ' , 'onleave(green) ' , 'onenteryellow ' , 'onenter(yellow) ' , 'onchange( green, yellow) ' , 'onafterwarn' , 'onafter(warn) '] ) ;
275+ deepEqual ( b . called , [ ] ) ;
216276
217277} ) ;
218278
0 commit comments