Skip to content

Commit

Permalink
Migrated to tap for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
anvaka committed Dec 28, 2014
1 parent 5eb9888 commit 8462e6c
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 674 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"es5": true,
"esnext": true,
"bitwise": false,
"camelcase": true,
"camelcase": false,
"curly": true,
"eqeqeq": true,
"immed": true,
Expand Down
8 changes: 7 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var path = require('path');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var del = require('del');
var run = require('gulp-run');

var files = getFiles();

gulp.task('clean', clean);
gulp.task('build', concatenateFiles);
gulp.task('release', ['clean', 'build']);
gulp.task('test', test);
gulp.task('release', ['clean', 'build', 'test']);
gulp.task('default', watch);

function watch() {
Expand All @@ -20,6 +22,10 @@ function clean(cb) {
del(['dist'], cb);
}

function test() {
new run.Command('npm test').exec();
}

function concatenateFiles() {
gulp.src(files)
.pipe(concat('vivagraph.js'))
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"start": "gulp",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "tap unit_tests/*.js"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +24,10 @@
"gulp": "^3.8.10",
"gulp-concat": "^2.4.2",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^1.0.2"
"gulp-uglify": "^1.0.2",
"tap": "^0.4.13"
},
"dependencies": {
"gulp-run": "^1.6.5"
}
}
36 changes: 0 additions & 36 deletions unit_tests/testRunner.html

This file was deleted.

174 changes: 106 additions & 68 deletions unit_tests/test_Utils.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,106 @@
var test_Utils = function (test) {
return {
randomIteratorReturnsAllItems : function () {
var a = [1, 2, 3, 4, 5, 6],
aCopy = a.map(function (i) { return i; }),
shuffle = Viva.randomIterator(aCopy),
iterated = [];
shuffle.forEach(function (i) {
iterated.push(i);
test.assert(a.indexOf(i) !== -1, 'Shuffle iterator should return only items from original array. Unexpected ' + i);
});

test.assertEqual(iterated.length, a.length, 'Number of iterated items does not match number of original array items');
},

lazyExtendDoesNotExtendExistingValues : function () {
var options = { age : 42 };

Viva.lazyExtend(options, { age : 24 });

test.assertEqual(options.age, 42, 'Should not touch properties when types match');
},

lazyExtendUpdatesWhenTypeDoesNotMatch : function () {
var options = { age : '42' };

Viva.lazyExtend(options, { age : 24 });

test.assertEqual(options.age, 24, 'Should extend, because types are different');
},

lazyExtendUpdatesWhenNewProperty : function () {
var options = { age : '42' };

Viva.lazyExtend(options, { newProperty : 24 });

test.assertEqual(options.age, '42', 'Should preserve old values');
test.assertEqual(options.newProperty, 24, 'Should extend, because new property');
},

lazyExtendDeepNewObjects : function () {
var options = { age : '42' };

Viva.lazyExtend(options, { nested : { name : 'deep'} });

test.assertEqual(options.age, '42', 'Should preserve old values');
test.assertEqual(options.nested.name, 'deep', 'Should extend deep properties');
},

lazyExtendDeepLogic : function () {
var options = { age : '42', nested: { first : 'Mark', age : '22'}};

Viva.lazyExtend(options, { nested : { first : '', last : 'Twain', age : 20} });

test.assertEqual(options.age, '42', 'Should preserve old values');
test.assertEqual(options.nested.first, 'Mark', 'Should preserve deep properties with same types');
test.assertEqual(options.nested.last, 'Twain', 'Should create new deep properties');
test.assertEqual(options.nested.age, 20, 'Should fix deep properties with wrong types');
},

lazyExtendCreatesNewObject : function () {
var options,
extended = Viva.lazyExtend(options, {});

test.assert(extended, 'New object should be created');
}
};
};
var test = require('tap').test;
var Viva = require('../dist/vivagraph.js');

test('randomIteratorReturnsAllItems', function(t) {
var a = [1, 2, 3, 4, 5, 6],
aCopy = a.map(function(i) {
return i;
}),
shuffle = Viva.randomIterator(aCopy),
iterated = [];
shuffle.forEach(function(i) {
iterated.push(i);
t.ok(a.indexOf(i) !== -1, 'Shuffle iterator should return only items from original array. Unexpected ' + i);
});

t.equals(iterated.length, a.length, 'Number of iterated items does not match number of original array items');
t.end();
});

test('lazyExtendDoesNotExtendExistingValues', function(t) {
var options = {
age: 42
};

Viva.lazyExtend(options, {
age: 24
});

t.equals(options.age, 42, 'Should not touch properties when types match');
t.end();
});

