From 1373d1d6907adf1f5b07098e7c83069330357688 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Fri, 16 Mar 2018 11:30:54 -0400 Subject: [PATCH] Start mscript work by adding tests. Will be relying on qunit. --- app/lib/mscript/index.js | 23 ++---- app/test/arduino_tests.js | 31 ++++++++ app/test/index.js | 3 + app/test/mscript_test.js | 146 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+), 18 deletions(-) create mode 100644 app/test/arduino_tests.js create mode 100644 app/test/index.js create mode 100644 app/test/mscript_test.js diff --git a/app/lib/mscript/index.js b/app/lib/mscript/index.js index 51deaa0..4a2ef53 100644 --- a/app/lib/mscript/index.js +++ b/app/lib/mscript/index.js @@ -1,10 +1,11 @@ -var fs, - input; +'use strict'; -var mscript = {}; +let fs; +let input; + +const mscript = {}; mscript.arg = function arg (shrt, lng) { - 'use strict'; if (process.argv.indexOf(shrt) !== -1 || process.argv.indexOf(lng) !== -1) { return true; @@ -13,7 +14,6 @@ mscript.arg = function arg (shrt, lng) { }; mscript.arg_pos = function arg_pos (shrt, lng) { - 'use strict'; var pos = -1; pos = process.argv.indexOf(shrt); if (pos === -1) { @@ -41,7 +41,6 @@ mscript.alts = { }; mscript.state = {}; mscript.state_clear = function state_clear () { - 'use strict'; mscript.state = { cam : 0, proj : 0, @@ -51,7 +50,6 @@ mscript.state_clear = function state_clear () { }; }; mscript.alts_unique = function alts_unique () { - 'use strict'; var ids = Object.keys(mscript.alts), all = []; for (var i = 0; i < ids.length; i++) { @@ -63,7 +61,6 @@ mscript.alts_unique = function alts_unique () { } }; mscript.interpret = function interpret (text, callback) { - 'use strict'; mscript.state_clear(); if (typeof text === 'undefined') { mscript.fail('No input'); @@ -218,15 +215,12 @@ mscript.interpret = function interpret (text, callback) { } }; mscript.last_loop = function last_loop () { - 'use strict'; return mscript.state.loops[mscript.state.loops.length - 1]; }; mscript.parent_loop = function parent_loop () { - 'use script'; return mscript.state.loops[mscript.state.loops.length - 2]; }; mscript.state_update = function state_update (cmd, val) { - 'use strict'; if (cmd === 'END') { for (var i = 0; i < val; i++) { 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) { - 'use strict'; var cnt = str.split(cmd), c = parseInt(cnt[1]), arr = []; @@ -294,7 +287,6 @@ mscript.str_to_arr = function str_to_arr (str, cmd) { return arr; }; mscript.light_state = function light_state (str) { - 'use strict'; //add parsers for other color spaces var color = str.replace('L ', '').trim(); mscript.state.color = color; @@ -322,16 +314,13 @@ mscript.light_to_arr = function light_to_arr (str, cmd) { return arr; }; mscript.loop_count = function loop_count (str) { - 'use strict'; return parseInt(str.split('LOOP ')[1]); }; mscript.fail = function fail (reason) { - 'use strict'; console.error(JSON.stringify({success: false, error: true, msg : reason})); if (process) process.exit(); }; mscript.output = function output (data) { - 'use strict'; var json = true; //default if (mscript.arg('-j', '--json')) { json = true; @@ -351,7 +340,6 @@ mscript.output = function output (data) { } }; mscript.init = function init () { - 'use strict'; if (mscript.arg('-t', '--tests')) { return mscript.tests(); } @@ -381,7 +369,6 @@ mscript.init = function init () { }; mscript.tests = function tests () { - 'use strict'; console.log('Running mscript tests'); console.time('Tests took'); diff --git a/app/test/arduino_tests.js b/app/test/arduino_tests.js new file mode 100644 index 0000000..dc6f2fe --- /dev/null +++ b/app/test/arduino_tests.js @@ -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 + + }); +});*/ \ No newline at end of file diff --git a/app/test/index.js b/app/test/index.js new file mode 100644 index 0000000..6f4720e --- /dev/null +++ b/app/test/index.js @@ -0,0 +1,3 @@ +'use strict' + +const mscript_test = require('./mscript_test.js'); \ No newline at end of file diff --git a/app/test/mscript_test.js b/app/test/mscript_test.js new file mode 100644 index 0000000..c8d2e1c --- /dev/null +++ b/app/test/mscript_test.js @@ -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); + } + }); +*/ \ No newline at end of file