mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-05-16 00:12:55 -04:00
Add unit and integration tests for the new functionality.
This commit is contained in:
parent
848deaff30
commit
a6de046dda
11 changed files with 335 additions and 0 deletions
BIN
test/fixtures/cloud_variables_exceeded_limit.sb2
vendored
Normal file
BIN
test/fixtures/cloud_variables_exceeded_limit.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_exceeded_limit.sb3
vendored
Normal file
BIN
test/fixtures/cloud_variables_exceeded_limit.sb3
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_limit.sb2
vendored
Normal file
BIN
test/fixtures/cloud_variables_limit.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_limit.sb3
vendored
Normal file
BIN
test/fixtures/cloud_variables_limit.sb3
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_local.sb2
vendored
Normal file
BIN
test/fixtures/cloud_variables_local.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_local.sb3
vendored
Normal file
BIN
test/fixtures/cloud_variables_local.sb3
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_simple.sb2
vendored
Normal file
BIN
test/fixtures/cloud_variables_simple.sb2
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/cloud_variables_simple.sb3
vendored
Normal file
BIN
test/fixtures/cloud_variables_simple.sb3
vendored
Normal file
Binary file not shown.
152
test/integration/cloud_variables_sb2.js
Normal file
152
test/integration/cloud_variables_sb2.js
Normal file
|
@ -0,0 +1,152 @@
|
|||
const path = require('path');
|
||||
const test = require('tap').test;
|
||||
const makeTestStorage = require('../fixtures/make-test-storage');
|
||||
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
||||
const VirtualMachine = require('../../src/index');
|
||||
|
||||
const cloudVarSimpleUri = path.resolve(__dirname, '../fixtures/cloud_variables_simple.sb2');
|
||||
const cloudVarLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_limit.sb2');
|
||||
const cloudVarExceededLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_exceeded_limit.sb2');
|
||||
const cloudVarLocalUri = path.resolve(__dirname, '../fixtures/cloud_variables_local.sb2');
|
||||
|
||||
const cloudVarSimple = readFileToBuffer(cloudVarSimpleUri);
|
||||
const cloudVarLimit = readFileToBuffer(cloudVarLimitUri);
|
||||
const cloudVarExceededLimit = readFileToBuffer(cloudVarExceededLimitUri);
|
||||
const cloudVarLocal = readFileToBuffer(cloudVarLocalUri);
|
||||
|
||||
test('importing an sb2 project with cloud variables', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarSimple).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
t.equal(stageVars.length, 1);
|
||||
|
||||
const variable = stageVars[0];
|
||||
t.equal(variable.name, '☁ firstCloud');
|
||||
t.equal(Number(variable.value), 100); // Though scratch 2 requires
|
||||
// cloud variables to be numbers, this is something that happens
|
||||
// when the message is being sent to the server rather than on the client
|
||||
t.equal(variable.isCloud, true);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing an sb2 project with cloud variables at the limit for a project', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarLimit).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 8);
|
||||
// All of the 8 stage variables should be cloud variables
|
||||
t.equal(stageVars.filter(v => v.isCloud).length, 8);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing an sb2 project with cloud variables exceeding the limit for a project', t => {
|
||||
// This tests a hacked project where additional cloud variables exceeding
|
||||
// the project limit have been added.
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarExceededLimit).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 15);
|
||||
// Only 8 of the variables should have the isCloud flag set to true
|
||||
t.equal(stageVars.filter(v => v.isCloud).length, 8);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing one project after the other resets cloud variable limit', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarExceededLimit).then(() => {
|
||||
t.equal(vm.runtime.canAddNewCloudVariable(), false);
|
||||
|
||||
vm.loadProject(cloudVarSimple).then(() => {
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
t.equal(stageVars.length, 1);
|
||||
|
||||
const variable = stageVars[0];
|
||||
t.equal(variable.name, '☁ firstCloud');
|
||||
t.equal(Number(variable.value), 100); // Though scratch 2 requires
|
||||
// cloud variables to be numbers, this is something that happens
|
||||
// when the message is being sent to the server rather than on the client
|
||||
t.equal(variable.isCloud, true);
|
||||
|
||||
t.equal(vm.runtime.canAddNewCloudVariable(), true);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('local cloud variables get imported as regular variables', t => {
|
||||
// This tests a hacked project where additional cloud variables exceeding
|
||||
// the project limit have been added.
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarLocal).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, false);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 0);
|
||||
|
||||
const sprite = vm.runtime.targets[1];
|
||||
const spriteVars = Object.values(sprite.variables);
|
||||
|
||||
t.equal(spriteVars.length, 1);
|
||||
t.equal(spriteVars[0].isCloud, false);
|
||||
|
||||
t.end();
|
||||
|
||||
process.nextTick(process.exit); // This is needed because this is the end of the last test in this file!!!
|
||||
});
|
||||
});
|
148
test/integration/cloud_variables_sb3.js
Normal file
148
test/integration/cloud_variables_sb3.js
Normal file
|
@ -0,0 +1,148 @@
|
|||
const path = require('path');
|
||||
const test = require('tap').test;
|
||||
const makeTestStorage = require('../fixtures/make-test-storage');
|
||||
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
||||
const VirtualMachine = require('../../src/index');
|
||||
|
||||
const cloudVarSimpleUri = path.resolve(__dirname, '../fixtures/cloud_variables_simple.sb3');
|
||||
const cloudVarLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_limit.sb3');
|
||||
const cloudVarExceededLimitUri = path.resolve(__dirname, '../fixtures/cloud_variables_exceeded_limit.sb3');
|
||||
const cloudVarLocalUri = path.resolve(__dirname, '../fixtures/cloud_variables_local.sb3');
|
||||
|
||||
const cloudVarSimple = readFileToBuffer(cloudVarSimpleUri);
|
||||
const cloudVarLimit = readFileToBuffer(cloudVarLimitUri);
|
||||
const cloudVarExceededLimit = readFileToBuffer(cloudVarExceededLimitUri);
|
||||
const cloudVarLocal = readFileToBuffer(cloudVarLocalUri);
|
||||
|
||||
test('importing an sb3 project with cloud variables', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarSimple).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
t.equal(stageVars.length, 1);
|
||||
|
||||
const variable = stageVars[0];
|
||||
t.equal(variable.name, '☁ firstCloud');
|
||||
t.equal(Number(variable.value), 100);
|
||||
t.equal(variable.isCloud, true);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing an sb3 project with cloud variables at the limit for a project', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarLimit).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 8);
|
||||
// All of the 8 stage variables should be cloud variables
|
||||
t.equal(stageVars.filter(v => v.isCloud).length, 8);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing an sb3 project with cloud variables exceeding the limit for a project', t => {
|
||||
// This tests a hacked project where additional cloud variables exceeding
|
||||
// the project limit have been added.
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarExceededLimit).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, true);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 15);
|
||||
// Only 8 of the variables should have the isCloud flag set to true
|
||||
t.equal(stageVars.filter(v => v.isCloud).length, 8);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('importing one project after the other resets cloud variable limit', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarExceededLimit).then(() => {
|
||||
t.equal(vm.runtime.canAddNewCloudVariable(), false);
|
||||
|
||||
vm.loadProject(cloudVarSimple).then(() => {
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
t.equal(stageVars.length, 1);
|
||||
|
||||
const variable = stageVars[0];
|
||||
t.equal(variable.name, '☁ firstCloud');
|
||||
t.equal(Number(variable.value), 100);
|
||||
t.equal(variable.isCloud, true);
|
||||
|
||||
t.equal(vm.runtime.canAddNewCloudVariable(), true);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('local cloud variables get imported as regular variables', t => {
|
||||
// This tests a hacked project where additional cloud variables exceeding
|
||||
// the project limit have been added.
|
||||
const vm = new VirtualMachine();
|
||||
vm.attachStorage(makeTestStorage());
|
||||
|
||||
// Start VM, load project, and run
|
||||
vm.start();
|
||||
vm.clear();
|
||||
vm.setCompatibilityMode(false);
|
||||
vm.setTurboMode(false);
|
||||
vm.loadProject(cloudVarLocal).then(() => {
|
||||
t.equal(vm.runtime.hasCloudData, false);
|
||||
|
||||
const stage = vm.runtime.targets[0];
|
||||
const stageVars = Object.values(stage.variables);
|
||||
|
||||
t.equal(stageVars.length, 0);
|
||||
|
||||
const sprite = vm.runtime.targets[1];
|
||||
const spriteVars = Object.values(sprite.variables);
|
||||
|
||||
t.equal(spriteVars.length, 1);
|
||||
t.equal(spriteVars[0].isCloud, false);
|
||||
|
||||
t.end();
|
||||
|
||||
process.nextTick(process.exit); // This is needed because this is the end of the last test in this file!!!
|
||||
});
|
||||
});
|
|
@ -127,3 +127,38 @@ test('Project loaded emits runtime event', t => {
|
|||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('Cloud variable limit allows only 8 cloud variables', t => {
|
||||
// This is a test of just the cloud variable limit mechanism
|
||||
// The functions being tested below need to be used when
|
||||
// creating and deleting cloud variables in the runtime.
|
||||
|
||||
const rt = new Runtime();
|
||||
|
||||
for (let i = 0; i < 8; i++) {
|
||||
t.equal(rt.canAddNewCloudVariable(), true);
|
||||
}
|
||||
|
||||
// We should be at the cloud variable limit now
|
||||
t.equal(rt.canAddNewCloudVariable(), false);
|
||||
|
||||
// Removing a cloud variable should allow the addition of exactly one more
|
||||
// when we are at the cloud variable limit
|
||||
rt.removeExistingCloudVariable();
|
||||
|
||||
t.equal(rt.canAddNewCloudVariable(), true);
|
||||
t.equal(rt.canAddNewCloudVariable(), false);
|
||||
|
||||
// Disposing of the runtime should reset the cloud variable limitations
|
||||
rt.dispose();
|
||||
|
||||
for (let i = 0; i < 8; i++) {
|
||||
t.equal(rt.canAddNewCloudVariable(), true);
|
||||
}
|
||||
|
||||
// We should be at the cloud variable limit now
|
||||
t.equal(rt.canAddNewCloudVariable(), false);
|
||||
|
||||
t.end();
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue