2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const adapter = require('../../src/engine/adapter');
|
|
|
|
const events = require('../fixtures/events.json');
|
2016-04-26 09:49:52 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('spec', t => {
|
2016-04-26 09:49:52 -04:00
|
|
|
t.type(adapter, 'function');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('invalid inputs', t => {
|
|
|
|
let nothing = adapter('not an object');
|
2016-06-07 11:04:44 -04:00
|
|
|
t.type(nothing, 'undefined');
|
2016-10-23 12:41:45 -04:00
|
|
|
nothing = adapter({noxmlproperty: true});
|
2016-06-07 11:04:44 -04:00
|
|
|
t.type(nothing, 'undefined');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create event', t => {
|
|
|
|
const result = adapter(events.create);
|
2016-04-26 09:49:52 -04:00
|
|
|
|
2016-06-06 15:59:21 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 2);
|
|
|
|
|
|
|
|
// Outer block
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].inputs, 'object');
|
2016-10-23 12:41:45 -04:00
|
|
|
t.type(result[0].inputs.DURATION, 'object');
|
2016-06-06 15:59:21 -04:00
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
|
|
|
|
|
|
|
// Enclosed shadow block
|
|
|
|
t.type(result[1].id, 'string');
|
|
|
|
t.type(result[1].opcode, 'string');
|
|
|
|
t.type(result[1].fields, 'object');
|
|
|
|
t.type(result[1].inputs, 'object');
|
2016-10-23 12:41:45 -04:00
|
|
|
t.type(result[1].fields.NUM, 'object');
|
|
|
|
t.type(result[1].fields.NUM.value, '10');
|
2016-06-06 15:59:21 -04:00
|
|
|
t.type(result[1].topLevel, 'boolean');
|
|
|
|
t.equal(result[1].topLevel, false);
|
2016-04-26 09:49:52 -04:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2016-06-07 10:49:12 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with branch', t => {
|
|
|
|
const result = adapter(events.createbranch);
|
2016-06-07 10:49:12 -04:00
|
|
|
// Outer block
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].inputs, 'object');
|
2016-10-23 12:41:45 -04:00
|
|
|
t.type(result[0].inputs.SUBSTACK, 'object');
|
2016-06-07 10:49:12 -04:00
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
2016-08-10 11:27:21 -04:00
|
|
|
// In branch
|
2017-04-20 19:17:05 -04:00
|
|
|
const branchBlockId = result[0].inputs.SUBSTACK.block;
|
|
|
|
const branchShadowId = result[0].inputs.SUBSTACK.shadow;
|
2016-08-10 11:27:21 -04:00
|
|
|
t.type(branchBlockId, 'string');
|
2016-09-06 10:55:52 -04:00
|
|
|
t.equal(branchShadowId, null);
|
2016-08-10 11:27:21 -04:00
|
|
|
// Find actual branch block
|
2017-04-20 19:17:05 -04:00
|
|
|
let branchBlock = null;
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (result[i].id === branchBlockId) {
|
2016-08-10 11:27:21 -04:00
|
|
|
branchBlock = result[i];
|
2016-06-07 10:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-10 11:27:21 -04:00
|
|
|
t.type(branchBlock, 'object');
|
2016-06-07 10:49:12 -04:00
|
|
|
t.end();
|
|
|
|
});
|
2016-06-07 11:04:44 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with two branches', t => {
|
|
|
|
const result = adapter(events.createtwobranches);
|
2016-06-07 11:04:44 -04:00
|
|
|
// Outer block
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].inputs, 'object');
|
2016-10-23 12:41:45 -04:00
|
|
|
t.type(result[0].inputs.SUBSTACK, 'object');
|
|
|
|
t.type(result[0].inputs.SUBSTACK2, 'object');
|
2016-06-07 11:04:44 -04:00
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
2016-08-10 11:27:21 -04:00
|
|
|
// In branchs
|
2017-04-20 19:17:05 -04:00
|
|
|
const firstBranchBlockId = result[0].inputs.SUBSTACK.block;
|
|
|
|
const secondBranchBlockId = result[0].inputs.SUBSTACK2.block;
|
2016-08-10 11:27:21 -04:00
|
|
|
t.type(firstBranchBlockId, 'string');
|
|
|
|
t.type(secondBranchBlockId, 'string');
|
2017-04-20 19:17:05 -04:00
|
|
|
const firstBranchShadowBlockId = result[0].inputs.SUBSTACK.shadow;
|
|
|
|
const secondBranchShadowBlockId = result[0].inputs.SUBSTACK2.shadow;
|
2016-09-06 10:55:52 -04:00
|
|
|
t.equal(firstBranchShadowBlockId, null);
|
|
|
|
t.equal(secondBranchShadowBlockId, null);
|
2016-08-10 11:27:21 -04:00
|
|
|
// Find actual branch blocks
|
2017-04-20 19:17:05 -04:00
|
|
|
let firstBranchBlock = null;
|
|
|
|
let secondBranchBlock = null;
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
2016-10-23 17:55:31 -04:00
|
|
|
if (result[i].id === firstBranchBlockId) {
|
2016-08-10 11:27:21 -04:00
|
|
|
firstBranchBlock = result[i];
|
2016-06-07 11:04:44 -04:00
|
|
|
}
|
2016-10-23 17:55:31 -04:00
|
|
|
if (result[i].id === secondBranchBlockId) {
|
2016-08-10 11:27:21 -04:00
|
|
|
secondBranchBlock = result[i];
|
2016-06-07 11:04:44 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-10 11:27:21 -04:00
|
|
|
t.type(firstBranchBlock, 'object');
|
|
|
|
t.type(secondBranchBlock, 'object');
|
2016-06-07 11:04:44 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with top-level shadow', t => {
|
|
|
|
const result = adapter(events.createtoplevelshadow);
|
2016-06-07 11:04:44 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 1);
|
|
|
|
|
|
|
|
// Outer block
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].inputs, 'object');
|
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with next connection', t => {
|
|
|
|
const result = adapter(events.createwithnext);
|
2016-06-07 11:04:44 -04:00
|
|
|
|
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 2);
|
|
|
|
|
|
|
|
// First block
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].inputs, 'object');
|
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
|
|
|
t.type(result[0].next, 'string');
|
|
|
|
t.equal(result[0].next, result[1].id);
|
|
|
|
|
|
|
|
// Second block
|
|
|
|
t.type(result[1].id, 'string');
|
|
|
|
t.type(result[1].opcode, 'string');
|
|
|
|
t.type(result[1].fields, 'object');
|
|
|
|
t.type(result[1].inputs, 'object');
|
|
|
|
t.type(result[1].topLevel, 'boolean');
|
|
|
|
t.equal(result[1].topLevel, false);
|
|
|
|
t.equal(result[1].next, null);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2016-06-07 12:01:47 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with obscured shadow', t => {
|
|
|
|
const result = adapter(events.createobscuredshadow);
|
2016-09-06 10:55:52 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 4);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-10-08 18:19:55 -04:00
|
|
|
test('create variable with entity in name', t => {
|
|
|
|
const result = adapter(events.createvariablewithentity);
|
|
|
|
|
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 1);
|
|
|
|
|
|
|
|
t.type(result[0].id, 'string');
|
|
|
|
t.type(result[0].opcode, 'string');
|
|
|
|
t.type(result[0].fields, 'object');
|
|
|
|
t.type(result[0].fields.VARIABLE, 'object');
|
|
|
|
t.type(result[0].fields.VARIABLE.value, 'string');
|
|
|
|
t.equal(result[0].fields.VARIABLE.value, 'this & that');
|
|
|
|
t.type(result[0].inputs, 'object');
|
|
|
|
t.type(result[0].topLevel, 'boolean');
|
|
|
|
t.equal(result[0].topLevel, true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with invalid block xml', t => {
|
2016-06-07 12:01:47 -04:00
|
|
|
// Entirely invalid block XML
|
2017-04-20 19:17:05 -04:00
|
|
|
const result = adapter(events.createinvalid);
|
2016-06-07 12:01:47 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 0);
|
|
|
|
|
|
|
|
// Invalid grandchild tag
|
2017-04-20 19:17:05 -04:00
|
|
|
const result2 = adapter(events.createinvalidgrandchild);
|
2016-06-07 12:01:47 -04:00
|
|
|
t.ok(Array.isArray(result2));
|
|
|
|
t.equal(result2.length, 1);
|
|
|
|
t.type(result2[0].id, 'string');
|
|
|
|
t.equal(Object.keys(result2[0].inputs).length, 0);
|
|
|
|
t.equal(Object.keys(result2[0].fields).length, 0);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with invalid xml', t => {
|
|
|
|
const result = adapter(events.createbadxml);
|
2016-06-07 12:01:47 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 0);
|
|
|
|
t.end();
|
|
|
|
});
|
2016-06-14 18:14:15 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('create with empty field', t => {
|
|
|
|
const result = adapter(events.createemptyfield);
|
2016-06-14 18:14:15 -04:00
|
|
|
t.ok(Array.isArray(result));
|
|
|
|
t.equal(result.length, 3);
|
|
|
|
t.end();
|
|
|
|
});
|