Updating tests for refactoring lists. Updating demo fixture -- now we have IDs and variable types for variables.

This commit is contained in:
Karishma Chadha 2017-11-13 16:30:47 -05:00
parent 26d4a3a069
commit 3f444138db
2 changed files with 51 additions and 11 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,6 @@
const test = require('tap').test;
const Target = require('../../src/engine/target');
const Variable = require('../../src/engine/variable');
test('spec', t => {
const target = new Target();
@ -23,14 +24,14 @@ test('spec', t => {
// Create Variable tests.
test('createVariable', t => {
const target = new Target();
target.createVariable('foo', 'bar', '');
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
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');
t.equal(variable.type, '');
t.equal(variable.type, Variable.SCALAR_TYPE);
t.equal(variable.value, 0);
t.equal(variable.isCloud, false);
@ -40,8 +41,8 @@ test('createVariable', t => {
// Create Same Variable twice.
test('createVariable2', t => {
const target = new Target();
target.createVariable('foo', 'bar', '');
target.createVariable('foo', 'bar', '');
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
@ -50,16 +51,16 @@ test('createVariable2', t => {
});
// Create a list
test('createVariable creates a list', t => {
test('createListVariable creates a list', t => {
const target = new Target();
target.createVariable('foo', 'bar', 'list');
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
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');
t.equal(variable.type, 'list');
t.equal(variable.type, Variable.LIST_TYPE);
t.assert(variable.value instanceof Array, true);
t.equal(variable.value.length, 0);
t.equal(variable.isCloud, false);
@ -67,10 +68,20 @@ test('createVariable creates a list', t => {
t.end();
});
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();
});
// Rename Variable tests.
test('renameVariable', t => {
const target = new Target();
target.createVariable('foo', 'bar', '');
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
@ -99,7 +110,7 @@ test('renameVariable2', t => {
// Expect no change.
test('renameVariable3', t => {
const target = new Target();
target.createVariable('foo1', 'foo', '');
target.createVariable('foo1', 'foo', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
@ -114,7 +125,7 @@ test('renameVariable3', t => {
// Delete Variable tests.
test('deleteVariable', t => {
const target = new Target();
target.createVariable('foo', 'bar', '');
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.deleteVariable('foo');
const variables = target.variables;
@ -133,3 +144,32 @@ test('deleteVariable2', t => {
t.end();
});
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();
});