Add references to runtime to constructor calls for Sprite, Target, and RenderedTarget so blocks get created properly.

This commit is contained in:
Karishma Chadha 2019-01-29 15:30:45 -05:00
parent 7ac8721aa5
commit e0b0d35b46
7 changed files with 90 additions and 78 deletions

View file

@ -157,7 +157,7 @@ test('set drag mode', t => {
const runtime = new Runtime();
runtime.requestTargetsUpdate = () => {}; // noop for testing
const sensing = new Sensing(runtime);
const s = new Sprite();
const s = new Sprite(null, runtime);
const rt = new RenderedTarget(s, runtime);
sensing.setDragMode({DRAG_MODE: 'not draggable'}, {target: rt});
@ -232,7 +232,7 @@ test('loud? boolean', t => {
test('get attribute of sprite variable', t => {
const rt = new Runtime();
const sensing = new Sensing(rt);
const s = new Sprite();
const s = new Sprite(null, rt);
const target = new RenderedTarget(s, rt);
const variable = {
name: 'cars',
@ -249,7 +249,7 @@ test('get attribute of sprite variable', t => {
test('get attribute of variable that does not exist', t => {
const rt = new Runtime();
const sensing = new Sensing(rt);
const s = new Sprite();
const s = new Sprite(null, rt);
const target = new RenderedTarget(s, rt);
rt.getTargetForStage = () => target;
t.equal(sensing.getAttributeOf({PROPERTY: 'variableThatDoesNotExist'}), 0);

View file

@ -67,7 +67,7 @@ const generateBlockInput = function (id, next, inp) {
};
const generateThread = function (runtime) {
const s = new Sprite();
const s = new Sprite(null, runtime);
const rt = new RenderedTarget(s, runtime);
const th = new Thread(randomString());

View file

@ -6,7 +6,7 @@ const Runtime = require('../../src/engine/runtime');
const events = require('../fixtures/events.json');
test('spec', t => {
const target = new Target();
const target = new Target(new Runtime());
t.type(Target, 'function');
t.type(target, 'object');
@ -26,7 +26,7 @@ test('spec', t => {
// Create Variable tests.
test('createVariable', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
const variables = target.variables;
@ -43,7 +43,7 @@ test('createVariable', t => {
// Create Same Variable twice.
test('createVariable2', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
@ -55,7 +55,7 @@ test('createVariable2', t => {
// Create a list
test('createListVariable creates a list', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
const variables = target.variables;
@ -123,7 +123,7 @@ test('createVariable does not call cloud io device\'s requestCreateVariable if t
});
test('createVariable throws when given invalid type', t => {
const target = new Target();
const target = new Target(new Runtime());
t.throws(
(() => target.createVariable('foo', 'bar', 'baz')),
new Error('Invalid variable type: baz')
@ -134,7 +134,7 @@ test('createVariable throws when given invalid type', t => {
// Rename Variable tests.
test('renameVariable', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
@ -151,7 +151,7 @@ test('renameVariable', t => {
// Rename Variable that doesn't exist.
test('renameVariable2', t => {
const target = new Target();
const target = new Target(new Runtime());
target.renameVariable('foo', 'bar2');
const variables = target.variables;
@ -163,7 +163,7 @@ test('renameVariable2', t => {
// Rename Variable that with id that exists as another variable's name.
// Expect no change.
test('renameVariable3', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo1', 'foo', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
@ -233,7 +233,7 @@ test('renameVariable does not call cloud io device\'s requestRenameVariable func
// Delete Variable tests.
test('deleteVariable', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.deleteVariable('foo');
@ -245,7 +245,7 @@ test('deleteVariable', t => {
// Delete Variable that doesn't exist.
test('deleteVariable2', t => {
const target = new Target();
const target = new Target(new Runtime());
target.deleteVariable('foo');
const variables = target.variables;
@ -300,7 +300,7 @@ test('deleteVariable calls cloud io device\'s requestRenameVariable function', t
});
test('duplicateVariable creates a new variable with a new ID by default', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
const originalVariable = target.variables['a var ID'];
@ -324,7 +324,7 @@ test('duplicateVariable creates a new variable with a new ID by default', t => {
});
test('duplicateVariable creates new array reference for list variable.value', t => {
const target = new Target();
const target = new Target(new Runtime());
const arr = [1, 2, 3];
target.createVariable('a var ID', 'arr', Variable.LIST_TYPE);
const originalVariable = target.variables['a var ID'];
@ -337,7 +337,7 @@ test('duplicateVariable creates new array reference for list variable.value', t
});
test('duplicateVariable creates a new variable with a original ID if specified', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
const originalVariable = target.variables['a var ID'];
@ -362,7 +362,7 @@ test('duplicateVariable creates a new variable with a original ID if specified',
});
test('duplicateVariable returns null if variable with specified ID does not exist', t => {
const target = new Target();
const target = new Target(new Runtime());
const variable = target.duplicateVariable('a var ID');
t.equal(variable, null);
@ -382,7 +382,7 @@ test('duplicateVariable returns null if variable with specified ID does not exis
});
test('duplicateVariables duplicates all variables', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('var ID 1', 'var1', Variable.SCALAR_TYPE);
target.createVariable('var ID 2', 'var2', Variable.SCALAR_TYPE);
@ -434,7 +434,7 @@ test('duplicateVariables duplicates all variables', t => {
});
test('duplicateVariables re-IDs variables when a block container is provided', t => {
const target = new Target();
const target = new Target(new Runtime());
target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);
target.createVariable('another var id', 'var2', Variable.SCALAR_TYPE);
@ -489,7 +489,7 @@ test('duplicateVariables re-IDs variables when a block container is provided', t
});
test('lookupOrCreateList creates a list if var with given id or var with given name does not exist', t => {
const target = new Target();
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
@ -502,7 +502,7 @@ test('lookupOrCreateList creates a list if var with given id or var with given n
});
test('lookupOrCreateList returns list if one with given id exists', t => {
const target = new Target();
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
@ -518,7 +518,7 @@ test('lookupOrCreateList returns list if one with given id exists', t => {
});
test('lookupOrCreateList succeeds in finding list if id is incorrect but name matches', t => {
const target = new Target();
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
@ -534,7 +534,7 @@ test('lookupOrCreateList succeeds in finding list if id is incorrect but name ma
});
test('lookupBroadcastMsg returns the var with given id if exists', t => {
const target = new Target();
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
@ -550,7 +550,7 @@ test('lookupBroadcastMsg returns the var with given id if exists', t => {
});
test('createComment adds a comment to the target', t => {
const target = new Target();
const target = new Target(new Runtime());
const comments = target.comments;
t.equal(Object.keys(comments).length, 0);
@ -572,7 +572,7 @@ test('createComment adds a comment to the target', t => {
});
test('creating comment with id that already exists does not change existing comment', t => {
const target = new Target();
const target = new Target(new Runtime());
const comments = target.comments;
t.equal(Object.keys(comments).length, 0);
@ -599,7 +599,7 @@ test('creating comment with id that already exists does not change existing comm
});
test('creating a comment with a blockId also updates the comment property on the block', t => {
const target = new Target();
const target = new Target(new Runtime());
const comments = target.comments;
// Create a mock block on the target
target.blocks = {

View file

@ -2,6 +2,7 @@ const test = require('tap').test;
const Thread = require('../../src/engine/thread');
const RenderedTarget = require('../../src/sprites/rendered-target');
const Sprite = require('../../src/sprites/sprite');
const Runtime = require('../../src/engine/runtime');
test('spec', t => {
t.type(Thread, 'function');
@ -120,8 +121,9 @@ test('PushGetParam', t => {
test('goToNextBlock', t => {
const th = new Thread('arbitraryString');
const s = new Sprite();
const rt = new RenderedTarget(s, null);
const r = new Runtime();
const s = new Sprite(null, r);
const rt = new RenderedTarget(s, r);
const block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,
@ -175,8 +177,9 @@ test('goToNextBlock', t => {
test('stopThisScript', t => {
const th = new Thread('arbitraryString');
const s = new Sprite();
const rt = new RenderedTarget(s, null);
const r = new Runtime();
const s = new Sprite(null, r);
const rt = new RenderedTarget(s, r);
const block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,
@ -227,8 +230,9 @@ test('stopThisScript', t => {
test('isRecursiveCall', t => {
const th = new Thread('arbitraryString');
const s = new Sprite();
const rt = new RenderedTarget(s, null);
const r = new Runtime();
const s = new Sprite(null, r);
const rt = new RenderedTarget(s, r);
const block1 = {fields: Object,
id: 'arbitraryString',
inputs: Object,

View file

@ -45,7 +45,8 @@ test('setProvider sets the provider', t => {
});
test('postData update message updates the variable', t => {
const stage = new Target();
const runtime = new Runtime();
const stage = new Target(runtime);
const fooVar = new Variable(
'a fake var id',
'foo',
@ -56,7 +57,6 @@ test('postData update message updates the variable', t => {
t.strictEquals(fooVar.value, 0);
const runtime = new Runtime();
const cloud = new Cloud(runtime);
cloud.setStage(stage);
cloud.postData({varUpdate: {

View file

@ -7,16 +7,17 @@ const FakeRenderer = require('../fixtures/fake-renderer');
test('clone effects', t => {
// Create two clones and ensure they have different graphic effect objects.
// Regression test for Github issue #224
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const b = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
const b = new RenderedTarget(spr, r);
t.ok(a.effects !== b.effects);
t.end();
});
test('setxy', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -27,8 +28,8 @@ test('setxy', t => {
});
test('direction', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -38,8 +39,8 @@ test('direction', t => {
});
test('setSay', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -49,8 +50,8 @@ test('setSay', t => {
});
test('setVisible', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -59,8 +60,8 @@ test('setVisible', t => {
});
test('setSize', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -70,8 +71,8 @@ test('setSize', t => {
});
test('set and clear effects', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -88,8 +89,8 @@ test('set and clear effects', t => {
test('setCostume', t => {
const o = new Object();
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
s.costumes = [o];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
@ -105,8 +106,8 @@ test('deleteCostume', t => {
const o4 = {id: 4};
const o5 = {id: 5};
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
s.costumes = [o1, o2, o3];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
@ -208,8 +209,8 @@ test('deleteSound', t => {
const o2 = {id: 2};
const o3 = {id: 3};
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
s.sounds = [o1, o2, o3];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
@ -228,8 +229,8 @@ test('deleteSound', t => {
});
test('setRotationStyle', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
a.renderer = renderer;
@ -238,8 +239,8 @@ test('setRotationStyle', t => {
});
test('getBounds', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -251,8 +252,8 @@ test('getBounds', t => {
});
test('isTouchingPoint', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -262,8 +263,8 @@ test('isTouchingPoint', t => {
});
test('isTouchingEdge', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -275,8 +276,8 @@ test('isTouchingEdge', t => {
});
test('isTouchingSprite', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -286,8 +287,8 @@ test('isTouchingSprite', t => {
});
test('isTouchingColor', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -297,8 +298,8 @@ test('isTouchingColor', t => {
});
test('colorIsTouchingColor', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -308,8 +309,8 @@ test('colorIsTouchingColor', t => {
});
test('layers', t => { // TODO this tests fake functionality. Move layering tests into Render.
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
const o = new Object();
r.attachRenderer(renderer);
@ -332,8 +333,8 @@ test('layers', t => { // TODO this tests fake functionality. Move layering tests
});
test('getLayerOrder returns result of renderer getDrawableOrder or null if renderer is not attached', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
// getLayerOrder should return null if there is no renderer attached to the runtime
@ -349,8 +350,8 @@ test('getLayerOrder returns result of renderer getDrawableOrder or null if rende
});
test('keepInFence', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const renderer = new FakeRenderer();
r.attachRenderer(renderer);
const a = new RenderedTarget(s, r);
@ -363,8 +364,8 @@ test('keepInFence', t => {
});
test('#stopAll clears graphics effects', t => {
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
const a = new RenderedTarget(s, r);
const effectName = 'brightness';
a.setEffect(effectName, 100);
@ -374,8 +375,9 @@ test('#stopAll clears graphics effects', t => {
});
test('#getCostumes returns the costumes', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
a.sprite.costumes = [{id: 1}, {id: 2}, {id: 3}];
t.equals(a.getCostumes().length, 3);
t.equals(a.getCostumes()[0].id, 1);
@ -385,8 +387,9 @@ test('#getCostumes returns the costumes', t => {
});
test('#getSounds returns the sounds', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
const sounds = [1, 2, 3];
a.sprite.sounds = sounds;
t.equals(a.getSounds(), sounds);
@ -394,8 +397,9 @@ test('#getSounds returns the sounds', t => {
});
test('#toJSON returns the sounds and costumes', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
const sounds = [1, 2, 3];
a.sprite.sounds = sounds;
a.sprite.costumes = [{id: 1}, {id: 2}, {id: 3}];
@ -405,8 +409,9 @@ test('#toJSON returns the sounds and costumes', t => {
});
test('#addSound does not duplicate names', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
a.sprite.sounds = [{name: 'first'}];
a.addSound({name: 'first'});
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'first2'}]);
@ -414,8 +419,9 @@ test('#addSound does not duplicate names', t => {
});
test('#addCostume does not duplicate names', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
a.addCostume({name: 'first'});
a.addCostume({name: 'first'});
t.equal(a.sprite.costumes.length, 2);
@ -425,8 +431,9 @@ test('#addCostume does not duplicate names', t => {
});
test('#renameSound does not duplicate names', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
a.sprite.sounds = [{name: 'first'}, {name: 'second'}];
a.renameSound(0, 'first'); // Shouldn't increment the name, noop
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'second'}]);
@ -436,8 +443,9 @@ test('#renameSound does not duplicate names', t => {
});
test('#renameCostume does not duplicate names', t => {
const spr = new Sprite();
const a = new RenderedTarget(spr, null);
const r = new Runtime();
const spr = new Sprite(null, r);
const a = new RenderedTarget(spr, r);
a.sprite.costumes = [{name: 'first'}, {name: 'second'}];
a.renameCostume(0, 'first'); // Shouldn't increment the name, noop
t.equal(a.sprite.costumes.length, 2);
@ -456,8 +464,8 @@ test('#reorderCostume', t => {
const o3 = {id: 2};
const o4 = {id: 3};
const o5 = {id: 4};
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
s.costumes = [o1, o2, o3, o4, o5];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();
@ -510,8 +518,8 @@ test('#reorderSound', t => {
const o3 = {id: 2, name: 'name2'};
const o4 = {id: 3, name: 'name3'};
const o5 = {id: 4, name: 'name4'};
const s = new Sprite();
const r = new Runtime();
const s = new Sprite(null, r);
s.sounds = [o1, o2, o3, o4, o5];
const a = new RenderedTarget(s, r);
const renderer = new FakeRenderer();

View file

@ -14,9 +14,9 @@ const test = tap.test;
test('deleteSound returns function after deleting or null if nothing was deleted', t => {
const vm = new VirtualMachine();
const sprite = new Sprite();
sprite.sounds = [{id: 1}, {id: 2}, {id: 3}];
const rt = new Runtime();
const sprite = new Sprite(null, rt);
sprite.sounds = [{id: 1}, {id: 2}, {id: 3}];
const target = new RenderedTarget(sprite, rt);
vm.editingTarget = target;
@ -37,10 +37,10 @@ test('deleteSound returns function after deleting or null if nothing was deleted
test('deleteCostume returns function after deleting or null if nothing was deleted', t => {
const vm = new VirtualMachine();
const sprite = new Sprite();
const rt = new Runtime();
const sprite = new Sprite(null, rt);
sprite.costumes = [{id: 1}, {id: 2}, {id: 3}];
sprite.currentCostume = 0;
const rt = new Runtime();
const target = new RenderedTarget(sprite, rt);
vm.editingTarget = target;