Start mscript work by adding tests. Will be relying on qunit.

This commit is contained in:
mmcwilliams 2018-03-16 11:30:54 -04:00
parent bab6593fba
commit 1373d1d690
4 changed files with 185 additions and 18 deletions

View File

@ -1,10 +1,11 @@
var fs, 'use strict';
input;
var mscript = {}; let fs;
let input;
const mscript = {};
mscript.arg = function arg (shrt, lng) { mscript.arg = function arg (shrt, lng) {
'use strict';
if (process.argv.indexOf(shrt) !== -1 || if (process.argv.indexOf(shrt) !== -1 ||
process.argv.indexOf(lng) !== -1) { process.argv.indexOf(lng) !== -1) {
return true; return true;
@ -13,7 +14,6 @@ mscript.arg = function arg (shrt, lng) {
}; };
mscript.arg_pos = function arg_pos (shrt, lng) { mscript.arg_pos = function arg_pos (shrt, lng) {
'use strict';
var pos = -1; var pos = -1;
pos = process.argv.indexOf(shrt); pos = process.argv.indexOf(shrt);
if (pos === -1) { if (pos === -1) {
@ -41,7 +41,6 @@ mscript.alts = {
}; };
mscript.state = {}; mscript.state = {};
mscript.state_clear = function state_clear () { mscript.state_clear = function state_clear () {
'use strict';
mscript.state = { mscript.state = {
cam : 0, cam : 0,
proj : 0, proj : 0,
@ -51,7 +50,6 @@ mscript.state_clear = function state_clear () {
}; };
}; };
mscript.alts_unique = function alts_unique () { mscript.alts_unique = function alts_unique () {
'use strict';
var ids = Object.keys(mscript.alts), var ids = Object.keys(mscript.alts),
all = []; all = [];
for (var i = 0; i < ids.length; i++) { for (var i = 0; i < ids.length; i++) {
@ -63,7 +61,6 @@ mscript.alts_unique = function alts_unique () {
} }
}; };
mscript.interpret = function interpret (text, callback) { mscript.interpret = function interpret (text, callback) {
'use strict';
mscript.state_clear(); mscript.state_clear();
if (typeof text === 'undefined') { if (typeof text === 'undefined') {
mscript.fail('No input'); mscript.fail('No input');
@ -218,15 +215,12 @@ mscript.interpret = function interpret (text, callback) {
} }
}; };
mscript.last_loop = function last_loop () { mscript.last_loop = function last_loop () {
'use strict';
return mscript.state.loops[mscript.state.loops.length - 1]; return mscript.state.loops[mscript.state.loops.length - 1];
}; };
mscript.parent_loop = function parent_loop () { mscript.parent_loop = function parent_loop () {
'use script';
return mscript.state.loops[mscript.state.loops.length - 2]; return mscript.state.loops[mscript.state.loops.length - 2];
}; };
mscript.state_update = function state_update (cmd, val) { mscript.state_update = function state_update (cmd, val) {
'use strict';
if (cmd === 'END') { if (cmd === 'END') {
for (var i = 0; i < val; i++) { for (var i = 0; i < val; i++) {
if (mscript.state.rec === 0) { if (mscript.state.rec === 0) {
@ -278,7 +272,6 @@ mscript.state_update = function state_update (cmd, val) {
} }
}; };
mscript.str_to_arr = function str_to_arr (str, cmd) { mscript.str_to_arr = function str_to_arr (str, cmd) {
'use strict';
var cnt = str.split(cmd), var cnt = str.split(cmd),
c = parseInt(cnt[1]), c = parseInt(cnt[1]),
arr = []; arr = [];
@ -294,7 +287,6 @@ mscript.str_to_arr = function str_to_arr (str, cmd) {
return arr; return arr;
}; };
mscript.light_state = function light_state (str) { mscript.light_state = function light_state (str) {
'use strict';
//add parsers for other color spaces //add parsers for other color spaces
var color = str.replace('L ', '').trim(); var color = str.replace('L ', '').trim();
mscript.state.color = color; mscript.state.color = color;
@ -322,16 +314,13 @@ mscript.light_to_arr = function light_to_arr (str, cmd) {
return arr; return arr;
}; };
mscript.loop_count = function loop_count (str) { mscript.loop_count = function loop_count (str) {
'use strict';
return parseInt(str.split('LOOP ')[1]); return parseInt(str.split('LOOP ')[1]);
}; };
mscript.fail = function fail (reason) { mscript.fail = function fail (reason) {
'use strict';
console.error(JSON.stringify({success: false, error: true, msg : reason})); console.error(JSON.stringify({success: false, error: true, msg : reason}));
if (process) process.exit(); if (process) process.exit();
}; };
mscript.output = function output (data) { mscript.output = function output (data) {
'use strict';
var json = true; //default var json = true; //default
if (mscript.arg('-j', '--json')) { if (mscript.arg('-j', '--json')) {
json = true; json = true;
@ -351,7 +340,6 @@ mscript.output = function output (data) {
} }
}; };
mscript.init = function init () { mscript.init = function init () {
'use strict';
if (mscript.arg('-t', '--tests')) { if (mscript.arg('-t', '--tests')) {
return mscript.tests(); return mscript.tests();
} }
@ -381,7 +369,6 @@ mscript.init = function init () {
}; };
mscript.tests = function tests () { mscript.tests = function tests () {
'use strict';
console.log('Running mscript tests'); console.log('Running mscript tests');
console.time('Tests took'); console.time('Tests took');

31
app/test/arduino_tests.js Normal file
View File

@ -0,0 +1,31 @@
/*var fs = require('fs'),
mcopy = {};
mcopy.cfg = JSON.parse(fs.readFileSync('./cfg.json', 'utf8'));
mcopy.arduino = require('../lib/mcopy-arduino.js')(mcopy.cfg);
mcopy.arduino.colorTest = function (color, cb) {
mcopy.arduino.send(mcopy.cfg.arduino.cmd.light, function () {
console.log('Light set to ' + color);
if (cb) setTimeout(cb, 20);
});
mcopy.arduino.string(color);
};
mcopy.arduino.init(function (success) {
mcopy.arduino.connect(function () {
mcopy.arduino.colorTest('255,140,70', function () {
mcopy.arduino.colorTest('5,0,0', function () {
mcopy.arduino.colorTest('255,255,255', function () {
mcopy.arduino.colorTest('160,14,250', function () {
mcopy.arduino.colorTest('5,32,200', function () {
});//5
});//4
});//3
});//2
});//1
});
});*/

3
app/test/index.js Normal file
View File

@ -0,0 +1,3 @@
'use strict'
const mscript_test = require('./mscript_test.js');

146
app/test/mscript_test.js Normal file
View File

@ -0,0 +1,146 @@
'use strict';
const mscript = require('../lib/mscript');
QUnit.test(`Basic functions`, (assert) => {
const script = 'CF\nPF\nCB\nPB\nBF\nBB';
let pass = false;
mscript.interpret(script, (obj) => {
if (obj.success === true
&& obj.cam === 0
&& obj.proj === 0
&& obj.arr.length === 6) {
pass = true;
}
assert.ok(pass, `Simple script compiles`)
});
})
/*
console.log('Basic function test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 0
&& obj.proj === 0
&& obj.arr.length === 6) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
var script = 'CF\nPF\nCB\nPB\nBF\nBB';
console.log('Functions with integers test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 0
&& obj.proj === 0
&& obj.arr.length === 6) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
script = 'CF 1000\nCB 1000\nSET PROJ 200\nPB 200';
console.log('Basic state test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 0
&& obj.proj === 0) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
script = 'LOOP 10\nCF 3\nPF 1\nEND LOOP';
console.log('Basic loop test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 30
&& obj.proj === 10
&& obj.arr.length === 40) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
script = 'LOOP 4\nLOOP 4\nPF\nBF\nEND LOOP\nEND LOOP';
console.log('Recursive loop test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 16
&& obj.proj === 16
&& obj.arr.length === 32) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
//Lighting tests
script = 'L 255,255,255\nCF\nPF';
console.log('Basic light test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 1
&& obj.proj === 1
&& obj.arr.length === 2
&& obj.light.length === 2
&& obj.light[0] === '255,255,255'
&& obj.light[1] === '') {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
script = 'L 255,255,255\nCF\nPF\nBF';
console.log('Basic black test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 2
&& obj.proj === 1
&& obj.arr.length === 3
&& obj.light.length === 3
&& obj.light[0] === '255,255,255'
&& obj.light[1] === ''
&& obj.light[2] === mscript.black) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
script = 'LOOP 2\nL 1,1,1\nCF\nL 2,2,2\nCF\nEND';
console.log('Basic light loop test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 4
&& obj.proj === 0
&& obj.arr.length === 4
&& obj.light.length === 4
&& obj.light[0] === '1,1,1'
&& obj.light[3] === '2,2,2') {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
//LOOP W/ CAM and PROJ
script = 'LOOP 2\nCAM 4\nPROJ 4\nEND';
console.log('Basic cam/proj loop test...');
mscript.interpret(script, function (obj) {
if (obj.success === true
&& obj.cam === 8
&& obj.proj === 8
&& obj.arr.length === 16
&& obj.light.length === 16
&& obj.light[0] === mscript.black) {
console.log('...Passed!');
} else {
fail(script, obj);
}
});
*/