mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Add tests for forEach block
This commit is contained in:
parent
928df260ff
commit
a03f45d1d7
1 changed files with 66 additions and 0 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue