Skip to content

Commit

Permalink
convert actions to spec. convert array_transitions to spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed Jan 15, 2021
1 parent fb301b6 commit a2b70f9
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 2 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"module": "dist/es6/jssm.js",
"types": "./jssm.d.ts",
"scripts": {
"jest-func": "jest --color --verbose",
"jest-spec": "jest -c jest-spec.config.js --color --verbose",
"jest-stoch": "jest -c jest-stoch.config.js --color --verbose",
"jest": "npm run jest-func && npm run jest-stoch",
"test": "npm run make && npm run justtest",
Expand Down
3 changes: 2 additions & 1 deletion src/ts/tests/actions.js
Expand Up @@ -3,7 +3,8 @@

import {describe} from 'ava-spec';

const jssm = require('../../../build/jssm.es5.cjs.js');
const jssm = require('../../../build/jssm.es5.cjs.js'),
sm = jssm.sm;



Expand Down
109 changes: 109 additions & 0 deletions src/ts/tests/actions.spec.ts
@@ -0,0 +1,109 @@

/* eslint-disable max-len */

const jssm = require('../../../build/jssm.es5.cjs.js'),
sm = jssm.sm;



describe('Reports on actions', () => {

const roa_machine = new jssm.Machine({
start_states: ['off'],
transitions:[ { name:'turn_on', action:'power_on', from:'off', to:'red'} ]
});

const roa_la = roa_machine.list_actions(); // todo comeback

test('Actions that it has', () => expect( typeof roa_machine.current_action_for('power_on') ).toBe('number') );
test('Actions that it doesn\'t have', () => expect( typeof roa_machine.current_action_for('power_left') ).toBe('undefined') );
test('Correct list type', () => expect( Array.isArray(roa_la) ).toBe(true) );
test('Correct list size', () => expect( roa_la.length ).toBe(1) );

});





describe('Actions', () => {

const act_machine = new jssm.Machine({
start_states: ['off'],
transitions:[ { from:'off', to:'red', action:'on' }, { from:'red', to:'off',action:'off' } ]
});

test('Red has actions().length 1', () => expect( act_machine.actions().length ).toBe(1) );
test('Red has actions()[0] "on"', () => expect( act_machine.actions()[0] ).toBe('on') );

test('Red has actions().length 1 again', () => expect( act_machine.actions('off').length ).toBe(1) );
test('Red has actions()[0] "on" again', () => expect( act_machine.actions('off')[0] ).toBe('on') );

test('Red has actions().length 1 re-again', () => expect( act_machine.actions('red').length ).toBe(1) );
test('Red has actions()[0] "off" re-again', () => expect( act_machine.actions('red')[0] ).toBe('off') );

});





describe('States having actions', () => {

const sha_machine = new jssm.Machine({
start_states: ['off'],
transitions:[ { from:'off', to:'red', action:'on' }, { from:'red', to:'off',action:'off' } ]
});

test('One action has "on"', () => expect( sha_machine.list_states_having_action('on').length ).toBe(1) );
test('"on" is had by "off"', () => expect( sha_machine.list_states_having_action('on')[0] ).toBe('off') );

});





describe('List exit actions', () => {

const lea_machine = new jssm.Machine({
start_states: ['off'],
transitions:[ { from:'off', to:'red', action:'on' }, { from:'red', to:'off',action:'off' } ]
});

test('List exit actions, shows "on" from off as default', () => expect( lea_machine.list_exit_actions()[0] ).toBe('on') );
test('List exit actions, shows "on" from off', () => expect( lea_machine.list_exit_actions('off')[0] ).toBe('on') );
test('List exit actions, shows "off" from red', () => expect( lea_machine.list_exit_actions('red')[0] ).toBe('off') );

});





describe('Reports on action edges', () => {

const roae_machine = new jssm.Machine({
start_states: ['off'],
transitions:[ { name:'turn_on', action:'power_on', from:'off', to:'red'} ]
});

test('Reports on action edges, that it has', () => expect( typeof roae_machine.current_action_edge_for('power_on') ).toBe('object') );
test('Reports on action edges, that it doesn\'t have', () => expect( () => roae_machine.current_action_edge_for('power_west') ).toThrow() );

});





describe('Two nodes should be able to have matching edges with differing action labels', () => {

// const machine = sm`a 'first' -> a; a 'second' -> a;`;

// test('that it has', () => expect( typeof machine.current_action_edge_for('power_on') ).toBe('object') );
// test('that it doesn\'t have', () => expect( () => machine.current_action_edge_for('power_west') ).toThrow() );

test.todo('todo - uncomment when #531 is resolved');

});
129 changes: 129 additions & 0 deletions src/ts/tests/array_transitions.spec.ts
@@ -0,0 +1,129 @@

/* eslint-disable max-len */

const jssm = require('../../../build/jssm.es5.cjs.js');





describe('Array basics', () => {



describe('array of one', () => {

const aLeft = [
{main_path: false,forced_only: false,"from":"a","to":"d","kind":"legal"},
];

test('[a]->d;', () => expect( jssm.compile(jssm.parse('[a]->d;')).transitions ).toEqual(aLeft) );

});



describe('array of one', () => {

const aLeft = [
{main_path: false,forced_only: false,"from":"a","to":"d","kind":"legal"},
];

test('[a]->d;', () => expect( jssm.compile(jssm.parse('[a]->d;')).transitions ).toEqual(aLeft) );

});



});





describe('Array sides', () => {



describe('array on left', () => {

const aLeft = [
{main_path: false,forced_only: false,"from":"a","to":"d","kind":"legal"},
{main_path: false,forced_only: false,"from":"b","to":"d","kind":"legal"},
{main_path: false,forced_only: false,"from":"c","to":"d","kind":"legal"}
];

test('[a b c]->d;', () => expect( jssm.compile(jssm.parse('[a b c]->d;')).transitions ).toEqual(aLeft) );

});



describe('array on right', () => {

const aRight = [
{main_path: false,forced_only: false,"from":"a","to":"b","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"c","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"d","kind":"legal"}
];

test('a->[b c d];', () => expect( jssm.compile(jssm.parse('a->[b c d];')).transitions ).toEqual(aRight) );

});



describe('array on both sides', () => {

const aBoth = [
{main_path: false,forced_only: false,"from":"a","to":"x","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"y","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"z","kind":"legal"},
{main_path: false,forced_only: false,"from":"b","to":"x","kind":"legal"},
{main_path: false,forced_only: false,"from":"b","to":"y","kind":"legal"},
{main_path: false,forced_only: false,"from":"b","to":"z","kind":"legal"},
{main_path: false,forced_only: false,"from":"c","to":"x","kind":"legal"},
{main_path: false,forced_only: false,"from":"c","to":"y","kind":"legal"},
{main_path: false,forced_only: false,"from":"c","to":"z","kind":"legal"}
];

test('[a b c]->[x y z];', () => expect( jssm.compile(jssm.parse('[a b c]->[x y z];')).transitions ).toEqual(aBoth) );

});



describe('array in middle', () => {

const aBoth = [
{main_path: false,forced_only: false,"from":"a","to":"x","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"y","kind":"legal"},
{main_path: false,forced_only: false,"from":"a","to":"z","kind":"legal"},
{main_path: false,forced_only: false,"from":"x","to":"b","kind":"legal"},
{main_path: false,forced_only: false,"from":"y","to":"b","kind":"legal"},
{main_path: false,forced_only: false,"from":"z","to":"b","kind":"legal"}
];

test('a->[x y z]->b;', () => expect( jssm.compile(jssm.parse('a->[x y z]->b;')).transitions ).toEqual(aBoth) );

});



});





describe('array of zero must throw', () => {

const aLeft = [];

test('On left []->d;', () => expect( () => jssm.compile( jssm.parse('[]->d;') ).transitions ).toThrow() );
test('On right d->[];', () => expect( () => jssm.compile( jssm.parse('d->[];') ).transitions ).toThrow() );
test('On both []->[];', () => expect( () => jssm.compile( jssm.parse('[]->[];') ).transitions ).toThrow() );
test('In middle d->[]->e;', () => expect( () => jssm.compile( jssm.parse('d->[]->e;') ).transitions ).toThrow() );
test('In loop d->[]->d;', () => expect( () => jssm.compile( jssm.parse('d->[]->d;') ).transitions ).toThrow() );

});

0 comments on commit a2b70f9

Please sign in to comment.