test('lazyExtendUpdatesWhenTypeDoesNotMatch', function(t) {
var options = {
age: '42'
};

Viva.lazyExtend(options, {
age: 24
});

t.equals(options.age, 24, 'Should extend, because types are different');
t.end();
});

test('lazyExtendUpdatesWhenNewProperty', function(t) {
var options = {
age: '42'
};

Viva.lazyExtend(options, {
newProperty: 24
});

t.equals(options.age, '42', 'Should preserve old values');
t.equals(options.newProperty, 24, 'Should extend, because new property');
t.end();
});

test('lazyExtendDeepNewObjects', function(t) {
var options = {
age: '42'
};

Viva.lazyExtend(options, {
nested: {
name: 'deep'
}
});

t.equals(options.age, '42', 'Should preserve old values');
t.equals(options.nested.name, 'deep', 'Should extend deep properties');
t.end();
});

test('lazyExtendDeepLogic', function(t) {
var options = {
age: '42',
nested: {
first: 'Mark',
age: '22'
}
};

Viva.lazyExtend(options, {
nested: {
first: '',
last: 'Twain',
age: 20
}
});

t.equals(options.age, '42', 'Should preserve old values');
t.equals(options.nested.first, 'Mark', 'Should preserve deep properties with same types');
t.equals(options.nested.last, 'Twain', 'Should create new deep properties');
t.equals(options.nested.age, 20, 'Should fix deep properties with wrong types');
t.end();
});

test('lazyExtendCreatesNewObject', function(t) {
var options,
extended = Viva.lazyExtend(options, {});

t.ok(extended, 'New object should be created');
t.end();
});
107 changes: 54 additions & 53 deletions unit_tests/test_constantLayout.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,57 @@
/*global Viva*/

/**
* Testing Viva.Graph.Layout.constant behavior.
* Testing Viva.Graph.Layout.constant behavior.
*/
var test_constantLayout = function(test) {
return {
nodePositionGeneratedByDefault: function() {
var graph = Viva.Graph.generator().path(10),
layout = Viva.Graph.Layout.constant(graph);

layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
test.assert(position, 'All nodes expected to have some position');
test.assert(typeof position.x === 'number', 'Node position does not have a valid x position');
test.assert(typeof position.y === 'number', 'Node position does not have a valid y position');
});
},

nodePositionUsesCustomCallback: function() {
var graph = Viva.Graph.generator().path(10),
layout = Viva.Graph.Layout.constant(graph),
placeNodeCallback = function() {
return new Viva.Graph.Point2d(42, 42); // all nodes should be placed at the same position.
};

layout.placeNode(placeNodeCallback);
layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
test.assertEqual(position.x, 42, 'Node position does not have a valid x position');
test.assertEqual(position.y, 42, 'Node position does not have a valid y position');
});
},

getGraphRectReflectsDefaultSettings: function() {
var graph = Viva.Graph.generator().path(10),
layoutSettings = {
maxX: 42,
maxY: 42
},
layout = Viva.Graph.Layout.constant(graph, layoutSettings);

layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
test.assert(position.x <= layoutSettings.maxX, 'Node position does not have a valid x position');
test.assert(position.y <= layoutSettings.maxY, 'Node position does not have a valid y position');
});
}

var test = require('tap').test;
var Viva = require('../dist/vivagraph.js');

test('nodePositionGeneratedByDefault', function(t) {
var graph = Viva.Graph.generator().path(10),
layout = Viva.Graph.Layout.constant(graph);

layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
t.ok(position, 'All nodes expected to have some position');
t.ok(typeof position.x === 'number', 'Node position does not have a valid x position');
t.ok(typeof position.y === 'number', 'Node position does not have a valid y position');
});
t.end();
});

test('nodePositionUsesCustomCallback', function(t) {
var graph = Viva.Graph.generator().path(10),
layout = Viva.Graph.Layout.constant(graph),
placeNodeCallback = function() {
return new Viva.Graph.Point2d(42, 42); // all nodes should be placed at the same position.
};
};

layout.placeNode(placeNodeCallback);
layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
t.equals(position.x, 42, 'Node position does not have a valid x position');
t.equals(position.y, 42, 'Node position does not have a valid y position');
});
t.end();
});

test('getGraphRectReflectsDefaultSettings', function(t) {
var graph = Viva.Graph.generator().path(10),
layoutSettings = {
maxX: 42,
maxY: 42
},
layout = Viva.Graph.Layout.constant(graph, layoutSettings);

layout.run();

graph.forEachNode(function(node) {
var position = layout.getNodePosition(node.id);
t.ok(position.x <= layoutSettings.maxX, 'Node position does not have a valid x position');
t.ok(position.y <= layoutSettings.maxY, 'Node position does not have a valid y position');
});
t.end();
});
Loading

0 comments on commit 8462e6c

Please sign in to comment.