Navigation Menu

Skip to content

Commit

Permalink
added function (fsm.states) returning list of all available states to…
Browse files Browse the repository at this point in the history
… help automated testing (issue #54)
  • Loading branch information
jakesgordon committed Nov 19, 2016
1 parent 7d4c16c commit d403758
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Expand Up @@ -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)
-------------------------------
Expand Down
3 changes: 3 additions & 0 deletions state-machine.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]();
Expand Down
2 changes: 1 addition & 1 deletion state-machine.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions test/test_basics.js
Expand Up @@ -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({
Expand Down

0 comments on commit d403758

Please sign in to comment.