2017-06-15 17:29:15 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Target = require('../../src/engine/target');
|
2017-11-13 16:30:47 -05:00
|
|
|
const Variable = require('../../src/engine/variable');
|
2017-06-15 17:29:15 -04:00
|
|
|
|
|
|
|
test('spec', t => {
|
|
|
|
const target = new Target();
|
|
|
|
|
|
|
|
t.type(Target, 'function');
|
|
|
|
t.type(target, 'object');
|
|
|
|
t.ok(target instanceof Target);
|
|
|
|
|
|
|
|
t.type(target.id, 'string');
|
|
|
|
t.type(target.blocks, 'object');
|
|
|
|
t.type(target.variables, 'object');
|
2018-05-31 16:33:42 -04:00
|
|
|
t.type(target.comments, 'object');
|
2017-06-15 17:29:15 -04:00
|
|
|
t.type(target._customState, 'object');
|
|
|
|
|
|
|
|
t.type(target.createVariable, 'function');
|
|
|
|
t.type(target.renameVariable, 'function');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create Variable tests.
|
|
|
|
test('createVariable', t => {
|
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
const variable = variables[Object.keys(variables)[0]];
|
|
|
|
t.equal(variable.id, 'foo');
|
|
|
|
t.equal(variable.name, 'bar');
|
2017-11-13 16:30:47 -05:00
|
|
|
t.equal(variable.type, Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
t.equal(variable.value, 0);
|
|
|
|
t.equal(variable.isCloud, false);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create Same Variable twice.
|
|
|
|
test('createVariable2', t => {
|
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
|
|
|
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-11-13 14:25:12 -05:00
|
|
|
// Create a list
|
2017-11-13 16:30:47 -05:00
|
|
|
test('createListVariable creates a list', t => {
|
2017-11-13 14:25:12 -05:00
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
|
2017-11-13 14:25:12 -05:00
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
const variable = variables[Object.keys(variables)[0]];
|
|
|
|
t.equal(variable.id, 'foo');
|
|
|
|
t.equal(variable.name, 'bar');
|
2017-11-13 16:30:47 -05:00
|
|
|
t.equal(variable.type, Variable.LIST_TYPE);
|
2017-11-13 14:25:12 -05:00
|
|
|
t.assert(variable.value instanceof Array, true);
|
|
|
|
t.equal(variable.value.length, 0);
|
|
|
|
t.equal(variable.isCloud, false);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-11-13 16:30:47 -05:00
|
|
|
test('createVariable throws when given invalid type', t => {
|
|
|
|
const target = new Target();
|
|
|
|
t.throws(
|
|
|
|
(() => target.createVariable('foo', 'bar', 'baz')),
|
|
|
|
new Error('Invalid variable type: baz')
|
|
|
|
);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-06-15 17:29:15 -04:00
|
|
|
// Rename Variable tests.
|
|
|
|
test('renameVariable', t => {
|
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
target.renameVariable('foo', 'bar2');
|
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
const variable = variables[Object.keys(variables)[0]];
|
|
|
|
t.equal(variable.id, 'foo');
|
|
|
|
t.equal(variable.name, 'bar2');
|
|
|
|
t.equal(variable.value, 0);
|
|
|
|
t.equal(variable.isCloud, false);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Rename Variable that doesn't exist.
|
|
|
|
test('renameVariable2', t => {
|
|
|
|
const target = new Target();
|
|
|
|
target.renameVariable('foo', 'bar2');
|
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Rename Variable that with id that exists as another variable's name.
|
|
|
|
// Expect no change.
|
|
|
|
test('renameVariable3', t => {
|
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo1', 'foo', Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
target.renameVariable('foo', 'bar2');
|
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
const variable = variables[Object.keys(variables)[0]];
|
|
|
|
t.equal(variable.id, 'foo1');
|
|
|
|
t.equal(variable.name, 'foo');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete Variable tests.
|
|
|
|
test('deleteVariable', t => {
|
|
|
|
const target = new Target();
|
2017-11-13 16:30:47 -05:00
|
|
|
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
|
2017-06-15 17:29:15 -04:00
|
|
|
target.deleteVariable('foo');
|
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Delete Variable that doesn't exist.
|
|
|
|
test('deleteVariable2', t => {
|
|
|
|
const target = new Target();
|
|
|
|
target.deleteVariable('foo');
|
|
|
|
|
|
|
|
const variables = target.variables;
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2017-11-13 16:30:47 -05:00
|
|
|
|
|
|
|
test('lookupOrCreateList creates a list if var with given id does not exist', t => {
|
|
|
|
const target = new Target();
|
|
|
|
const variables = target.variables;
|
|
|
|
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
const listVar = target.lookupOrCreateList('foo', 'bar');
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
t.equal(listVar.id, 'foo');
|
|
|
|
t.equal(listVar.name, 'bar');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('lookupOrCreateList returns list if one with given id exists', t => {
|
|
|
|
const target = new Target();
|
|
|
|
const variables = target.variables;
|
|
|
|
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
|
|
|
|
const listVar = target.lookupOrCreateList('foo', 'bar');
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
t.equal(listVar.id, 'foo');
|
|
|
|
t.equal(listVar.name, 'bar');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2017-11-29 10:43:07 -05:00
|
|
|
|
2017-12-04 18:01:29 -05:00
|
|
|
test('lookupBroadcastMsg returns the var with given id if exists', t => {
|
2017-11-29 10:43:07 -05:00
|
|
|
const target = new Target();
|
|
|
|
const variables = target.variables;
|
|
|
|
|
|
|
|
t.equal(Object.keys(variables).length, 0);
|
|
|
|
target.createVariable('foo', 'bar', Variable.BROADCAST_MESSAGE_TYPE);
|
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
|
2017-12-04 18:01:29 -05:00
|
|
|
const broadcastMsg = target.lookupBroadcastMsg('foo', 'bar');
|
2017-11-29 10:43:07 -05:00
|
|
|
t.equal(Object.keys(variables).length, 1);
|
|
|
|
t.equal(broadcastMsg.id, 'foo');
|
|
|
|
t.equal(broadcastMsg.name, 'bar');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2018-06-12 18:29:43 -04:00
|
|
|
|
|
|
|
test('createComment adds a comment to the target', t => {
|
|
|
|
const target = new Target();
|
|
|
|
const comments = target.comments;
|
|
|
|
|
|
|
|
t.equal(Object.keys(comments).length, 0);
|
|
|
|
target.createComment('a comment', null, 'some comment text',
|
|
|
|
10, 20, 200, 300, true);
|
|
|
|
t.equal(Object.keys(comments).length, 1);
|
|
|
|
|
|
|
|
const comment = comments['a comment'];
|
|
|
|
t.notEqual(comment, null);
|
|
|
|
t.equal(comment.blockId, null);
|
|
|
|
t.equal(comment.text, 'some comment text');
|
|
|
|
t.equal(comment.x, 10);
|
|
|
|
t.equal(comment.y, 20);
|
|
|
|
t.equal(comment.width, 200);
|
|
|
|
t.equal(comment.height, 300);
|
|
|
|
t.equal(comment.minimized, true);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('creating comment with id that already exists does not change existing comment', t => {
|
|
|
|
const target = new Target();
|
|
|
|
const comments = target.comments;
|
|
|
|
|
|
|
|
t.equal(Object.keys(comments).length, 0);
|
|
|
|
target.createComment('a comment', null, 'some comment text',
|
|
|
|
10, 20, 200, 300, true);
|
|
|
|
t.equal(Object.keys(comments).length, 1);
|
|
|
|
|
|
|
|
target.createComment('a comment', null,
|
|
|
|
'some new comment text', 40, 50, 300, 400, false);
|
|
|
|
|
|
|
|
const comment = comments['a comment'];
|
|
|
|
t.notEqual(comment, null);
|
|
|
|
// All of the comment properties should remain unchanged from the first
|
|
|
|
// time createComment was called
|
|
|
|
t.equal(comment.blockId, null);
|
|
|
|
t.equal(comment.text, 'some comment text');
|
|
|
|
t.equal(comment.x, 10);
|
|
|
|
t.equal(comment.y, 20);
|
|
|
|
t.equal(comment.width, 200);
|
|
|
|
t.equal(comment.height, 300);
|
|
|
|
t.equal(comment.minimized, true);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('creating a comment with a blockId also updates the comment property on the block', t => {
|
|
|
|
const target = new Target();
|
|
|
|
const comments = target.comments;
|
|
|
|
// Create a mock block on the target
|
|
|
|
target.blocks = {
|
|
|
|
'a mock block': {
|
|
|
|
id: 'a mock block'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the getBlock function that's used in commentCreate
|
|
|
|
target.blocks.getBlock = id => target.blocks[id];
|
|
|
|
|
|
|
|
t.equal(Object.keys(comments).length, 0);
|
|
|
|
target.createComment('a comment', 'a mock block', 'some comment text',
|
|
|
|
10, 20, 200, 300, true);
|
|
|
|
t.equal(Object.keys(comments).length, 1);
|
|
|
|
|
|
|
|
const comment = comments['a comment'];
|
|
|
|
t.equal(comment.blockId, 'a mock block');
|
|
|
|
t.equal(target.blocks.getBlock('a mock block').comment, 'a comment');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|