forked from anvaka/VivaGraphJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
616 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.