Skip to content

Commit d403758

Browse files
committed
added function (fsm.states) returning list of all available states to help automated testing (issue jakesgordon#54)
1 parent 7d4c16c commit d403758

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Version 2.4.0 (ETA - December 2016)
2323
* exclude build files from bower install (pull request #75)
2424
* ensure WILDCARD events are included in list of available transitions() (issue #93)
2525
* fix FSM getting stuck into "*" state when using double wildcard (issue #64)
26+
* function (fsm.states) returning list of all available states in the machine would help automated testing (issue #54)
2627

2728
Version 2.3.5 (January 20 2014)
2829
-------------------------------

state-machine.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
5656
}
57+
if (e.to)
58+
transitions[e.to] = transitions[e.to] || [];
5759
};
5860

5961
if (initial) {
@@ -81,6 +83,7 @@
8183
fsm.transitions = function() { return (transitions[this.current] || []).concat(transitions[StateMachine.WILDCARD] || []); };
8284
fsm.isFinished = function() { return this.is(terminal); };
8385
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)
86+
fsm.states = function() { return Object.keys(transitions).sort() };
8487

8588
if (initial && !initial.defer)
8689
fsm[initial.event]();

state-machine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_basics.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,24 @@ test("is", function() {
114114

115115
//-----------------------------------------------------------------------------
116116

117+
test("states", function() {
118+
119+
var fsm = StateMachine.create({
120+
initial: 'green',
121+
events: [
122+
{ name: 'warn', from: 'green', to: 'yellow' },
123+
{ name: 'panic', from: 'yellow', to: 'red' },
124+
{ name: 'calm', from: 'red', to: 'yellow' },
125+
{ name: 'clear', from: 'yellow', to: 'green' },
126+
{ name: 'finish', from: 'green', to: 'done' },
127+
]});
128+
129+
deepEqual(fsm.states(), [ 'done', 'green', 'none', 'red', 'yellow' ]);
130+
131+
});
132+
133+
//-----------------------------------------------------------------------------
134+
117135
test("transitions", function() {
118136

119137
var fsm = StateMachine.create({

0 commit comments

Comments
 (0)