diff --git a/test/unit/blocks_control.js b/test/unit/blocks_control.js index e2eb383a4..691f2dfe5 100644 --- a/test/unit/blocks_control.js +++ b/test/unit/blocks_control.js @@ -52,6 +52,72 @@ test('repeatUntil', t => { t.end(); }); +test('forEach', t => { + const rt = new Runtime(); + const c = new Control(rt); + + // for each (variable) in "abcd" + // ..should yield variable values "a", "b", "c", "d" + const variableValues = []; + const variable = {value: 0}; + let value = 'abcd'; + + const util = { + stackFrame: Object.create(null), + target: { + lookupOrCreateVariable: function () { + return variable; + } + }, + startBranch: function () { + variableValues.push(variable.value); + c.forEach({VARIABLE: {}, VALUE: value}, util); + } + }; + + c.forEach({VARIABLE: {}, VALUE: value}, util); + t.deepEqual(variableValues, ['a', 'b', 'c', 'd']); + + // for each (variable) in "5" + // ..should yield variable values 1, 2, 3, 4, 5 + util.stackFrame = Object.create(null); + variableValues.splice(0); + variable.value = 0; + value = '5'; + c.forEach({VARIABLE: {}, VALUE: value}, util); + t.deepEqual(variableValues, [1, 2, 3, 4, 5]); + + // for each (variable) in 4 + // ..should yield variable values 1, 2, 3, 4 + util.stackFrame = Object.create(null); + variableValues.splice(0); + variable.value = 0; + value = 4; + c.forEach({VARIABLE: {}, VALUE: value}, util); + t.deepEqual(variableValues, [1, 2, 3, 4]); + + // for each (variable) in 10: + // if variable % 4 === 3: + // variable++ + // ..should yield variable values 1, 2, 3, 5, 6, 7, 9, 10 + // (this script skips multiples of 4) + util.stackFrame = Object.create(null); + variableValues.splice(0); + variable.value = 0; + value = 10; + util.startBranch = function () { + variableValues.push(variable.value); + if (variable.value % 4 === 3) { + variable.value++; + } + c.forEach({VARIABLE: {}, VALUE: value}, util); + }; + c.forEach({VARIABLE: {}, VALUE: value}, util); + t.deepEquals(variableValues, [1, 2, 3, 5, 6, 7, 9, 10]); + + t.end(); +}); + test('forever', t => { const rt = new Runtime(); const c = new Control(rt);