UNPKG

@div-int/typedfsm

Version:

A TypeScript finite state machine library

168 lines 7.38 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const typedfsm_1 = require('../../typedfsm'); const chai_1 = require('chai'); var GhostStates; (function (GhostStates) { GhostStates['Waiting'] = 'Waiting'; GhostStates['Chasing'] = 'Chasing'; GhostStates['Scatter'] = 'Scatter'; GhostStates['Frightened'] = 'Frightened'; GhostStates['Eaten'] = 'Eaten'; GhostStates['Paused'] = 'Paused'; })(GhostStates || (GhostStates = {})); var GhostActions; (function (GhostActions) { GhostActions['Wait'] = 'Wait'; GhostActions['Chase'] = 'Chase'; GhostActions['Scatter'] = 'Scatter'; GhostActions['Frighten'] = 'Frighten'; GhostActions['Eat'] = 'Eat'; GhostActions['Pause'] = 'Pause'; })(GhostActions || (GhostActions = {})); const ghostState = new typedfsm_1.Typed.FSM('Waiting' /* Waiting */); ghostState .from('Waiting' /* Waiting */, 'Wait' /* Wait */) .to('Chasing' /* Chasing */, 'Chase' /* Chase */) .to('Scatter' /* Scatter */, 'Scatter' /* Scatter */) .to('Paused' /* Paused */, 'Pause' /* Pause */) .toFrom('Paused' /* Paused */, 'Pause' /* Pause */); ghostState .from('Chasing' /* Chasing */, 'Chase' /* Chase */) .toFrom('Scatter' /* Scatter */, 'Scatter' /* Scatter */) .toFrom('Frightened' /* Frightened */, 'Frighten' /* Frighten */) .toFrom('Paused' /* Paused */, 'Pause' /* Pause */); ghostState .from('Scatter' /* Scatter */, 'Scatter' /* Scatter */) .toFrom('Chasing' /* Chasing */, 'Chase' /* Chase */) .toFrom('Frightened' /* Frightened */, 'Frighten' /* Frighten */) .toFrom('Paused' /* Paused */, 'Pause' /* Pause */); ghostState .from('Frightened' /* Frightened */, 'Frighten' /* Frighten */) .to('Eaten' /* Eaten */, 'Eat' /* Eat */) .toFrom('Paused' /* Paused */, 'Pause' /* Pause */); ghostState .from('Eaten' /* Eaten */, 'Eat' /* Eat */) .to('Scatter' /* Scatter */, 'Scatter' /* Scatter */) .to('Chasing' /* Chasing */, 'Chase' /* Chase */) .to('Chasing' /* Chasing */, 'Chase' /* Chase */) .toFrom('Paused' /* Paused */, 'Pause' /* Pause */); describe('Test ghost state machine.', () => { it('Is the current/default state waiting?', () => { ghostState.reset(); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); }); describe('Test ghost states and actions.', () => { it('Can change state to paused?', () => { ghostState.change('Paused' /* Paused */); chai_1.expect(ghostState.currentState).to.equal('Paused' /* Paused */); }); it('Can change state to waiting?', () => { ghostState.change('Waiting' /* Waiting */); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); it('Can\'t change state to frightened? ', () => { const canChangeResult = ghostState.canChange('Frightened' /* Frightened */); chai_1.expect(canChangeResult).to.equal(false); }); it('Can change state to chasing?', () => { ghostState.change('Chasing' /* Chasing */); chai_1.expect(ghostState.currentState).to.equal('Chasing' /* Chasing */); }); it('Can change state back to paused?', () => { ghostState.change('Paused' /* Paused */); chai_1.expect(ghostState.currentState).to.equal('Paused' /* Paused */); }); it('Can perform action pause?', () => { ghostState.reset(); ghostState.do('Pause' /* Pause */); chai_1.expect(ghostState.currentState).to.equal('Paused' /* Paused */); }); it('Can perform action wait?', () => { ghostState.do('Wait' /* Wait */); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); it('Can\'t perform action frighten? ', () => { const canChangeResult = ghostState.canDo('Frighten' /* Frighten */); chai_1.expect(canChangeResult).to.equal(false); }); it('Can perform action chase?', () => { ghostState.do('Chase' /* Chase */); chai_1.expect(ghostState.currentState).to.equal('Chasing' /* Chasing */); }); it('Can perform action pause?', () => { ghostState.do('Pause' /* Pause */); chai_1.expect(ghostState.currentState).to.equal('Paused' /* Paused */); }); it('Does resetting change state to waiting?', () => { ghostState.reset(); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); }); let resultOnPostChange; describe('Create on pre change state callback.', () => { it('Should be same state if we cancel it? (change)', () => { ghostState.OnPreChange = (from, to, action) => { return false; }; ghostState.reset(); ghostState.change('Chasing' /* Chasing */); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); it('Should be chasing if we don\'t cancel it? (change)', () => { ghostState.OnPreChange = (from, to, action) => { return true; }; ghostState.reset(); ghostState.change('Chasing' /* Chasing */); chai_1.expect(ghostState.currentState).to.equal('Chasing' /* Chasing */); }); it('Should be same state if we cancel it? (do)', () => { ghostState.OnPreChange = (from, to, action) => { return false; }; ghostState.reset(); ghostState.do('Chase' /* Chase */); chai_1.expect(ghostState.currentState).to.equal('Waiting' /* Waiting */); }); it('Should be chasing if we don\'t cancel it? (do)', () => { ghostState.OnPreChange = (from, to, action) => { return true; }; ghostState.reset(); ghostState.do('Chase' /* Chase */); chai_1.expect(ghostState.currentState).to.equal('Chasing' /* Chasing */); }); }); describe('Create on post change state callback.', () => { it('Should be scatter after callback? (change)', () => { ghostState.OnPostChange = (from, to, action) => { resultOnPostChange = `${from} ===> ${to} do ${action}`; }; ghostState.reset(); ghostState.change('Scatter' /* Scatter */); chai_1.expect(ghostState.currentState).to.equal('Scatter' /* Scatter */); }); it('Should be scatter after callback? (do)', () => { ghostState.OnPostChange = (from, to, action) => { resultOnPostChange = `${from} ===> ${to} do ${action}`; }; ghostState.reset(); ghostState.do('Scatter' /* Scatter */); chai_1.expect(ghostState.currentState).to.equal('Scatter' /* Scatter */); }); }); describe('Check if we can perform invalid actions or state changes.', () => { it('Should return an error if given invalid action? (do)', () => { ghostState.reset(); const result = ghostState.do('Eat' /* Eat */); chai_1.expect(result instanceof Error).to.equal(true); }); it('Should return an error if given invalid state? (change)', () => { ghostState.reset(); const result = ghostState.change('Eaten' /* Eaten */); chai_1.expect(result instanceof Error).to.equal(true); }); }); //# sourceMappingURL=ghosts.test.js.map