@@ -368,7 +368,7 @@ test("no-op transitions (github issue #5)", function() {
368368 var fsm = StateMachine . create ( {
369369 initial : 'green' ,
370370 events : [
371- { name : 'noop' , from : 'green' , } , // NOTE: no 'to' option specified
371+ { name : 'noop' , from : 'green' , /* no-op */ } ,
372372 { name : 'warn' , from : 'green' , to : 'yellow' } ,
373373 { name : 'panic' , from : 'yellow' , to : 'red' } ,
374374 { name : 'calm' , from : 'red' , to : 'yellow' } ,
@@ -388,4 +388,64 @@ test("no-op transitions (github issue #5)", function() {
388388
389389} ) ;
390390
391+ //-----------------------------------------------------------------------------
392+
393+ test ( "wildcard 'from' allows event from any state (github issue #11)" , function ( ) {
394+
395+ var fsm = StateMachine . create ( {
396+ initial : 'stopped' ,
397+ events : [
398+ { name : 'prepare' , from : 'stopped' , to : 'ready' } ,
399+ { name : 'start' , from : 'ready' , to : 'running' } ,
400+ { name : 'resume' , from : 'paused' , to : 'running' } ,
401+ { name : 'pause' , from : 'running' , to : 'paused' } ,
402+ { name : 'stop' , from : '*' , to : 'stopped' }
403+ ] } ) ;
404+
405+ equals ( fsm . current , 'stopped' , "initial state should be stopped" ) ;
406+
407+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
408+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from ready to stopped" ) ;
409+
410+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
411+ fsm . start ( ) ; equals ( fsm . current , 'running' , "start event should transition from ready to running" ) ;
412+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from running to stopped" ) ;
413+
414+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
415+ fsm . start ( ) ; equals ( fsm . current , 'running' , "start event should transition from ready to running" ) ;
416+ fsm . pause ( ) ; equals ( fsm . current , 'paused' , "pause event should transition from running to paused" ) ;
417+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from paused to stopped" ) ;
418+
419+ } ) ;
420+
421+ //-----------------------------------------------------------------------------
422+
423+ test ( "missing 'from' allows event from any state (github issue #11) " , function ( ) {
424+
425+ var fsm = StateMachine . create ( {
426+ initial : 'stopped' ,
427+ events : [
428+ { name : 'prepare' , from : 'stopped' , to : 'ready' } ,
429+ { name : 'start' , from : 'ready' , to : 'running' } ,
430+ { name : 'resume' , from : 'paused' , to : 'running' } ,
431+ { name : 'pause' , from : 'running' , to : 'paused' } ,
432+ { name : 'stop' , /* any from state */ to : 'stopped' }
433+ ] } ) ;
434+
435+ equals ( fsm . current , 'stopped' , "initial state should be stopped" ) ;
436+
437+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
438+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from ready to stopped" ) ;
439+
440+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
441+ fsm . start ( ) ; equals ( fsm . current , 'running' , "start event should transition from ready to running" ) ;
442+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from running to stopped" ) ;
443+
444+ fsm . prepare ( ) ; equals ( fsm . current , 'ready' , "prepare event should transition from stopped to ready" ) ;
445+ fsm . start ( ) ; equals ( fsm . current , 'running' , "start event should transition from ready to running" ) ;
446+ fsm . pause ( ) ; equals ( fsm . current , 'paused' , "pause event should transition from running to paused" ) ;
447+ fsm . stop ( ) ; equals ( fsm . current , 'stopped' , "stop event should transition from paused to stopped" ) ;
448+
449+ } ) ;
450+
391451
0 commit comments