Permalink
Browse files

added function (fsm.states) returning list of all available states to…

… help automated testing (issue #54)
  • Loading branch information...
1 parent 7d4c16c commit d4037581fa41f7f66ebc664ea1d2a13bf2da57ee @jakesgordon committed Nov 19, 2016
Showing with 23 additions and 1 deletion.
  1. +1 −0 RELEASE_NOTES.md
  2. +3 −0 state-machine.js
  3. +1 −1 state-machine.min.js
  4. +18 −0 test/test_basics.js
View
@@ -23,6 +23,7 @@ Version 2.4.0 (ETA - December 2016)
* exclude build files from bower install (pull request #75)
* ensure WILDCARD events are included in list of available transitions() (issue #93)
* fix FSM getting stuck into "*" state when using double wildcard (issue #64)
+ * function (fsm.states) returning list of all available states in the machine would help automated testing (issue #54)
Version 2.3.5 (January 20 2014)
-------------------------------
View
@@ -54,6 +54,8 @@
map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
}
+ if (e.to)
+ transitions[e.to] = transitions[e.to] || [];
};
if (initial) {
@@ -81,6 +83,7 @@
fsm.transitions = function() { return (transitions[this.current] || []).concat(transitions[StateMachine.WILDCARD] || []); };
fsm.isFinished = function() { return this.is(terminal); };
fsm.error = cfg.error || function(name, from, to, args, error, msg, e) { throw e || msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3 and #17)
+ fsm.states = function() { return Object.keys(transitions).sort() };
if (initial && !initial.defer)
fsm[initial.event]();
@@ -1 +1 @@
-(function(){var a={VERSION:"2.4.0",Result:{SUCCEEDED:1,NOTRANSITION:2,CANCELLED:3,PENDING:4},Error:{INVALID_TRANSITION:100,PENDING_TRANSITION:200,INVALID_CALLBACK:300},WILDCARD:"*",ASYNC:"async",create:function(g,h){var j=(typeof g.initial=="string")?{state:g.initial}:g.initial;var f=g.terminal||g["final"];var e=h||g.target||{};var m=g.events||[];var i=g.callbacks||{};var c={};var k={};var l=function(o){var q=Array.isArray(o.from)?o.from:(o.from?[o.from]:[a.WILDCARD]);c[o.name]=c[o.name]||{};for(var p=0;p<q.length;p++){k[q[p]]=k[q[p]]||[];k[q[p]].push(o.name);c[o.name][q[p]]=o.to||q[p]}};if(j){j.event=j.event||"startup";l({name:j.event,from:"none",to:j.state})}for(var d=0;d<m.length;d++){l(m[d])}for(var b in c){if(c.hasOwnProperty(b)){e[b]=a.buildEvent(b,c[b])}}for(var b in i){if(i.hasOwnProperty(b)){e[b]=i[b]}}e.current="none";e.is=function(n){return Array.isArray(n)?(n.indexOf(this.current)>=0):(this.current===n)};e.can=function(n){return !this.transition&&(c[n]!==undefined)&&(c[n].hasOwnProperty(this.current)||c[n].hasOwnProperty(a.WILDCARD))};e.cannot=function(n){return !this.can(n)};e.transitions=function(){return(k[this.current]||[]).concat(k[a.WILDCARD]||[])};e.isFinished=function(){return this.is(f)};e.error=g.error||function(p,t,s,o,n,r,q){throw q||r};if(j&&!j.defer){e[j.event]()}return e},doCallback:function(g,d,c,i,h,b){if(d){try{return d.apply(g,[c,i,h].concat(b))}catch(f){return g.error(c,i,h,b,a.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",f)}}},beforeAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onbeforeevent,c,f,e,b)},afterAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onafterevent||d.onevent,c,f,e,b)},leaveAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onleavestate,c,f,e,b)},enterAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onenterstate||d.onstate,c,f,e,b)},changeState:function(d,c,f,e,b){return a.doCallback(d,d.onchangestate,c,f,e,b)},beforeThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onbefore"+c],c,f,e,b)},afterThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onafter"+c]||d["on"+c],c,f,e,b)},leaveThisState:function(d,c,f,e,b){return a.doCallback(d,d["onleave"+f],c,f,e,b)},enterThisState:function(d,c,f,e,b){return a.doCallback(d,d["onenter"+e]||d["on"+e],c,f,e,b)},beforeEvent:function(d,c,f,e,b){if((false===a.beforeThisEvent(d,c,f,e,b))||(false===a.beforeAnyEvent(d,c,f,e,b))){return false}},afterEvent:function(d,c,f,e,b){a.afterThisEvent(d,c,f,e,b);a.afterAnyEvent(d,c,f,e,b)},leaveState:function(f,e,h,g,d){var c=a.leaveThisState(f,e,h,g,d),b=a.leaveAnyState(f,e,h,g,d);if((false===c)||(false===b)){return false}else{if((a.ASYNC===c)||(a.ASYNC===b)){return a.ASYNC}}},enterState:function(d,c,f,e,b){a.enterThisState(d,c,f,e,b);a.enterAnyState(d,c,f,e,b)},buildEvent:function(b,c){return function(){var h=this.current;var g=c[h]||c[a.WILDCARD]||h;var e=Array.prototype.slice.call(arguments);if(this.transition){return this.error(b,h,g,e,a.Error.PENDING_TRANSITION,"event "+b+" inappropriate because previous transition did not complete")}if(this.cannot(b)){return this.error(b,h,g,e,a.Error.INVALID_TRANSITION,"event "+b+" inappropriate in current state "+this.current)}if(false===a.beforeEvent(this,b,h,g,e)){return a.Result.CANCELLED}if(h===g){a.afterEvent(this,b,h,g,e);return a.Result.NOTRANSITION}var f=this;this.transition=function(){f.transition=null;f.current=g;a.enterState(f,b,h,g,e);a.changeState(f,b,h,g,e);a.afterEvent(f,b,h,g,e);return a.Result.SUCCEEDED};this.transition.cancel=function(){f.transition=null;a.afterEvent(f,b,h,g,e)};var d=a.leaveState(this,b,h,g,e);if(false===d){this.transition=null;return a.Result.CANCELLED}else{if(a.ASYNC===d){return a.Result.PENDING}else{if(this.transition){return this.transition()}}}}}};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=a}exports.StateMachine=a}else{if(typeof define==="function"&&define.amd){define(function(b){return a})}else{if(typeof window!=="undefined"){window.StateMachine=a}else{if(typeof self!=="undefined"){self.StateMachine=a}}}}}());
+(function(){var a={VERSION:"2.4.0",Result:{SUCCEEDED:1,NOTRANSITION:2,CANCELLED:3,PENDING:4},Error:{INVALID_TRANSITION:100,PENDING_TRANSITION:200,INVALID_CALLBACK:300},WILDCARD:"*",ASYNC:"async",create:function(g,h){var j=(typeof g.initial=="string")?{state:g.initial}:g.initial;var f=g.terminal||g["final"];var e=h||g.target||{};var m=g.events||[];var i=g.callbacks||{};var c={};var k={};var l=function(o){var q=Array.isArray(o.from)?o.from:(o.from?[o.from]:[a.WILDCARD]);c[o.name]=c[o.name]||{};for(var p=0;p<q.length;p++){k[q[p]]=k[q[p]]||[];k[q[p]].push(o.name);c[o.name][q[p]]=o.to||q[p]}if(o.to){k[o.to]=k[o.to]||[]}};if(j){j.event=j.event||"startup";l({name:j.event,from:"none",to:j.state})}for(var d=0;d<m.length;d++){l(m[d])}for(var b in c){if(c.hasOwnProperty(b)){e[b]=a.buildEvent(b,c[b])}}for(var b in i){if(i.hasOwnProperty(b)){e[b]=i[b]}}e.current="none";e.is=function(n){return Array.isArray(n)?(n.indexOf(this.current)>=0):(this.current===n)};e.can=function(n){return !this.transition&&(c[n]!==undefined)&&(c[n].hasOwnProperty(this.current)||c[n].hasOwnProperty(a.WILDCARD))};e.cannot=function(n){return !this.can(n)};e.transitions=function(){return(k[this.current]||[]).concat(k[a.WILDCARD]||[])};e.isFinished=function(){return this.is(f)};e.error=g.error||function(p,t,s,o,n,r,q){throw q||r};e.states=function(){return Object.keys(k).sort()};if(j&&!j.defer){e[j.event]()}return e},doCallback:function(g,d,c,i,h,b){if(d){try{return d.apply(g,[c,i,h].concat(b))}catch(f){return g.error(c,i,h,b,a.Error.INVALID_CALLBACK,"an exception occurred in a caller-provided callback function",f)}}},beforeAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onbeforeevent,c,f,e,b)},afterAnyEvent:function(d,c,f,e,b){return a.doCallback(d,d.onafterevent||d.onevent,c,f,e,b)},leaveAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onleavestate,c,f,e,b)},enterAnyState:function(d,c,f,e,b){return a.doCallback(d,d.onenterstate||d.onstate,c,f,e,b)},changeState:function(d,c,f,e,b){return a.doCallback(d,d.onchangestate,c,f,e,b)},beforeThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onbefore"+c],c,f,e,b)},afterThisEvent:function(d,c,f,e,b){return a.doCallback(d,d["onafter"+c]||d["on"+c],c,f,e,b)},leaveThisState:function(d,c,f,e,b){return a.doCallback(d,d["onleave"+f],c,f,e,b)},enterThisState:function(d,c,f,e,b){return a.doCallback(d,d["onenter"+e]||d["on"+e],c,f,e,b)},beforeEvent:function(d,c,f,e,b){if((false===a.beforeThisEvent(d,c,f,e,b))||(false===a.beforeAnyEvent(d,c,f,e,b))){return false}},afterEvent:function(d,c,f,e,b){a.afterThisEvent(d,c,f,e,b);a.afterAnyEvent(d,c,f,e,b)},leaveState:function(f,e,h,g,d){var c=a.leaveThisState(f,e,h,g,d),b=a.leaveAnyState(f,e,h,g,d);if((false===c)||(false===b)){return false}else{if((a.ASYNC===c)||(a.ASYNC===b)){return a.ASYNC}}},enterState:function(d,c,f,e,b){a.enterThisState(d,c,f,e,b);a.enterAnyState(d,c,f,e,b)},buildEvent:function(b,c){return function(){var h=this.current;var g=c[h]||(c[a.WILDCARD]!=a.WILDCARD?c[a.WILDCARD]:h)||h;var e=Array.prototype.slice.call(arguments);if(this.transition){return this.error(b,h,g,e,a.Error.PENDING_TRANSITION,"event "+b+" inappropriate because previous transition did not complete")}if(this.cannot(b)){return this.error(b,h,g,e,a.Error.INVALID_TRANSITION,"event "+b+" inappropriate in current state "+this.current)}if(false===a.beforeEvent(this,b,h,g,e)){return a.Result.CANCELLED}if(h===g){a.afterEvent(this,b,h,g,e);return a.Result.NOTRANSITION}var f=this;this.transition=function(){f.transition=null;f.current=g;a.enterState(f,b,h,g,e);a.changeState(f,b,h,g,e);a.afterEvent(f,b,h,g,e);return a.Result.SUCCEEDED};this.transition.cancel=function(){f.transition=null;a.afterEvent(f,b,h,g,e)};var d=a.leaveState(this,b,h,g,e);if(false===d){this.transition=null;return a.Result.CANCELLED}else{if(a.ASYNC===d){return a.Result.PENDING}else{if(this.transition){return this.transition()}}}}}};if(typeof exports!=="undefined"){if(typeof module!=="undefined"&&module.exports){exports=module.exports=a}exports.StateMachine=a}else{if(typeof define==="function"&&define.amd){define(function(b){return a})}else{if(typeof window!=="undefined"){window.StateMachine=a}else{if(typeof self!=="undefined"){self.StateMachine=a}}}}}());
View
@@ -114,6 +114,24 @@ test("is", function() {
//-----------------------------------------------------------------------------
+test("states", function() {
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ events: [
+ { name: 'warn', from: 'green', to: 'yellow' },
+ { name: 'panic', from: 'yellow', to: 'red' },
+ { name: 'calm', from: 'red', to: 'yellow' },
+ { name: 'clear', from: 'yellow', to: 'green' },
+ { name: 'finish', from: 'green', to: 'done' },
+ ]});
+
+ deepEqual(fsm.states(), [ 'done', 'green', 'none', 'red', 'yellow' ]);
+
+});
+
+//-----------------------------------------------------------------------------
+
test("transitions", function() {
var fsm = StateMachine.create({

0 comments on commit d403758

Please sign in to comment.