Merge branch 'master' of github.com:scriptographer/paper.js

This commit is contained in:
Jonathan Puckey 2011-05-07 18:18:51 +01:00
commit 791d9335ce
16 changed files with 344 additions and 146 deletions

View file

@ -55,6 +55,7 @@ var Document = this.Document = Base.extend({
this.bounds = Rectangle.create(0, 0, this._size.width, this.bounds = Rectangle.create(0, 0, this._size.width,
this._size.height); this._size.height);
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d');
// Push it onto paper.documents and adjust index in one:
Base.splice(paper.documents, [this]); Base.splice(paper.documents, [this]);
this.activate(); this.activate();
this.layers = []; this.layers = [];

View file

@ -29,8 +29,8 @@ var Symbol = this.Symbol = Base.extend({
setDefinition: function(item) { setDefinition: function(item) {
this._definition = item; this._definition = item;
this._definition._removeFromParent(); item._removeFromParent();
this._definition.setPosition(new Point(0, 0)); item.setPosition(new Point());
} }
// TODO: // TODO:

View file

@ -17,5 +17,6 @@
var ChangeFlags = { var ChangeFlags = {
PATH: 1, // Path geometry PATH: 1, // Path geometry
STROKE: 2, // Stroke geometry STROKE: 2, // Stroke geometry
STYLE: 4 // Fille style or stroke color / dash STYLE: 4, // Fille style or stroke color / dash,
}; HIERARCHY: 8 // Change in item hierarchy
};

View file

@ -20,34 +20,28 @@ var Layer = this.Layer = Group.extend({
initialize: function() { initialize: function() {
this.children = []; this.children = [];
this._document = paper.document; this._document = paper.document;
this._document.layers.push(this); // Push it onto document.layers and adjust index in one:
Base.splice(this._document.layers, [this]);
this.activate(); this.activate();
}, },
getIndex: function() {
return this.parent ? this.base() : this._document.layers.indexOf(this);
},
/** /**
* Removes the layer from its document's layers list * Removes the layer from its document's layers list
* or its parent's children list. * or its parent's children list.
*/ */
_removeFromParent: function() { _removeFromParent: function() {
if (!this.parent) { return this.parent ? this.base()
return !!this._document.layers.splice(this.getIndex(), 1).length; : !!Base.splice(this._document.layers, null, this._index, 1).length;
} else {
return this.base();
}
}, },
getNextSibling: function() { getNextSibling: function() {
return this.parent ? this.base() return this.parent ? this.base()
: this._document.layers[this.getIndex() + 1] || null; : this._document.layers[this._index + 1] || null;
}, },
getPreviousSibling: function() { getPreviousSibling: function() {
return this.parent ? this.base() return this.parent ? this.base()
: this._document.layers[this.getIndex() - 1] || null; : this._document.layers[this._index - 1] || null;
}, },
activate: function() { activate: function() {
@ -58,14 +52,13 @@ var Layer = this.Layer = Group.extend({
return function(item) { return function(item) {
// if the item is a layer and contained within Document#layers // if the item is a layer and contained within Document#layers
if (item instanceof Layer && !item.parent if (item instanceof Layer && !item.parent
&& this._removeFromParent()) { && this._removeFromParent()) {
item._document.layers.splice(item.getIndex() + (above ? 1 : -1), Base.splice(item._document.layers, [this],
0, this); item._index + (above ? 1 : -1), 0);
this._setDocument(item._document); this._setDocument(item._document);
return true; return true;
} else {
return this.base(item);
} }
return this.base(item);
}; };
} }

View file

@ -46,12 +46,6 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
this.children[i].smooth(); this.children[i].smooth();
}, },
moveTo: function() {
var path = new Path();
this.appendTop(path);
path.moveTo.apply(path, arguments);
},
draw: function(ctx, param) { draw: function(ctx, param) {
var firstChild = this.children[0]; var firstChild = this.children[0];
ctx.beginPath(); ctx.beginPath();
@ -70,8 +64,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
ctx.stroke(); ctx.stroke();
} }
} }
}, new function() { }, new function() { // Injection scope for PostScript-like drawing functions
function getCurrentPath(that) { function getCurrentPath(that) {
if (that.children.length) { if (that.children.length) {
return that.children[that.children.length - 1]; return that.children[that.children.length - 1];
@ -81,6 +74,12 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
} }
var fields = { var fields = {
moveTo: function() {
var path = new Path();
this.appendTop(path);
path.moveTo.apply(path, arguments);
},
moveBy: function() { moveBy: function() {
var point = arguments.length ? Point.read(arguments) : new Point(), var point = arguments.length ? Point.read(arguments) : new Point(),
path = getCurrentPath(this), path = getCurrentPath(this),
@ -93,6 +92,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
} }
}; };
// Redirect all other drawing commands to the current path
Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo', Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo',
'arcTo', 'lineBy', 'curveBy', 'arcBy'], function(key) { 'arcTo', 'lineBy', 'curveBy', 'arcBy'], function(key) {
fields[key] = function() { fields[key] = function() {

View file

@ -15,7 +15,4 @@
*/ */
var PathItem = this.PathItem = Item.extend({ var PathItem = this.PathItem = Item.extend({
initialize: function() {
this.base();
}
}); });

View file

@ -1,5 +1,22 @@
// Let's be strict // Let's be strict
equals = strictEqual;
function equals(actual, expected, message) {
if (typeof actual === 'function') {
if (!message) {
message = actual.toString().match(
/^\s*function[^\{]*\{([\s\S]*)\}\s*$/)[1]
.replace(/ /g, '')
.replace(/^\s+|\s+$/g, '');
if (/^return /.test(message)) {
message = message
.replace(/^return /, '')
.replace(/;$/, '');
}
}
actual = actual();
}
return strictEqual(actual, expected, message);
}
function compareNumbers(number1, number2, message) { function compareNumbers(number1, number2, message) {
if (number1 !== 0) if (number1 !== 0)

View file

@ -18,5 +18,7 @@ test('moveTo / lineTo', function() {
path.fillColor = 'black'; 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(); var secondDoc = new Document();
doc.activate(); doc.activate();
var path = new Path(); var path = new Path();
equals(doc.activeLayer.children[0] == path, true); equals(function() {
equals(secondDoc.activeLayer.children.length == 0, true); 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() { test('new Group()', function() {
var doc = new Document(); var doc = new Document();
var group = new Group(); 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() { test('new Group([item])', function() {
var doc = new Document(); var doc = new Document();
var path = new Path(); var path = new Path();
var group = new Group([path]); var group = new Group([path]);
equals(doc.activeLayer.children.length == 1, true); equals(function() {
equals(group.children[0] == path, true); return doc.activeLayer.children.length == 1;
}, true);
equals(function() {
return group.children[0] == path;
}, true);
}); });
test('Group bounds', function() { test('Group bounds', function() {

View file

@ -5,9 +5,15 @@ test('copyTo(document)', function() {
var path = new Path(); var path = new Path();
var secondDoc = new Document(); var secondDoc = new Document();
var copy = path.copyTo(secondDoc); var copy = path.copyTo(secondDoc);
equals(secondDoc.activeLayer.children.indexOf(copy) != -1, true); equals(function() {
equals(doc.activeLayer.children.indexOf(copy) == -1, true); return secondDoc.activeLayer.children.indexOf(copy) != -1;
equals(copy != path, true); }, true);
equals(function() {
return doc.activeLayer.children.indexOf(copy) == -1;
}, true);
equals(function() {
return copy != path;
}, true);
}); });
test('copyTo(layer)', function() { test('copyTo(layer)', function() {
@ -16,23 +22,33 @@ test('copyTo(layer)', function() {
var layer = new Layer(); var layer = new Layer();
var copy = path.copyTo(layer); var copy = path.copyTo(layer);
equals(layer.children.indexOf(copy) != -1, true); equals(function() {
equals(doc.layers[0].children.indexOf(copy) == -1, true); return layer.children.indexOf(copy) != -1;
}, true);
equals(function() {
return doc.layers[0].children.indexOf(copy) == -1;
}, true);
}); });
test('clone()', function() { test('clone()', function() {
var doc = new Document(); var doc = new Document();
var path = new Path(); var path = new Path();
var copy = path.clone(); var copy = path.clone();
equals(doc.activeLayer.children.length == 2, true); equals(function() {
equals(path != copy, true); return doc.activeLayer.children.length == 2;
}, true);
equals(function() {
return path != copy;
}, true);
}); });
test('appendChild(item)', function() { test('appendChild(item)', function() {
var doc = new Document(); var doc = new Document();
var path = new Path(); var path = new Path();
doc.activeLayer.appendChild(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() { 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 secondDoc = new Document();
var path = new Path(); var path = new Path();
doc.activeLayer.appendChild(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); secondDoc.activeLayer.appendTop(path);
equals(doc.activeLayer.isChild(path), false); equals(function() {
equals(path.isParent(doc.activeLayer), false); return doc.activeLayer.isChild(path);
equals(secondDoc.activeLayer.isChild(path), true); }, false);
equals(path.isParent(secondDoc.activeLayer), true); equals(function() {
return path.isParent(doc.activeLayer);
equals(doc.activeLayer.children.indexOf(path) == -1, true); }, false);
equals(secondDoc.activeLayer.children.indexOf(path) == 0, true); 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() { test('item.lastChild / item.firstChild', function() {
var doc = new Document(); var doc = new Document();
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
equals(doc.activeLayer.firstChild == path, true); equals(function() {
equals(doc.activeLayer.lastChild == secondPath, true); return doc.activeLayer.firstChild == path;
}, true);
equals(function() {
return doc.activeLayer.lastChild == secondPath;
}, true);
}); });
test('appendBottom(item)', function() { test('appendBottom(item)', function() {
@ -64,7 +97,9 @@ test('appendBottom(item)', function() {
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
doc.activeLayer.appendBottom(secondPath); doc.activeLayer.appendBottom(secondPath);
equals(secondPath.index < path.index, true); equals(function() {
return secondPath.index < path.index;
}, true);
}); });
test('moveAbove(item)', function() { test('moveAbove(item)', function() {
@ -72,32 +107,48 @@ test('moveAbove(item)', function() {
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
path.moveAbove(secondPath); path.moveAbove(secondPath);
equals(doc.activeLayer.lastChild == path, true); equals(function() {
return doc.activeLayer.lastChild == path;
}, true);
}); });
test('moveBelow(item)', function() { test('moveBelow(item)', function() {
var doc = new Document(); var doc = new Document();
var firstPath = new Path(); var firstPath = new Path();
var secondPath = new Path(); var secondPath = new Path();
equals(secondPath.index > firstPath.index, true); equals(function() {
return secondPath.index > firstPath.index;
}, true);
secondPath.moveBelow(firstPath); secondPath.moveBelow(firstPath);
equals(secondPath.index < firstPath.index, true); equals(function() {
return secondPath.index < firstPath.index;
}, true);
}); });
test('isDescendant(item) / isAncestor(item)', function() { test('isDescendant(item) / isAncestor(item)', function() {
var doc = new Document(); var doc = new Document();
var path = new Path(); var path = new Path();
equals(path.isDescendant(doc.activeLayer), true); equals(function() {
equals(doc.activeLayer.isDescendant(path), false); return path.isDescendant(doc.activeLayer);
}, true);
equals(path.isAncestor(doc.activeLayer), false); equals(function() {
equals(doc.activeLayer.isAncestor(path), true); 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: // 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: // 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() { test('isGroupedWith', function() {
@ -107,34 +158,60 @@ test('isGroupedWith', function() {
var group = new Group([path]); var group = new Group([path]);
var secondGroup = new Group([secondPath]); var secondGroup = new Group([secondPath]);
equals(path.isGroupedWith(secondPath), false); equals(function() {
return path.isGroupedWith(secondPath);
}, false);
secondGroup.appendTop(path); secondGroup.appendTop(path);
equals(path.isGroupedWith(secondPath), true); equals(function() {
equals(path.isGroupedWith(group), false); return path.isGroupedWith(secondPath);
equals(path.isDescendant(secondGroup), true); }, true);
equals(secondGroup.isDescendant(path), false); equals(function() {
equals(secondGroup.isDescendant(secondGroup), false); return path.isGroupedWith(group);
equals(path.isGroupedWith(secondGroup), false); }, 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); paper.document.activeLayer.appendTop(path);
equals(path.isGroupedWith(secondPath), false); equals(function() {
return path.isGroupedWith(secondPath);
}, false);
paper.document.activeLayer.appendTop(secondPath); paper.document.activeLayer.appendTop(secondPath);
equals(path.isGroupedWith(secondPath), false); equals(function() {
return path.isGroupedWith(secondPath);
}, false);
}); });
test('getPreviousSibling() / getNextSibling()', function() { test('getPreviousSibling() / getNextSibling()', function() {
var doc = new Document(); var doc = new Document();
var firstPath = new Path(); var firstPath = new Path();
var secondPath = new Path(); var secondPath = new Path();
equals(firstPath.nextSibling == secondPath, true); equals(function() {
equals(secondPath.previousSibling == firstPath, true); return firstPath.nextSibling == secondPath;
equals(secondPath.nextSibling == null, true); }, true);
equals(function() {
return secondPath.previousSibling == firstPath;
}, true);
equals(function() {
return secondPath.nextSibling == null;
}, true);
}); });
test('hidden', function() { test('hidden', function() {
var doc = new Document(); var doc = new Document();
var firstPath = new Path(); var firstPath = new Path();
firstPath.visible = false; firstPath.visible = false;
equals(firstPath.hidden, true); equals(function() {
return firstPath.hidden;
}, true);
}); });
test('reverseChildren()', function() { test('reverseChildren()', function() {
@ -142,11 +219,19 @@ test('reverseChildren()', function() {
var path = new Path(); var path = new Path();
var secondPath = new Path(); var secondPath = new Path();
var thirdPath = new Path(); var thirdPath = new Path();
equals(doc.activeLayer.firstChild == path, true); equals(function() {
return doc.activeLayer.firstChild == path;
}, true);
doc.activeLayer.reverseChildren(); doc.activeLayer.reverseChildren();
equals(doc.activeLayer.firstChild == path, false); equals(function() {
equals(doc.activeLayer.firstChild == thirdPath, true); return doc.activeLayer.firstChild == path;
equals(doc.activeLayer.lastChild == path, true); }, 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() { 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(); var group = new Group();
group.appendTop(new Path()); group.appendTop(new Path());
equals(path.document == doc1, true); equals(function() {
return path.document == doc1;
}, true);
var doc2 = new Document(); var doc2 = new Document();
doc2.activeLayer.appendTop(path); doc2.activeLayer.appendTop(path);
equals(path.document == doc2, true); equals(function() {
return path.document == doc2;
}, true);
doc2.activeLayer.appendTop(group); doc2.activeLayer.appendTop(group);
equals(group.children[0].document == doc2, true); equals(function() {
return group.children[0].document == doc2;
}, true);
}); });
test('group.selected', function() { test('group.selected', function() {
@ -170,16 +261,27 @@ test('group.selected', function() {
var path2 = new Path([0, 0]); var path2 = new Path([0, 0]);
var group = new Group([path, path2]); var group = new Group([path, path2]);
path.selected = true; path.selected = true;
equals(group.selected, true); equals(function() {
return group.selected;
}, true);
path.selected = false; path.selected = false;
equals(group.selected, false); equals(function() {
return group.selected;
}, false);
group.selected = true; group.selected = true;
equals(path.selected, true); equals(function() {
equals(path2.selected, true); return path.selected;
}, true);
equals(function() {
return path2.selected;
}, true);
group.selected = false; group.selected = false;
equals(path.selected, false); equals(function() {
equals(path2.selected, false); 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 doc = new Document();
var firstLayer = doc.activeLayer; var firstLayer = doc.activeLayer;
var secondLayer = new Layer(); var secondLayer = new Layer();
equals(secondLayer.previousSibling == firstLayer, true); equals(function() {
equals(secondLayer.nextSibling == null, true); return secondLayer.previousSibling == firstLayer;
}, true);
equals(function() {
return secondLayer.nextSibling == null;
}, true);
// Move another layer into secondLayer and check nextSibling / // Move another layer into secondLayer and check nextSibling /
// previousSibling: // previousSibling:
var path = new Path(); var path = new Path();
var thirdLayer = new Layer(); var thirdLayer = new Layer();
secondLayer.appendBottom(thirdLayer); secondLayer.appendBottom(thirdLayer);
equals(secondLayer.children.length, 2); equals(function() {
equals(thirdLayer.nextSibling == path, true); return secondLayer.children.length;
}, 2);
equals(function() {
return thirdLayer.nextSibling == path;
}, true);
secondLayer.appendTop(thirdLayer); secondLayer.appendTop(thirdLayer);
equals(thirdLayer.nextSibling == null, true); equals(function() {
equals(thirdLayer.previousSibling == path, true); return thirdLayer.nextSibling == null;
equals(doc.layers.length == 2, true); }, true);
equals(function() {
return thirdLayer.previousSibling == path;
}, true);
equals(function() {
return doc.layers.length == 2;
}, true);
firstLayer.appendTop(secondLayer); firstLayer.appendTop(secondLayer);
equals(doc.layers.length == 1, true); equals(function() {
return doc.layers.length == 1;
}, true);
}); });
test('moveAbove / moveBelow', function() { test('moveAbove / moveBelow', function() {
@ -28,17 +44,26 @@ test('moveAbove / moveBelow', function() {
var firstLayer = doc.activeLayer; var firstLayer = doc.activeLayer;
var secondLayer = new Layer(); var secondLayer = new Layer();
secondLayer.moveBelow(firstLayer); secondLayer.moveBelow(firstLayer);
equals(secondLayer.previousSibling == null, true); equals(function() {
equals(secondLayer.nextSibling == firstLayer, true); return secondLayer.previousSibling == null;
}, true);
equals(function() {
return secondLayer.nextSibling == firstLayer;
}, true);
var path = new Path(); var path = new Path();
firstLayer.appendTop(path); firstLayer.appendTop(path);
// move the layer above the path, inside the firstLayer: // move the layer above the path, inside the firstLayer:
secondLayer.moveAbove(path); secondLayer.moveAbove(path);
equals(secondLayer.previousSibling == path, true); equals(function() {
equals(secondLayer.parent == firstLayer, true); return secondLayer.previousSibling == path;
}, true);
equals(function() {
return secondLayer.parent == firstLayer;
}, true);
// There should now only be one layer left: // 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); path.join(path2);
equals(path.segments.toString(), '{ point: { x: 0, y: 0 } },{ point: { x: 10, y: 0 } },{ point: { x: 20, y: 10 } }'); 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(); var path = new Path();
path.add(0, 0); path.add(0, 0);
@ -47,7 +49,9 @@ test('path.join(path)', function() {
path.join(path2); 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.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() { test('path.remove()', function() {
@ -59,17 +63,25 @@ test('path.remove()', function() {
path.add(30, 0); path.add(30, 0);
path.removeSegment(0); path.removeSegment(0);
equals(path.segments.length, 3); equals(function() {
return path.segments.length;
}, 3);
path.removeSegment(0); path.removeSegment(0);
equals(path.segments.length, 2); equals(function() {
return path.segments.length;
}, 2);
path.removeSegments(0, 2); path.removeSegments(0, 2);
equals(path.segments.length, 0); equals(function() {
return path.segments.length;
}, 0);
path.remove(); 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 doc = new Document();
var path = new Path([0, 0]); var path = new Path([0, 0]);
path.selected = true; path.selected = true;
equals(path.selected, true); equals(function() {
equals(doc.selectedItems.length, 1); return path.selected;
}, true);
equals(function() {
return doc.selectedItems.length;
}, 1);
path.segments = [[0, 10]]; path.segments = [[0, 10]];
equals(path.selected, false); equals(function() {
equals(doc.selectedItems.length, 0); return path.selected;
}, false);
equals(function() {
return doc.selectedItems.length;
}, 0);
}); });
test('Path#reverse', function() { 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 // the group now contains two paths with different fillColors and therefore
// should return undefined: // 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': //If we remove the first path, it should now return 'black':
group.children[0].remove(); group.children[0].remove();
@ -112,7 +114,9 @@ test('setting group styles 2', function() {
// By appending a path with a different fillcolor, // By appending a path with a different fillcolor,
// the group's fillColor should return undefined: // 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: // But, both paths have a red strokeColor, so:
compareRGBColors(group.strokeColor, 'red', 'group.strokeColor'); 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() { test('get size', function() {
var rect = new Rectangle(10, 10, 20, 30); 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() { test('set size', function() {
@ -142,55 +144,72 @@ test('set rightCenter', function() {
test('intersects(rect)', function() { test('intersects(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 }); var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 }; 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 }); rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 }; 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() { test('contains(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 }); var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var rect2 = { x: 195, y: 301, width: 19, height: 19 }; 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 }); rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
rect2 = new Rectangle({ x: 170.5, y: 280.5, width: 19, height: 19 }); 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 }); rect1 = new Rectangle({ x: 299, y: 161, width: 137, height: 129 });
rect2 = new Rectangle({ x: 340, y: 197, width: 61, height: 61 }); rect2 = new Rectangle({ x: 340, y: 197, width: 61, height: 61 });
equals(rect1.contains(rect2), true); equals(function() {
equals(rect2.contains(rect1), false); return rect1.contains(rect2);
}); }, true);
equals(function() {
return rect2.contains(rect1);
}, false);});
test('contains(point)', function() { test('contains(point)', function() {
var rect = new Rectangle({ x: 160, y: 270, width: 20, height: 20 }); var rect = new Rectangle({ x: 160, y: 270, width: 20, height: 20 });
var point = new Point(166, 280); var point = new Point(166, 280);
equals(rect.contains(point), true); equals(function() {
return rect.contains(point);
}, true);
var point = new Point(30, 30); var point = new Point(30, 30);
equals(rect.contains(point), false); equals(function() {
}); return rect.contains(point);
}, false);});
test('intersect(rect)', function() { test('intersect(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 }); 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 rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var intersected = rect1.intersect(rect2); 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() { test('unite(rect)', function() {
var rect1 = new Rectangle({ x: 160, y: 270, width: 20, height: 20 }); 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 rect2 = { x: 170.5, y: 280.5, width: 19, height: 19 };
var united = rect1.unite(rect2); 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() { test('include(point)', function() {
var rect1 = new Rectangle({ x: 95, y: 151, width: 20, height: 20 }); var rect1 = new Rectangle({ x: 95, y: 151, width: 20, height: 20 });
var included = rect1.include([50, 50]); 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() { test('toString()', function() {

View file

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