Update tests to use new function() {} notation for automatic rendering of messages.

This commit is contained in:
Jürg Lehni 2011-05-07 17:46:06 +01:00
parent 20409f3084
commit 520f5054e5
10 changed files with 308 additions and 115 deletions

View file

@ -6,8 +6,12 @@ function equals(actual, expected, message) {
message = actual.toString().match(
/^\s*function[^\{]*\{([\s\S]*)\}\s*$/)[1]
.replace(/ /g, '')
.replace(/^\s+|\s+$/g, '')
.replace(/^return /, '');
.replace(/^\s+|\s+$/g, '');
if (/^return /.test(message)) {
message = message
.replace(/^return /, '')
.replace(/;$/, '');
}
}
actual = actual();
}

View file

@ -18,5 +18,7 @@ test('moveTo / lineTo', function() {
path.fillColor = 'black';
equals(path.children.length, 2);
equals(function() {
return path.children.length;
}, 2));
});

View file

@ -5,6 +5,10 @@ test('activate()', function() {
var secondDoc = new Document();
doc.activate();
var path = new Path();
equals(doc.activeLayer.children[0] == path, true);
equals(secondDoc.activeLayer.children.length == 0, true);
equals(function() {
return doc.activeLayer.children[0] == path;
}, true);
equals(function() {
return secondDoc.activeLayer.children.length == 0;
}, true);
});

View file

