From 773240910219034b0c73351d8f939316b4d61dcd Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Mon, 6 Jun 2016 15:59:21 -0400 Subject: [PATCH] Update unit tests for new adapter, blocks module --- test/unit/adapter.js | 26 +++++++++--- test/unit/blocks.js | 97 ++++++++++++++++++++++++++++++++++++++++++++ test/unit/runtime.js | 77 ----------------------------------- 3 files changed, 118 insertions(+), 82 deletions(-) create mode 100644 test/unit/blocks.js diff --git a/test/unit/adapter.js b/test/unit/adapter.js index 547e0b9b0..31e904845 100644 --- a/test/unit/adapter.js +++ b/test/unit/adapter.js @@ -10,11 +10,27 @@ test('spec', function (t) { test('create event', function (t) { var result = adapter(events.create); - t.type(result, 'object'); - t.type(result.id, 'string'); - t.type(result.opcode, 'string'); - t.type(result.fields, 'object'); - t.type(result.fields['DURATION'], 'object'); + 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'); + t.type(result[0].inputs['DURATION'], 'object'); + 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'); + t.type(result[1].fields['NUM'], 'object'); + t.type(result[1].fields['NUM'].value, '10'); + t.type(result[1].topLevel, 'boolean'); + t.equal(result[1].topLevel, false); t.end(); }); diff --git a/test/unit/blocks.js b/test/unit/blocks.js new file mode 100644 index 000000000..0e1bcabcc --- /dev/null +++ b/test/unit/blocks.js @@ -0,0 +1,97 @@ +var test = require('tap').test; +var Blocks = require('../../src/engine/Blocks'); + +test('spec', function (t) { + var b = new Blocks(); + + t.type(Blocks, 'function'); + t.type(b, 'object'); + t.ok(b instanceof Blocks); + + t.type(b._blocks, 'object'); + t.type(b._stacks, 'object'); + t.ok(Array.isArray(b._stacks)); + + t.type(b.createBlock, 'function'); + t.type(b.moveBlock, 'function'); + t.type(b.changeBlock, 'function'); + t.type(b.deleteBlock, 'function'); + + t.end(); +}); + +test('create', function (t) { + var b = new Blocks(); + b.createBlock({ + id: 'foo', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + + t.type(b._blocks['foo'], 'object'); + t.equal(b._blocks['foo'].opcode, 'TEST_BLOCK'); + t.notEqual(b._stacks.indexOf('foo'), -1); + t.end(); +}); + +test('move', function (t) { + var b = new Blocks(); + b.createBlock({ + id: 'foo', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + b.createBlock({ + id: 'bar', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + + // Attach 'bar' to the end of 'foo' + b.moveBlock({ + id: 'bar', + newParent: 'foo' + }); + t.equal(b._stacks.length, 1); + t.equal(Object.keys(b._blocks).length, 2); + t.equal(b._blocks['foo'].next, 'bar'); + + // Detach 'bar' from 'foo' + b.moveBlock({ + id: 'bar', + oldParent: 'foo' + }); + t.equal(b._stacks.length, 2); + t.equal(Object.keys(b._blocks).length, 2); + t.equal(b._blocks['foo'].next, null); + + t.end(); +}); + +test('delete', function (t) { + var b = new Blocks(); + b.createBlock({ + id: 'foo', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + b.deleteBlock({ + id: 'foo' + }); + + t.type(b._blocks['foo'], 'undefined'); + t.equal(b._stacks.indexOf('foo'), -1); + t.end(); +}); diff --git a/test/unit/runtime.js b/test/unit/runtime.js index 0e41ed5be..40757c054 100644 --- a/test/unit/runtime.js +++ b/test/unit/runtime.js @@ -8,82 +8,5 @@ test('spec', function (t) { t.type(r, 'object'); t.ok(r instanceof Runtime); - t.type(r.blocks, 'object'); - t.type(r.stacks, 'object'); - t.ok(Array.isArray(r.stacks)); - - t.type(r.createBlock, 'function'); - t.type(r.moveBlock, 'function'); - t.type(r.changeBlock, 'function'); - t.type(r.deleteBlock, 'function'); - - t.end(); -}); - -test('create', function (t) { - var r = new Runtime(); - r.createBlock({ - id: 'foo', - opcode: 'TEST_BLOCK', - next: null, - fields: {} - }); - - t.type(r.blocks['foo'], 'object'); - t.equal(r.blocks['foo'].opcode, 'TEST_BLOCK'); - t.notEqual(r.stacks.indexOf('foo'), -1); - t.end(); -}); - -test('move', function (t) { - var r = new Runtime(); - r.createBlock({ - id: 'foo', - opcode: 'TEST_BLOCK', - next: null, - fields: {} - }); - r.createBlock({ - id: 'bar', - opcode: 'TEST_BLOCK', - next: null, - fields: {} - }); - - // Attach 'bar' to the end of 'foo' - r.moveBlock({ - id: 'bar', - newParent: 'foo' - }); - t.equal(r.stacks.length, 1); - t.equal(Object.keys(r.blocks).length, 2); - t.equal(r.blocks['foo'].next, 'bar'); - - // Detach 'bar' from 'foo' - r.moveBlock({ - id: 'bar', - oldParent: 'foo' - }); - t.equal(r.stacks.length, 2); - t.equal(Object.keys(r.blocks).length, 2); - t.equal(r.blocks['foo'].next, null); - - t.end(); -}); - -test('delete', function (t) { - var r = new Runtime(); - r.createBlock({ - id: 'foo', - opcode: 'TEST_BLOCK', - next: null, - fields: {} - }); - r.deleteBlock({ - id: 'foo' - }); - - t.type(r.blocks['foo'], 'undefined'); - t.equal(r.stacks.indexOf('foo'), -1); t.end(); });