node-state-machine
Version:
State machine for NodeJS.
129 lines (112 loc) • 3.89 kB
JavaScript
import chai from 'chai';
import { Machine } from '../dist/model/Machine';
let expect = chai.expect;
describe('Machine tests', function() {
it('should fail without states', () => {
expect(() => { new Machine(); }).to.throw('No states provided!');
});
it('should not have duplicate states', () => {
let m = new Machine('1', '2', '2', '4', '3', '4', '5');
expect(m.numberOfStates()).to.equal(5);
});
it('should have correct initial value', () => {
let m = new Machine('1', '2', '2', '4', '3', '4', '5');
expect(m.initial()).to.equal('1');
});
it('should work with bulk on sample machine A', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('B').to('C').on('a');
m.from('C').to('A').on('c');
m.finalize();
m.bulkProcess(['a', 'a', 'c']);
expect(m.current()).to.equal('A');
});
it('should work on sample machine A', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('B').to('C').on('a');
m.from('C').to('A').on('c');
m.finalize();
m.process('a').process('a').process('c');
expect(m.current()).to.equal('A');
});
it('should not work on unfinalized sample machine A', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('B').to('C').on('a');
m.from('C').to('A').on('c');
expect(() => { m.process('a') }).to.throw('Machine is not finalized!');
});
it('should not work with illegal string on sample machine A', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('B').to('C').on('a');
m.from('C').to('A').on('c');
m.finalize();
expect(() => { m.process('z') }).to.throw('No transitions found!');
});
it('should work on sample machine B', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('A').to('C').default();
m.from('B').to('C').on('a');
m.from('C').to('A').on('c');
m.from('C').to('B').default();
m.finalize();
m.process('z').process('c').process('a');
expect(m.current()).to.equal('B');
});
it('should have defaults', () => {
let m = new Machine('A', 'B', 'C');
m.setDefaults();
m.from('A').to('B').on('z');
m.from('B').to('C').on('z');
m.finalize();
m.process('a');
expect(m.current()).to.equal('A');
m.process('z');
expect(m.current()).to.equal('B');
m.process('k');
expect(m.current()).to.equal('B');
m.process('z');
expect(m.current()).to.equal('C');
m.process('z');
expect(m.current()).to.equal('C');
});
it('should detect a deterministic machine', () => {
let m = new Machine('A', 'B', 'C');
m.setDefaults();
expect(m.isDeterministic(['a', 'b'])).to.equal(true);
});
it('should detect another deterministic machine', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('A').to('B').on('b');
m.from('B').to('C').on('a');
m.from('B').to('C').on('b');
m.from('C').to('A').on('a');
m.from('C').to('A').on('b');
expect(m.isDeterministic(['a', 'b'])).to.equal(true);
});
it('should detect a non-deterministic machine', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('A').to('C').on('a');
m.from('A').to('B').on('b');
m.from('B').to('C').on('a');
m.from('B').to('C').on('b');
m.from('C').to('A').on('a');
m.from('C').to('A').on('b');
expect(m.isDeterministic(['a', 'b'])).to.equal(false);
});
it('should detect another non-deterministic machine', () => {
let m = new Machine('A', 'B', 'C');
m.from('A').to('B').on('a');
m.from('A').to('B').on('b');
m.from('B').to('C').on('a');
m.from('C').to('A').on('a');
m.from('C').to('A').on('b');
expect(m.isDeterministic(['a', 'b'])).to.equal(false);
});
});