@ -3,15 +3,21 @@ module('Group');
test('new Group()', function() {
var doc = new Document();
var group = new Group();
equals(doc.activeLayer.children[0] == group, true);
equals(function() {
return doc.activeLayer.children[0] == group;
}, true);
});
test('new Group([item])', function() {
var doc = new Document();
var path = new Path();
var group = new Group([path]);
equals(doc.activeLayer.children.length == 1, true);
equals(group.children[0] == path, true);
equals(function() {
return doc.activeLayer.children.length == 1;
}, true);
equals(function() {
return group.children[0] == path;
}, true);
});
test('Group bounds', function() {

View file

@ -5,9 +5,15 @@ test('copyTo(document)', function() {
var path = new Path();
var secondDoc = new Document();
var copy = path.copyTo(secondDoc);
equals(secondDoc.activeLayer.children.indexOf(copy) != -1, true);
equals(doc.activeLayer.children.indexOf(copy) == -1, true);
equals(copy != path, true);
equals(function() {
return secondDoc.activeLayer.children.indexOf(copy) != -1;
}, true);
equals(function() {
return doc.activeLayer.children.indexOf(copy) == -1;
}, true);
equals(function() {
return copy != path;
}, true);
});
test('copyTo(layer)', function() {
@ -16,23 +22,33 @@ test('copyTo(layer)', function() {
var layer = new Layer();
var copy = path.copyTo(layer);
equals(layer.children.indexOf(copy) != -1, true);
equals(doc.layers[0].children.indexOf(copy) == -1, true);
equals(function() {
return layer.children.indexOf(copy) != -1;
}, true);
equals(function() {
return doc.layers[0].children.indexOf(copy) == -1;
}, true);
});
test('clone()', function() {
var doc = new Document();
var path = new Path();
var copy = path.clone();
equals(doc.activeLayer.children.length == 2, true);
equals(path != copy, true);
equals(function() {
return doc.activeLayer.children.length == 2;
}, true);
equals(function() {
return path != copy;
}, true);
});
test('appendChild(item)', function() {
var doc = new Document();
var path = new Path();
doc.activeLayer.appendChild(path);
equals(doc.activeLayer.children.length, 1);
equals(function() {
return doc.activeLayer.children.length;
}, 1);
});
test('item.parent / item.isChild / item.isParent', function() {
@ -40,23 +56,40 @@ test('item.parent / item.isChild / item.isParent', function() {
var secondDoc = new Document();
var path = new Path();
doc.activeLayer.appendChild(path);
equals(doc.activeLayer.children.indexOf(path) != -1, true);
equals(function() {
return doc.activeLayer.children.indexOf(path) != -1;
}, true);
secondDoc.activeLayer.appendTop(path);
equals(doc.activeLayer.isChild(path), false);
equals(path.isParent(doc.activeLayer), false);
equals(secondDoc.activeLayer.isChild(path), true);
equals(path.isParent(secondDoc.activeLayer), true);
equals(doc.activeLayer.children.indexOf(path) == -1, true);
equals(secondDoc.activeLayer.children.indexOf(path) == 0, true);
equals(function() {
return doc.activeLayer.isChild(path);
}, false);
equals(function() {
return path.isParent(doc.activeLayer);
}, false);
equals(function() {
return secondDoc.activeLayer.isChild(path);
}, true);
equals(function() {
return path.isParent(secondDoc.activeLayer);
}, true);
equals(function() {
return doc.activeLayer.children.indexOf(path) == -1;
}, true);
equals(function() {
return secondDoc.activeLayer.children.indexOf(path) == 0;
}, true);
});
test('item.lastChild / item.firstChild', function() {
var doc = new Document();
var path = new Path();
var secondPath = new Path();
equals(doc.activeLayer.firstChild == path, true);
equals(doc.activeLayer.lastChild == secondPath, true);
equals(function() {
return doc.activeLayer.firstChild == path;
}, true);
equals(function() {
return doc.activeLayer.lastChild == secondPath;
}, true);
});
test('appendBottom(item)', function() {
@ -64,7 +97,9 @@ test('appendBottom(item)', function() {
var path = new Path();
var secondPath = new Path();
doc.activeLayer.appendBottom(secondPath);
equals(secondPath.index < path.index, true);
equals(function() {
return secondPath.index < path.index;
}, true);
});
test('moveAbove(item)', function() {
@ -72,32 +107,48 @@ test('moveAbove(item)', function() {
var path = new Path();
var secondPath = new Path();
path.moveAbove(secondPath);
equals(doc.activeLayer.lastChild == path, true);
equals(function() {
return doc.activeLayer.lastChild == path;
}, true);
});
test('moveBelow(item)', function() {
var doc = new Document();
var firstPath = new Path();
var secondPath = new Path();
equals(secondPath.index > firstPath.index, true);
equals(function() {
return secondPath.index > firstPath.index;
}, true);
secondPath.moveBelow(firstPath);
equals(secondPath.index < firstPath.index, true);
equals(function() {
return secondPath.index < firstPath.index;
}, true);
});
test('isDescendant(item) / isAncestor(item)', function() {
var doc = new Document();
var path = new Path();
equals(path.isDescendant(doc.activeLayer), true);
equals(doc.activeLayer.isDescendant(path), false);
equals(path.isAncestor(doc.activeLayer), false);
equals(doc.activeLayer.isAncestor(path), true);
equals(function() {
return path.isDescendant(doc.activeLayer);
}, true);
equals(function() {
return doc.activeLayer.isDescendant(path);
}, false);
equals(function() {
return path.isAncestor(doc.activeLayer);
}, false);
equals(function() {
return doc.activeLayer.isAncestor(path);
}, true);
// an item can't be its own descendant:
equals(doc.activeLayer.isDescendant(doc.activeLayer), false);
equals(function() {
return doc.activeLayer.isDescendant(doc.activeLayer);
}, false);
// an item can't be its own ancestor:
equals(doc.activeLayer.isAncestor(doc.activeLayer), false);
equals(function() {
return doc.activeLayer.isAncestor(doc.activeLayer);
}, false);
});
test('isGroupedWith', function() {
@ -107,34 +158,60 @@ test('isGroupedWith', function() {
var group = new Group([path]);
var secondGroup = new Group([secondPath]);
equals(path.isGroupedWith(secondPath), false);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
secondGroup.appendTop(path);
equals(path.isGroupedWith(secondPath), true);
equals(path.isGroupedWith(group), false);
equals(path.isDescendant(secondGroup), true);
equals(secondGroup.isDescendant(path), false);
equals(secondGroup.isDescendant(secondGroup), false);
equals(path.isGroupedWith(secondGroup), false);
equals(function() {
return path.isGroupedWith(secondPath);
}, true);
equals(function() {
return path.isGroupedWith(group);
}, false);
equals(function() {
return path.isDescendant(secondGroup);
}, true);
equals(function() {
return secondGroup.isDescendant(path);
}, false);
equals(function() {
return secondGroup.isDescendant(secondGroup);
}, false);
equals(function() {
return path.isGroupedWith(secondGroup);
}, false);
paper.document.activeLayer.appendTop(path);
equals(path.isGroupedWith(secondPath), false);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
paper.document.activeLayer.appendTop(secondPath);
equals(path.isGroupedWith(secondPath), false);
equals(function() {
return path.isGroupedWith(secondPath);
}, false);
});
test('getPreviousSibling() / getNextSibling()', function() {
var doc = new Document();
var firstPath = new Path();
var secondPath = new Path();
equals(firstPath.nextSibling == secondPath, true);
equals(secondPath.previousSibling == firstPath, true);
equals(secondPath.nextSibling == null, true);
equals(function() {
return firstPath.nextSibling == secondPath;
}, true);
equals(function() {
return secondPath.previousSibling == firstPath;
}, true);
equals(function() {
return secondPath.nextSibling == null;
}, true);
});
test('hidden', function() {
var doc = new Document();
var firstPath = new Path();
firstPath.visible = false;
equals(firstPath.hidden, true);
equals(function() {
return firstPath.hidden;
}, true);
});
test('reverseChildren()', function() {
@ -142,11 +219,19 @@ test('reverseChildren()', function() {
var path = new Path();
var secondPath = new Path();
var thirdPath = new Path();
equals(doc.activeLayer.firstChild == path, true);
equals(function() {
return doc.activeLayer.firstChild == path;
}, true);
doc.activeLayer.reverseChildren();
equals(doc.activeLayer.firstChild == path, false);
equals(doc.activeLayer.firstChild == thirdPath, true);
equals(doc.activeLayer.lastChild == path, true);
equals(function() {
return doc.activeLayer.firstChild == path;
}, false);
equals(function() {
return doc.activeLayer.firstChild == thirdPath;
}, true);
equals(function() {
return doc.activeLayer.lastChild == path;
}, true);
});
test('Check item#document when moving items across documents', function() {
@ -155,13 +240,19 @@ test('Check item#document when moving items across documents', function() {
var group = new Group();
group.appendTop(new Path());
equals(path.document == doc1, true);
equals(function() {
return path.document == doc1;
}, true);
var doc2 = new Document();
doc2.activeLayer.appendTop(path);
equals(path.document == doc2, true);
equals(function() {
return path.document == doc2;
}, true);
doc2.activeLayer.appendTop(group);
equals(group.children[0].document == doc2, true);
equals(function() {
return group.children[0].document == doc2;
}, true);
});
test('group.selected', function() {
@ -170,16 +261,27 @@ test('group.selected', function() {
var path2 = new Path([0, 0]);
var group = new Group([path, path2]);
path.selected = true;
equals(group.selected, true);
equals(function() {
return group.selected;
}, true);
path.selected = false;
equals(group.selected, false);
equals(function() {
return group.selected;
}, false);
group.selected = true;
equals(path.selected, true);
equals(path2.selected, true);
equals(function() {
return path.selected;
}, true);
equals(function() {
return path2.selected;
}, true);
group.selected = false;
equals(path.selected, false);
equals(path2.selected, false);
});
equals(function() {
return path.selected;
}, false);
equals(function() {
return path2.selected;
}, false);});

View file

@ -4,23 +4,39 @@ test('previousSibling / nextSibling', function() {
var doc = new Document();
var firstLayer = doc.activeLayer;
var secondLayer = new Layer();
equals(secondLayer.previousSibling == firstLayer, true);
equals(secondLayer.nextSibling == null, true);
equals(function() {
return secondLayer.previousSibling == firstLayer;
}, true);
equals(function() {
return secondLayer.nextSibling == null;
}, true);
// Move another layer into secondLayer and check nextSibling /
// previousSibling:
var path = new Path();
var thirdLayer = new Layer();
secondLayer.appendBottom(thirdLayer);
equals(secondLayer.children.length, 2);
equals(thirdLayer.nextSibling == path, true);
equals(function() {
return secondLayer.children.length;
}, 2);
equals(function() {
return thirdLayer.nextSibling == path;
}, true);
secondLayer.appendTop(thirdLayer);
equals(thirdLayer.nextSibling == null, true);
equals(thirdLayer.previousSibling == path, true);
equals(doc.layers.length == 2, true);
equals(function() {
return thirdLayer.nextSibling == null;
}, true);
equals(function() {
return thirdLayer.previousSibling == path;
}, true);
equals(function() {
return doc.layers.length == 2;
}, true);
firstLayer.appendTop(secondLayer);
equals(doc.layers.length == 1, true);
equals(function() {
return doc.layers.length == 1;
}, true);
});
test('moveAbove / moveBelow', function() {
@ -28,17 +44,26 @@ test('moveAbove / moveBelow', function() {
var firstLayer = doc.activeLayer;
var secondLayer = new Layer();
secondLayer.moveBelow(firstLayer);
equals(secondLayer.previousSibling == null, true);
equals(secondLayer.nextSibling == firstLayer, true);
equals(function() {
return secondLayer.previousSibling == null;
}, true);
equals(function() {
return secondLayer.nextSibling == firstLayer;
}, true);
var path = new Path();
firstLayer.appendTop(path);
// move the layer above the path, inside the firstLayer:
secondLayer.moveAbove(path);
equals(secondLayer.previousSibling == path, true);
equals(secondLayer.parent == firstLayer, true);
equals(function() {
return secondLayer.previousSibling == path;
}, true);
equals(function() {
return secondLayer.parent == firstLayer;
}, true);
// There should now only be one layer left:
equals(doc.layers.length, 1);
equals(function() {
return doc.layers.length;
}, 1);
});

View file

@ -12,7 +12,9 @@ test('path.join(path)', function() {
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }');
equals(doc.activeLayer.children.length, 1);
equals(function() {
return doc.activeLayer.children.length;
}, 1);
var path = new Path();
path.add(0, 0);
@ -47,7 +49,9 @@ test('path.join(path)', function() {
path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } },{ point: { x: 10, y: 5 } }');
equals(path.closed, true);
equals(function() {
return path.closed;
}, true);
});
test('path.remove()', function() {
@ -59,17 +63,25 @@ test('path.remove()', function() {
path.add(30, 0);
path.removeSegment(0);
equals(path.segments.length, 3);
equals(function() {
return path.segments.length;
}, 3);
path.removeSegment(0);
equals(path.segments.length, 2);
equals(function() {
return path.segments.length;
}, 2);
path.removeSegments(0, 2);
equals(path.segments.length, 0);
equals(function() {
return path.segments.length;
}, 0);
path.remove();
equals(doc.activeLayer.children.length, 0);
equals(function() {
return doc.activeLayer.children.length;
}, 0);
});
@ -77,12 +89,20 @@ test('Is the path deselected after setting a new list of segments?', function()
var doc = new Document();
var path = new Path([0, 0]);
path.selected = true;
equals(path.selected, true);
equals(doc.selectedItems.length, 1);
equals(function() {
return path.selected;
}, true);
equals(function() {
return doc.selectedItems.length;
}, 1);
path.segments = [[0, 10]];
equals(path.selected, false);
equals(doc.selectedItems.length, 0);
equals(function() {
return path.selected;
}, false);
equals(function() {
return doc.selectedItems.length;
}, 0);
});
test('Path#reverse', function() {

View file

@ -62,7 +62,9 @@ test('getting group styles', function() {
// the group now contains two paths with different fillColors and therefore
// should return undefined:
equals(group.fillColor, undefined, 'group.fillColor');
equals(function() {
return group.fillColor;
}, undefined);
//If we remove the first path, it should now return 'black':
group.children[0].remove();
@ -112,7 +114,9 @@ test('setting group styles 2', function() {
// By appending a path with a different fillcolor,
// the group's fillColor should return undefined:
equals(group.fillColor, undefined, 'group.fillColor');
equals(function() {
return group.fillColor;
}, undefined);
// But, both paths have a red strokeColor, so:
compareRGBColors(group.strokeColor, 'red', 'group.strokeColor');

View file

@ -26,7 +26,9 @@ test('new Rectangle({x: 10, y: 20, width: 30, height: 40});', function() {
test('get size', function() {
var rect = new Rectangle(10, 10, 20, 30);
equals(rect.size.equals([20, 30]), true);
equals(function() {
return rect.size.equals([20, 30]);
}, true);
});
test('set size', function() {
@ -142,55 +144,72 @@ test('set rightCenter', function() {
test('intersects(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
equals(rect1.intersects(rect2), false);
equals(function() {
return rect1.intersects(rect2);
}, false);
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
equals(rect1.intersects(rect2), true);
equals(function() {
return rect1.intersects(rect2);
}, true);
});
test('contains(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 };
equals(rect1.contains(rect2), false);
equals(function() {
return rect1.contains(rect2);
}, false);
rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = new Rectangle({ x: 170.5, y: 280.5, width: 19, height: 19 });
equals(rect1.contains(rect2), false);
equals(function() {
return rect1.contains(rect2);
}, false);
rect1 = new Rectangle({ x: 299, y: 161, width: 137, height: 129 });
rect2 = new Rectangle({ x: 340, y: 197, width: 61, height: 61 });
equals(rect1.contains(rect2), true);
equals(rect2.contains(rect1), false);
});
equals(function() {
return rect1.contains(rect2);
}, true);
equals(function() {
return rect2.contains(rect1);
}, false);});
test('contains(point)', function() {
var rect = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var point = new Point(166, 280);
equals(rect.contains(point), true);
equals(function() {
return rect.contains(point);
}, true);
var point = new Point(30, 30);
equals(rect.contains(point), false);
});
equals(function() {
return rect.contains(point);
}, false);});
test('intersect(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var intersected = rect1.intersect(rect2);
equals(intersected.equals({ x: 170.5, y: 280.5, width: 9.5, height: 9.5 }), true);
equals(function() {
return intersected.equals({ x: 170.5, y: 280.5, width: 9.5, height: 9.5 });
}, true);
});
test('unite(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var united = rect1.unite(rect2);
equals(united.equals({ x: 160, y: 270, width: 29.5, height: 29.5 }), true);
equals(function() {
return united.equals({ x: 160, y: 270, width: 29.5, height: 29.5 });
}, true);
});
test('include(point)', function() {
var rect1 = new Rectangle({ x: 95, y: 151, width: 20, height: 20 });
var included = rect1.include([50, 50]);
equals(included.equals({ x: 50, y: 50, width: 65, height: 121 }), true);
equals(function() {
return included.equals({ x: 50, y: 50, width: 65, height: 121 });
}, true);
});
test('toString()', function() {

View file

@ -33,8 +33,13 @@ test('segment.reverse()', function() {
test('segment.clone()', function() {
var segment = new Segment(new Point(10, 10), new Point(5, 5), new Point(15, 15));
var clone = segment.clone();
equals(segment == clone, false);
equals(segment.toString(), clone.toString());
equals(function() {
return segment == clone;
}, false);
equals(function() {
return segment.toString();
}, clone.toString());
});
test('segment.remove()', function() {
@ -46,8 +51,10 @@ test('segment.remove()', function() {
test('segment.selected', function() {
var path = new Path([10, 20], [50, 100]);
path.segments[0].point.selected = true;
equals(path.segments[0].point.selected, true);
equals(function() {
return path.segments[0].point.selected;
}, true);
path.segments[0].point.selected = false;
equals(path.segments[0].point.selected, false);
});
equals(function() {
return path.segments[0].point.selected;
}, false);});