paper.js/test/lib/helpers.js

350 lines
9.1 KiB
JavaScript
Raw Normal View History

// Override equals to convert functions to message and execute them as tests()
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();
}
// Let's be strict
return strictEqual(actual, expected, message);
}
2011-05-05 08:38:20 -04:00
function test(testName, expected) {
return QUnit.test(testName, function() {
2011-05-20 09:08:17 -04:00
var project = new Project();
expected();
2011-05-20 09:08:17 -04:00
project.remove();
});
}
function compareNumbers(number1, number2, message) {
2011-05-03 03:55:01 -04:00
if (number1 !== 0)
number1 = Math.round(number1 * 100) / 100;
2011-05-03 03:55:01 -04:00
if (number2 !== 0)
number2 = Math.round(number2 * 100) / 100;
equals(number1, number2, message);
}
2011-02-07 13:28:09 -05:00
function comparePoints(point1, point2, message) {
2011-05-05 08:05:39 -04:00
compareNumbers(point1.x, point2.x,
(message || '') + ' x');
compareNumbers(point1.y, point2.y,
(message || '') + ' y');
}
function compareRectangles(rect1, rect2, message) {
2011-05-05 08:05:39 -04:00
compareNumbers(rect1.x, rect2.x,
(message || '') + ' x');
compareNumbers(rect1.y, rect2.y,
(message || '') + ' y');
compareNumbers(rect1.width, rect2.width,
2011-05-05 08:05:39 -04:00
(message || '') + ' width');
compareNumbers(rect1.height, rect2.height,
2011-05-05 08:05:39 -04:00
(message || '') + ' height');
}
2011-02-19 11:11:17 -05:00
function compareRgbColors(color1, color2, message) {
color1 = new RgbColor(color1);
color2 = new RgbColor(color2);
2011-07-07 10:09:02 -04:00
compareNumbers(color1.red, color2.red,
2011-05-05 08:05:39 -04:00
(message || '') + ' red');
compareNumbers(color1.green, color2.green,
2011-05-05 08:05:39 -04:00
(message || '') + ' green');
compareNumbers(color1.blue, color2.blue,
2011-05-05 08:05:39 -04:00
(message || '') + ' blue');
compareNumbers(color1.alpha, color2.alpha,
2011-05-05 08:05:39 -04:00
(message || '') + ' alpha');
}
function compareHsbColors(color1, color2, message) {
color1 = new HsbColor(color1);
color2 = new HsbColor(color2);
2011-07-07 10:09:02 -04:00
compareNumbers(color1.hue, color2.hue,
2011-05-05 08:05:39 -04:00
(message || '') + ' hue');
compareNumbers(color1.saturation, color2.saturation,
2011-05-05 08:05:39 -04:00
(message || '') + ' saturation');
compareNumbers(color1.brightness, color2.brightness,
2011-05-05 08:05:39 -04:00
(message || '') + ' brightness');
compareNumbers(color1.alpha, color2.alpha,
2011-05-05 08:05:39 -04:00
(message || '') + ' alpha');
}
function compareGrayColors(color1, color2, message) {
color1 = new GrayColor(color1);
color2 = new GrayColor(color2);
2011-07-07 10:09:02 -04:00
compareNumbers(color1.gray, color2.gray,
2011-05-05 08:05:39 -04:00
(message || '') + ' gray');
}
function compareGradientColors(gradientColor, gradientColor2, checkIdentity) {
Base.each(['origin', 'destination', 'hilite'], function(key) {
if (checkIdentity) {
equals(function() {
return gradientColor[key] !== gradientColor2[key];
}, true, 'Strict compare GradientColor#' + key);
}
2011-05-21 12:38:49 -04:00
equals(gradientColor[key].toString(), gradientColor2[key].toString(),
'Compare GradientColor#' + key);
});
equals(function() {
2011-05-21 12:38:49 -04:00
return gradientColor.gradient.equals(gradientColor2.gradient);
}, true);
}
function comparePathStyles(style, style2, checkIdentity) {
if (checkIdentity) {
equals(function() {
return style !== style2;
}, true);
}
2011-05-21 12:38:49 -04:00
Base.each(['fillColor', 'strokeColor'], function(key) {
if (style[key]) {
// The color should not point to the same color object:
if (checkIdentity) {
2011-05-21 12:38:49 -04:00
equals(function() {
return style[key] !== style2[key];
}, true, 'The ' + key + ' should not point to the same color object:');
}
if (style[key] instanceof GradientColor) {
if (checkIdentity) {
equals(function() {
return style[key].gradient === style2[key].gradient;
}, true, 'The ' + key + '.gradient should point to the same object:');
}
compareGradientColors(style[key], style2[key], checkIdentity);
2011-05-21 12:38:49 -04:00
} else {
equals(style[key].toString(), style2[key].toString(),
'Compare PathStyle#' + key);
}
}
});
Base.each(['strokeCap', 'strokeJoin', 'dashOffset', 'miterLimit',
'strokeOverprint', 'fillOverprint'], function(key) {
2011-05-21 12:38:49 -04:00
if (style[key]) {
equals(function() {
return style[key] == style2[key];
}, true, 'Compare PathStyle#' + key);
}
});
if (style.dashArray) {
equals(style.dashArray.toString(), style2.dashArray.toString(),
'Compare CharacterStyle#dashArray');
}
}
function compareObjects(name, keys, obj, obj2, checkIdentity) {
if (checkIdentity) {
equals(function() {
return obj !== obj2;
}, true);
}
2011-05-21 12:38:49 -04:00
Base.each(keys, function(key) {
equals(obj[key], obj2[key], 'Compare ' + name + '#' + key);
2011-05-21 12:38:49 -04:00
});
}
2011-05-21 12:38:49 -04:00
function compareCharacterStyles(characterStyle, characterStyle2, checkIdentity) {
compareObjects('CharacterStyle', ['fontSize', 'font'],
characterStyle, characterStyle2, checkIdentity);
2011-05-21 12:38:49 -04:00
}
function compareParagraphStyles(paragraphStyle, paragraphStyle2, checkIdentity) {
compareObjects('ParagraphStyle', ['justification'],
paragraphStyle, paragraphStyle2, checkIdentity);
2011-05-21 12:38:49 -04:00
}
function compareSegmentPoints(segmentPoint, segmentPoint2, checkIdentity) {
compareObjects('SegmentPoint', ['x', 'y', 'selected'],
segmentPoint, segmentPoint2, checkIdentity);
}
function compareSegments(segment, segment2, checkIdentity) {
if (checkIdentity) {
equals(function() {
return segment !== segment2;
}, true);
}
2011-05-21 14:37:25 -04:00
equals(function() {
return segment.selected == segment2.selected;
}, true);
Base.each(['handleIn', 'handleOut', 'point'], function(key) {
2011-05-21 14:37:25 -04:00
compareSegmentPoints(segment[key], segment2[key]);
});
}
function compareSegmentLists(segmentList, segmentList2, checkIdentity) {
if (checkIdentity) {
equals(function() {
return segmentList !== segmentList2;
}, true);
}
equals(segmentList.toString(), segmentList2.toString(),
'Compare Item#segments');
if (checkIdentity) {
for (var i = 0, l = segmentList.length; i < l; i++) {
var segment = segmentList[i],
segment2 = segmentList2[i];
compareSegments(segment, segment2, checkIdentity);
}
}
}
function compareItems(item, item2, cloned, checkIdentity) {
if (checkIdentity) {
equals(function() {
return item !== item2;
}, true);
2011-07-07 10:09:02 -04:00
equals(function() {
return item.id !== item2.id;
}, true);
}
2011-05-21 12:38:49 -04:00
equals(function() {
return item.constructor == item2.constructor;
}, true);
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
'selected', 'clipMask'];
Base.each(itemProperties, function(key) {
var value = item[key];
// When item was cloned and had a name, the name will be versioned
equals(
key == 'name' && cloned && value
? value + ' 1'
: value,
item2[key],
'compare Item#' + key);
});
if (checkIdentity) {
equals(function() {
return item.bounds !== item2.bounds;
}, true);
}
2011-05-21 12:38:49 -04:00
equals(item.bounds.toString(), item2.bounds.toString(),
'Compare Item#bounds');
2011-05-21 14:37:25 -04:00
if (checkIdentity) {
equals(function() {
return item.position !== item2.position;
2011-05-21 14:37:25 -04:00
}, true);
}
equals(item.position.toString(), item2.position.toString(),
'Compare Item#position');
2011-07-07 10:09:02 -04:00
if (item.matrix) {
if (checkIdentity) {
equals(function() {
return item.matrix !== item2.matrix;
}, true);
}
equals(item.matrix.toString(), item2.matrix.toString(),
2011-05-21 12:38:49 -04:00
'Compare Item#matrix');
}
// Path specific
if (item2 instanceof Path) {
2011-05-21 14:37:25 -04:00
var keys = ['closed', 'fullySelected', 'clockwise', 'length'];
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
equals(item[key], item2[key], 'Compare Path#' + key);
2011-05-21 14:37:25 -04:00
}
compareSegmentLists(item.segments, item2.segments, checkIdentity);
}
// Group specific
if (item instanceof Group) {
equals(function() {
2011-05-21 12:38:49 -04:00
return item.clipped == item2.clipped;
}, true);
}
// Layer specific
if (item instanceof Layer) {
equals(function() {
return item.project == item2.project;
}, true);
}
// PlacedSymbol specific
if (item instanceof PlacedSymbol) {
equals(function() {
return item.symbol == item2.symbol;
}, true);
}
// Raster specific
if (item instanceof Raster) {
2011-05-21 12:38:49 -04:00
// TODO: remove access of private fields:
if (item._canvas) {
if (checkIdentity) {
equals(function() {
return item._canvas !== item2._canvas;
}, true);
}
}
if (item._image) {
equals(function() {
2011-05-21 12:38:49 -04:00
return item._image == item2._image;
}, true);
}
equals(item._size.toString(), item2._size.toString(),
2011-05-21 12:38:49 -04:00
'Compare Item#size');
}
// TextItem specific:
if (item instanceof TextItem) {
2011-05-21 12:38:49 -04:00
equals(item.content, item2.content, 'Compare Item#content');
compareCharacterStyles(item.characterStyle, item2.characterStyle,
checkIdentity);
}
2011-07-07 10:09:02 -04:00
// PointText specific:
if (item instanceof PointText) {
if (checkIdentity) {
equals(function() {
return item.point !== item2.point;
}, true);
}
2011-12-20 17:32:28 -05:00
equals(item.point.toString(), item2.point.toString(),
'Compare Item#point');
}
2011-07-07 10:09:02 -04:00
2011-05-21 12:38:49 -04:00
if (item.style) {
// Path Style
comparePathStyles(item.style, item2.style, checkIdentity);
}
// 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++) {
compareItems(item.children[i], item2.children[i], cloned, checkIdentity);
}
}
}
// SVG
function createSvg(xml) {
return new DOMParser().parseFromString('<svg xmlns="http://www.w3.org/2000/svg">' + xml + '</svg>', 'application/xml');
}