|
| 1 | +//----------------------------------------------------------------------------- |
| 2 | + |
| 3 | +module("classes"); |
| 4 | + |
| 5 | +//----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +test("prototype based state machine", function() { |
| 8 | + |
| 9 | + var myFSM = function() { |
| 10 | + this.counter = 42; |
| 11 | + this.startup(); |
| 12 | + }; |
| 13 | + |
| 14 | + myFSM.prototype = { |
| 15 | + onwarn: function() { this.counter++; } |
| 16 | + } |
| 17 | + |
| 18 | + StateMachine.create({ |
| 19 | + target: myFSM.prototype, |
| 20 | + events: [ |
| 21 | + { name: 'startup', from: 'none', to: 'green' }, |
| 22 | + { name: 'warn', from: 'green', to: 'yellow' }, |
| 23 | + { name: 'panic', from: 'yellow', to: 'red' }, |
| 24 | + { name: 'clear', from: 'yellow', to: 'green' } |
| 25 | + ] |
| 26 | + }); |
| 27 | + |
| 28 | + var a = new myFSM(); |
| 29 | + var b = new myFSM(); |
| 30 | + |
| 31 | + equal(a.current, 'green', 'start with correct state'); |
| 32 | + equal(b.current, 'green', 'start with correct state'); |
| 33 | + |
| 34 | + equal(a.counter, 42, 'start with correct counter'); |
| 35 | + equal(b.counter, 42, 'start with correct counter'); |
| 36 | + |
| 37 | + a.warn(); |
| 38 | + |
| 39 | + equal(a.current, 'yellow', 'maintain independent current state'); |
| 40 | + equal(b.current, 'green', 'maintain independent current state'); |
| 41 | + |
| 42 | + equal(a.counter, 43, 'counter for (a) should have incremented'); |
| 43 | + equal(b.counter, 42, 'counter for (b) should remain untouched'); |
| 44 | + |
| 45 | + ok(a.hasOwnProperty('current'), "each instance should have its own current state"); |
| 46 | + ok(b.hasOwnProperty('current'), "each instance should have its own current state"); |
| 47 | + ok(!a.hasOwnProperty('warn'), "each instance should NOT have its own event methods"); |
| 48 | + ok(!b.hasOwnProperty('warn'), "each instance should NOT have its own event methods"); |
| 49 | + ok(a.warn === b.warn, "each instance should share event methods"); |
| 50 | + ok(a.warn === a.__proto__.warn, "each instance event methods come from its shared prototype"); |
| 51 | + ok(b.warn === b.__proto__.warn, "each instance event methods come from its shared prototype"); |
| 52 | + |
| 53 | +}); |
| 54 | + |
| 55 | +//----------------------------------------------------------------------------- |
| 56 | + |
| 57 | +test("github issue 19", function() { |
| 58 | + |
| 59 | + var Foo = function() { |
| 60 | + this.counter = 7; |
| 61 | + this.initFSM(); |
| 62 | + }; |
| 63 | + |
| 64 | + Foo.prototype.onenterready = function() { this.counter++; }; |
| 65 | + Foo.prototype.onenterrunning = function() { this.counter++; }; |
| 66 | + |
| 67 | + StateMachine.create({ |
| 68 | + target : Foo.prototype, |
| 69 | + initial: { state: 'ready', event: 'initFSM', defer: true }, // unfortunately, trying to apply an IMMEDIATE initial state wont work on prototype based FSM, it MUST be deferred and called in the constructor for each instance |
| 70 | + events : [{name: 'execute', from: 'ready', to: 'running'}, |
| 71 | + {name: 'abort', from: 'running', to: 'ready'}] |
| 72 | + }); |
| 73 | + |
| 74 | + var foo = new Foo(); |
| 75 | + var bar = new Foo(); |
| 76 | + |
| 77 | + equal(foo.current, 'ready', 'start with correct state'); |
| 78 | + equal(bar.current, 'ready', 'start with correct state'); |
| 79 | + |
| 80 | + equal(foo.counter, 8, 'start with correct counter 7 (from constructor) + 1 (from onenterready)'); |
| 81 | + equal(bar.counter, 8, 'start with correct counter 7 (from constructor) + 1 (from onenterready)'); |
| 82 | + |
| 83 | + foo.execute(); // transition foo, but NOT bar |
| 84 | + |
| 85 | + equal(foo.current, 'running', 'changed state'); |
| 86 | + equal(bar.current, 'ready', 'state remains the same'); |
| 87 | + |
| 88 | + equal(foo.counter, 9, 'incremented counter during onenterrunning'); |
| 89 | + equal(bar.counter, 8, 'counter remains the same'); |
| 90 | + |
| 91 | +}); |
| 92 | + |
0 commit comments