2013-01-28 21:03:27 -05:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2014-01-03 19:47:16 -05:00
|
|
|
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2013-01-28 21:03:27 -05:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2013-06-11 16:43:29 -04:00
|
|
|
// Register a jsDump parser for Base.
|
2013-06-11 16:38:08 -04:00
|
|
|
QUnit.jsDump.setParser('Base', function (obj, stack) {
|
2014-08-16 13:24:54 -04:00
|
|
|
// Just compare the string representation of classes inheriting from Base,
|
|
|
|
// since they hide the internal values.
|
|
|
|
return obj.toString();
|
2013-06-11 16:38:08 -04:00
|
|
|
});
|
|
|
|
|
2013-06-11 16:43:29 -04:00
|
|
|
// Override the default object parser to handle Base objects.
|
|
|
|
// We need to keep a reference to the previous implementation.
|
|
|
|
var objectParser = QUnit.jsDump.parsers.object;
|
|
|
|
|
2013-06-11 16:38:08 -04:00
|
|
|
QUnit.jsDump.setParser('object', function (obj, stack) {
|
2014-08-16 13:24:54 -04:00
|
|
|
return (obj instanceof Base
|
|
|
|
? QUnit.jsDump.parsers.Base
|
|
|
|
: objectParser).call(this, obj, stack);
|
2013-06-11 16:38:08 -04:00
|
|
|
});
|
|
|
|
|
2014-12-28 08:33:22 -05:00
|
|
|
var comparators = {
|
2014-12-28 09:21:38 -05:00
|
|
|
Null: QUnit.strictEqual,
|
|
|
|
Undefined: QUnit.strictEqual,
|
2014-12-28 10:41:23 -05:00
|
|
|
Boolean: QUnit.strictEqual,
|
|
|
|
|
|
|
|
Object: function(actual, expected, message, options) {
|
|
|
|
QUnit.push(Base.equals(actual, expected), actual, expected, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
Base: function(actual, expected, message, options) {
|
|
|
|
comparators.Object(actual, expected, message, options);
|
|
|
|
},
|
2014-12-28 09:21:38 -05:00
|
|
|
|
2014-12-28 08:33:22 -05:00
|
|
|
Number: function(actual, expected, message, options) {
|
|
|
|
// Compare with a default tolerance of Numerical.TOLERANCE:
|
|
|
|
var ok = Math.abs(actual - expected)
|
|
|
|
<= Base.pick(options && options.tolerance, Numerical.TOLERANCE);
|
|
|
|
QUnit.push(ok, ok ? expected : actual, expected, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
Array: function(actual, expected, message, options) {
|
2014-12-28 09:21:38 -05:00
|
|
|
QUnit.strictEqual(actual.length, expected.length,
|
|
|
|
(message || '') + ' length');
|
2014-12-28 08:33:22 -05:00
|
|
|
for (var i = 0, l = actual.length; i < l; i++) {
|
|
|
|
equals(actual[i], expected[i], (message || '') + ' [' + i + ']',
|
|
|
|
options);
|
|
|
|
}
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Point: function(actual, expected, message, options) {
|
2014-12-28 09:21:38 -05:00
|
|
|
comparators.Number(actual.x, expected.x, (message || '') + ' x',
|
|
|
|
options);
|
|
|
|
comparators.Number(actual.y, expected.y, (message || '') + ' y',
|
|
|
|
options);
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Size: function(actual, expected, message, options) {
|
2014-12-28 09:21:38 -05:00
|
|
|
comparators.Number(actual.width, expected.width,
|
|
|
|
(message || '') + ' width', options);
|
|
|
|
comparators.Number(actual.height, expected.height,
|
|
|
|
(message || '') + ' height', options);
|
2014-12-28 08:59:48 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Rectangle: function(actual, expected, message, options) {
|
|
|
|
comparators.Point(actual, expected, message, options);
|
|
|
|
comparators.Size(actual, expected, message, options);
|
2014-12-28 09:21:38 -05:00
|
|
|
},
|
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
Matrix: function(actual, expected, message, options) {
|
|
|
|
comparators.Array(actual.values, expected.values, message, options);
|
|
|
|
},
|
|
|
|
|
2014-12-28 09:21:38 -05:00
|
|
|
Color: function(actual, expected, message, options) {
|
|
|
|
if (actual && expected) {
|
|
|
|
equals(actual.type, expected.type,
|
|
|
|
(message || '') + ' type', options);
|
2014-12-28 10:41:23 -05:00
|
|
|
// NOTE: This also compares gradients, with identity checks and all.
|
2014-12-28 09:21:38 -05:00
|
|
|
equals(actual.components, expected.components,
|
|
|
|
(message || '') + ' components', options);
|
|
|
|
} else {
|
|
|
|
equals(actual, expected, message, options);
|
|
|
|
}
|
2014-12-28 10:41:23 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
Segment: function(actual, expected, message, options) {
|
|
|
|
Base.each(['handleIn', 'handleOut', 'point', 'selected'],
|
|
|
|
function(key) {
|
|
|
|
equals(actual[key], expected[key], (message || '') + ' ' + key);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
SegmentPoint: function(actual, expected, message, options) {
|
|
|
|
comparators.Point(actual, expected, message, options);
|
|
|
|
comparators.Boolean(actual.selected, expected.selected,
|
|
|
|
(message || '') + ' selected', options);
|
2014-12-28 08:33:22 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
var identicalAfterClone = {
|
|
|
|
Gradient: true,
|
|
|
|
Symbol: true
|
|
|
|
};
|
|
|
|
|
2014-12-28 08:33:22 -05:00
|
|
|
function getFunctionMessage(func) {
|
|
|
|
var message = func.toString().match(
|
2014-08-16 13:24:54 -04:00
|
|
|
/^\s*function[^\{]*\{([\s\S]*)\}\s*$/)[1]
|
|
|
|
.replace(/ /g, '')
|
|
|
|
.replace(/^\s+|\s+$/g, '');
|
2014-12-28 08:33:22 -05:00
|
|
|
if (/^return /.test(message)) {
|
|
|
|
message = message
|
|
|
|
.replace(/^return /, '')
|
|
|
|
.replace(/;$/, '');
|
|
|
|
}
|
|
|
|
return message;
|
2013-07-21 19:11:09 -04:00
|
|
|
}
|
|
|
|
|
2011-05-14 13:59:04 -04:00
|
|
|
// Override equals to convert functions to message and execute them as tests()
|
2014-12-28 08:33:22 -05:00
|
|
|
function equals(actual, expected, message, options) {
|
2014-11-30 14:27:14 -05:00
|
|
|
// Allow the use of functions for actual, which will get called and their
|
|
|
|
// source content extracted for readable reports.
|
2014-08-16 13:24:54 -04:00
|
|
|
if (typeof actual === 'function') {
|
2014-12-28 08:33:22 -05:00
|
|
|
if (!message)
|
|
|
|
message = getFunctionMessage(actual);
|
2014-08-16 13:24:54 -04:00
|
|
|
actual = actual();
|
|
|
|
}
|
2014-12-28 09:21:38 -05:00
|
|
|
// Get the comparator based on the expected value's type only and ignore the
|
|
|
|
// actual value's type.
|
2014-12-28 10:41:23 -05:00
|
|
|
var type = typeof expected,
|
|
|
|
cls;
|
|
|
|
type = expected === null && 'Null'
|
|
|
|
|| type === 'number' && 'Number'
|
|
|
|
|| type === 'boolean' && 'Boolean'
|
2014-12-28 09:21:38 -05:00
|
|
|
|| type === 'undefined' && 'Undefined'
|
|
|
|
|| Array.isArray(expected) && 'Array'
|
2014-12-28 10:41:23 -05:00
|
|
|
|| (cls = expected && expected._class)
|
|
|
|
|| type === 'object' && 'Object';
|
|
|
|
var comparator = type && comparators[type];
|
2014-12-28 09:21:38 -05:00
|
|
|
if (comparator) {
|
|
|
|
comparator(actual, expected, message, options);
|
|
|
|
} else if (expected && expected.equals) {
|
|
|
|
// Fall back to equals
|
|
|
|
QUnit.push(expected.equals(actual), actual, expected, message);
|
|
|
|
} else {
|
|
|
|
// Finally perform a strict compare
|
|
|
|
QUnit.push(actual === expected, actual, expected, message);
|
2014-12-28 08:33:22 -05:00
|
|
|
}
|
2014-12-28 10:41:23 -05:00
|
|
|
if (options && options.cloned && cls) {
|
|
|
|
var identical = identicalAfterClone[cls];
|
|
|
|
QUnit.push(identical ? actual === expected : actual !== expected,
|
|
|
|
actual, identical ? expected : 'not ' + expected,
|
|
|
|
(message || '') + ' identity');
|
|
|
|
}
|
2011-05-07 12:25:34 -04:00
|
|
|
}
|
2011-05-05 08:38:20 -04:00
|
|
|
|
2011-05-14 13:59:04 -04:00
|
|
|
function test(testName, expected) {
|
2014-08-16 13:24:54 -04:00
|
|
|
return QUnit.test(testName, function() {
|
|
|
|
var project = new Project();
|
|
|
|
expected();
|
|
|
|
project.remove();
|
|
|
|
});
|
2011-05-14 13:59:04 -04:00
|
|
|
}
|
|
|
|
|
2013-03-17 13:13:55 -04:00
|
|
|
function asyncTest(testName, expected) {
|
2014-08-16 13:24:54 -04:00
|
|
|
return QUnit.asyncTest(testName, function() {
|
|
|
|
var project = new Project();
|
|
|
|
expected(function() {
|
|
|
|
project.remove();
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
});
|
2013-03-17 13:13:55 -04:00
|
|
|
}
|
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
function compareItems(item, item2, options) {
|
|
|
|
if (options && options.cloned)
|
|
|
|
QUnit.notStrictEqual(item.id, item2.id, 'Compare Item#id');
|
|
|
|
|
|
|
|
QUnit.strictEqual(item.constructor, item2.constructor,
|
|
|
|
'Compare Item#constructor');
|
|
|
|
// When item was cloned and had a name, the name will be versioned
|
|
|
|
equals(options && options.cloned && item.name ? item.name + ' 1'
|
|
|
|
: item.name, item2.name, 'Compare Item#name');
|
|
|
|
Base.each(['bounds', 'position', 'data', 'matrix', 'opacity', 'locked',
|
|
|
|
'visible', 'blendMode', 'selected', 'fullySelected', 'clipMask',
|
|
|
|
'guide'],
|
|
|
|
function(key) {
|
|
|
|
equals(item[key], item2[key], 'Compare Item#' + key, options);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2014-12-28 10:41:23 -05:00
|
|
|
);
|
2014-08-16 13:24:54 -04:00
|
|
|
|
2014-12-28 10:41:23 -05:00
|
|
|
// Style
|
|
|
|
Base.each(['fillColor', 'strokeColor', 'strokeCap', 'strokeJoin',
|
|
|
|
'dashArray', 'dashOffset', 'miterLimit',
|
2014-08-16 13:24:54 -04:00
|
|
|
'fontSize', 'font', 'leading', 'justification'],
|
2014-12-28 10:41:23 -05:00
|
|
|
function(key) {
|
|
|
|
equals(item.style[key], item2.style[key], 'Compare Style#' + key,
|
|
|
|
options);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2014-12-28 10:41:23 -05:00
|
|
|
);
|
2014-08-16 13:24:54 -04:00
|
|
|
|
|
|
|
// Path specific
|
|
|
|
if (item instanceof Path) {
|
2014-12-28 10:41:23 -05:00
|
|
|
Base.each(['segments', 'closed', 'clockwise', 'length'],
|
|
|
|
function(key) {
|
|
|
|
equals(item[key], item2[key], 'Compare Path#' + key, options);
|
|
|
|
}
|
|
|
|
);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shape specific
|
|
|
|
if (item instanceof Shape) {
|
2014-12-28 10:41:23 -05:00
|
|
|
Base.each(['shape', 'size', 'radius'],
|
|
|
|
function(key) {
|
|
|
|
equals(item[key], item2[key], 'Compare Shape#' + key, options);
|
|
|
|
}
|
|
|
|
);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Group specific
|
|
|
|
if (item instanceof Group) {
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.clipped, item2.clipped, 'Compare Group#clipped', options);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layer specific
|
|
|
|
if (item instanceof Layer) {
|
|
|
|
equals(function() {
|
2014-12-28 08:33:22 -05:00
|
|
|
return options && options.dontShareProject
|
2014-08-16 13:24:54 -04:00
|
|
|
? item.project != item2.project
|
|
|
|
: item.project == item2.project;
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PlacedSymbol specific
|
|
|
|
if (item instanceof PlacedSymbol) {
|
2014-12-28 08:33:22 -05:00
|
|
|
if (options.dontShareProject) {
|
2014-08-16 13:24:54 -04:00
|
|
|
compareItems(item.symbol.definition, item2.symbol.definition,
|
2014-12-28 08:33:22 -05:00
|
|
|
options,
|
2014-08-16 13:24:54 -04:00
|
|
|
'Compare Symbol#definition');
|
|
|
|
} else {
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.symbol, item2.symbol, 'Compare PlacedSymbol#symbol',
|
|
|
|
options);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Raster specific
|
|
|
|
if (item instanceof Raster) {
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.size, item2.size, 'Compare Raster#size');
|
2014-11-30 14:27:14 -05:00
|
|
|
equals(item.width, item2.width, 'Compare Raster#width');
|
|
|
|
equals(item.height, item2.height, 'Compare Raster#height');
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.ppi, item2.ppi, 'Compare Raster#ppi');
|
2014-08-16 13:24:54 -04:00
|
|
|
equals(item.source, item2.source, 'Compare Raster#source');
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.image, item2.image, 'Compare Raster#image');
|
|
|
|
equals(item.toDataURL(), item2.toDataURL(),
|
2014-08-16 13:24:54 -04:00
|
|
|
'Compare Raster#toDataUrl()');
|
|
|
|
}
|
|
|
|
|
|
|
|
// TextItem specific:
|
|
|
|
if (item instanceof TextItem) {
|
|
|
|
equals(item.content, item2.content, 'Compare Item#content');
|
|
|
|
}
|
|
|
|
|
|
|
|
// PointText specific:
|
|
|
|
if (item instanceof PointText) {
|
2014-12-28 10:41:23 -05:00
|
|
|
equals(item.point, item2.point, 'Compare Item#point');
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check length of children and recursively compare them:
|
|
|
|
if (item.children) {
|
|
|
|
equals(function() {
|
|
|
|
return item.children.length == item2.children.length;
|
|
|
|
}, true);
|
|
|
|
for (var i = 0, l = item.children.length; i < l; i++) {
|
2014-12-28 08:33:22 -05:00
|
|
|
compareItems(item.children[i], item2.children[i], options);
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
|
|
|
}
|
2011-05-26 04:20:21 -04:00
|
|
|
}
|
2012-12-01 14:05:48 -05:00
|
|
|
|
2013-03-17 09:42:20 -04:00
|
|
|
function compareProjects(project, project2) {
|
2014-08-16 13:24:54 -04:00
|
|
|
// Compare Project#symbols:
|
|
|
|
equals(function() {
|
|
|
|
return project.symbols.length == project2.symbols.length;
|
|
|
|
}, true);
|
|
|
|
for (var i = 0, l = project.symbols.length; i < l; i++) {
|
|
|
|
var definition1 = project.symbols[i].definition;
|
|
|
|
var definition2 = project2.symbols[i].definition;
|
2014-12-28 08:33:22 -05:00
|
|
|
compareItems(definition1, definition2, { dontShareProject: true },
|
2014-08-16 13:24:54 -04:00
|
|
|
'Compare Symbol#definition');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare Project#layers:
|
|
|
|
equals(function() {
|
|
|
|
return project.layers.length == project2.layers.length;
|
|
|
|
}, true);
|
|
|
|
for (var i = 0, l = project.layers.length; i < l; i++) {
|
2014-12-28 08:33:22 -05:00
|
|
|
compareItems(project.layers[i], project2.layers[i],
|
|
|
|
{ dontShareProject: true });
|
2014-08-16 13:24:54 -04:00
|
|
|
}
|
2013-03-17 09:42:20 -04:00
|
|
|
}
|
|
|
|
|
2012-12-01 14:05:48 -05:00
|
|
|
// SVG
|
|
|
|
|
2013-04-23 10:19:08 -04:00
|
|
|
function createSVG(xml) {
|
2014-08-16 13:24:54 -04:00
|
|
|
return new DOMParser().parseFromString(
|
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg">' + xml + '</svg>',
|
|
|
|
'text/xml');
|
2014-04-06 07:48:03 -04:00
|
|
|
}
|