Commit 40f3346f authored by Nicolas Biri's avatar Nicolas Biri

Test type normalisation

parent 574ef1ff
Pipeline #901 passed with stage
in 1 minute and 56 seconds
'use strict';
'use strict'
var JSMF = require('../../src/index');
const JSMF = require('../../src/index')
var Class, Model, Enum;
let Class, Model, Enum
(function() {
Model = JSMF.Model;
Class = JSMF.Class;
Enum = JSMF.Enum;
Model = JSMF.Model
Class = JSMF.Class
Enum = JSMF.Enum
}).call();
var Signal = new Enum('Signal', ['LOW', 'HIGH']);
const Signal = new Enum('Signal', ['LOW', 'HIGH'])
var NamedElement = Class.newInstance('NamedElement', [], {name: String});
const NamedElement = Class.newInstance('NamedElement', [], {name: String})
var App = Class.newInstance('App', NamedElement);
const App = Class.newInstance('App', NamedElement)
var State = Class.newInstance('State', NamedElement);
App.setReference('states', State, -1);
App.setReference('initial', State, 1);
const State = Class.newInstance('State', NamedElement)
App.setReference('states', State, -1)
App.setReference('initial', State, 1)
var Brick = Class.newInstance('Brick', NamedElement, {pin: JSMF.Range(0,13)});
const Brick = Class.newInstance('Brick', NamedElement, {pin: JSMF.Range(0,13)})
var Action = Class.newInstance('Action', [], {value: Signal});
State.setReference('action', Action, -1);
const Action = Class.newInstance('Action', [], {value: Signal})
State.setReference('action', Action, -1)
var Transition = Class.newInstance('Transition', [], {value: Signal});
Transition.setReference('next', State, 1);
State.setReference('transition', Transition, 1);
const Transition = Class.newInstance('Transition', [], {value: Signal})
Transition.setReference('next', State, 1)
State.setReference('transition', Transition, 1)
var Sensor = Class.newInstance('Sensor', Brick);
Transition.setReference('sensor', Sensor, 1);
const Sensor = Class.newInstance('Sensor', Brick)
Transition.setReference('sensor', Sensor, 1)
var Actuator = Class.newInstance('Actuator', Brick);
Action.setReference('actuator', Actuator, 1);
var Actuator = Class.newInstance('Actuator', Brick)
Action.setReference('actuator', Actuator, 1)
App.setReference('bricks', Brick, -1);
App.setReference('bricks', Brick, -1)
var ArduinoML = new Model('ArduinoML', {}, App, true);
var ArduinoML = new Model('ArduinoML', {}, App, true)
module.exports = JSMF.modelExport(ArduinoML);
module.exports = JSMF.modelExport(ArduinoML)
'use strict';
var AML = require('./MMArduinoML.js')
var Model;
const AML = require('./MMArduinoML.js')
const Model = require('../../src/index').Model
(function() {
var JSMF = require('../../src/index');
Model = JSMF.Model;
}).call();
var button = AML.Sensor.newInstance({name: 'button', pin: 9});
var led = AML.Actuator.newInstance({name: 'led', pin: 12});
const button = AML.Sensor.newInstance({name: 'button', pin: 9})
const led = AML.Actuator.newInstance({name: 'led', pin: 12})
/*
* on state
*/
var aOn = AML.Action.newInstance({value: AML.Signal.HIGH, actuator: led});
var tOn = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button});
const aOn = AML.Action.newInstance({value: AML.Signal.HIGH, actuator: led})
const tOn = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button})
var on = AML.State.newInstance({name: 'on'});
on.action = aOn;
on.transition = tOn;
const on = AML.State.newInstance({name: 'on'})
on.action = aOn
on.transition = tOn
/*
* off state
*/
var aOff = AML.Action.newInstance({value: AML.Signal.LOW, actuator: led});
var tOff = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button});
var off = AML.State.newInstance({name: 'off'});
on.action = aOff;
on.transition = tOff;
const aOff = AML.Action.newInstance({value: AML.Signal.LOW, actuator: led})
const tOff = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button})
const off = AML.State.newInstance({name: 'off'})
on.action = aOff
on.transition = tOff
/*
* set transitions
*/
tOn.next = off;
tOff.next = on;
tOn.next = off
tOff.next = on
/*
* define app
*/
var switchApp = AML.App.newInstance({
const switchApp = AML.App.newInstance({
name: 'Switch!',
bricks: [button, led],
states: [on, off],
initial: off
});
})
var Switch = new Model('Switch', AML.ArduinoML, switchApp, true);
const Switch = new Model('Switch', AML.ArduinoML, switchApp, true)
module.exports = {
Switch: Switch,
......
......@@ -35,6 +35,7 @@ module.exports =
, Date: _.isDate
, Array: _.isArray
, Object: _.isObject
, Function: _.isFunction
, Range: function Range(min, max) {
const self = x => x >= min && x <= max
Object.assign(self, {typeName: 'Range', min, max})
......
......@@ -26,6 +26,19 @@ describe('JSMFAny', function() {
})
describe('Function', done => {
it('accept functions', done => {
JSMF.Function(x => x + 1).should.be.true()
done()
})
it('refuses objects', done => {
JSMF.Function({x :42}).should.be.false()
done()
})
})
describe('Range', done => {
it('accepts values in range', done => {
......@@ -53,3 +66,13 @@ describe('Range', done => {
})
})
describe('normalizeType', done => {
it ('transform Number to JSMF.Number', done => {
JSMF.normalizeType(Number).should.be.eql(JSMF.Number)
JSMF.normalizeType(String)(2).should.be.false()
done()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment