mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Tests: add cloneAndCompare and compareItems helper functions.
This commit is contained in:
parent
013fe56ea4
commit
8f5f5ee43c
1 changed files with 142 additions and 0 deletions
|
@ -86,4 +86,146 @@ function compareGrayColors(color1, color2, message) {
|
|||
|
||||
compareNumbers(color1.gray, color2.gray,
|
||||
(message || '') + ' gray');
|
||||
}
|
||||
|
||||
function cloneAndCompare(item) {
|
||||
var copy = item.clone();
|
||||
compareItems(item, copy);
|
||||
// Remove the cloned item to restore the document:
|
||||
copy.remove();
|
||||
}
|
||||
|
||||
function compareItems(item, item2) {
|
||||
equals(function() {
|
||||
return item != item2;
|
||||
}, true);
|
||||
|
||||
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
|
||||
'closed', 'selected'];
|
||||
Base.each(itemProperties, function(key) {
|
||||
equals(function() {
|
||||
return item[key] == item2[key];
|
||||
}, true, 'item[\'' + key + '\'] == item2[\'' + key + '\']');
|
||||
});
|
||||
|
||||
equals(function() {
|
||||
return item.id != item2.id;
|
||||
}, true);
|
||||
|
||||
if (item._matrix) {
|
||||
equals(function() {
|
||||
return item._matrix != item2._matrix;
|
||||
});
|
||||
equals(item._matrix.toString(), item2._matrix.toString(),
|
||||
'item._matrix.toString() == item2._matrix.toString()');
|
||||
}
|
||||
|
||||
if (item2.segments) {
|
||||
equals(item.segments.toString(), item2.segments.toString(),
|
||||
'item.segments.toString() == item2.segments.toString()');
|
||||
}
|
||||
|
||||
// Path specific
|
||||
if (item instanceof PathItem) {
|
||||
equals(function() {
|
||||
return item._clockwise == item2._clockwise;
|
||||
}, true);
|
||||
}
|
||||
|
||||
// Group specific
|
||||
if (item instanceof Group) {
|
||||
equals(function() {
|
||||
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);
|
||||
}
|
||||
|
||||
// TODO: Raster specific
|
||||
|
||||
// TextItem specific:
|
||||
if (item instanceof TextItem) {
|
||||
equals(item.content, item2.content, 'item.content == item2.content');
|
||||
var characterStyleKeys = ['fontSize', 'font'];
|
||||
Base.each(characterStyleKeys, function(key) {
|
||||
equals(function() {
|
||||
return item2.characterStyle[key];
|
||||
}, item.characterStyle[key], 'item.characterStyle[\'' + key
|
||||
+ '\'] == item2.characterStyle[\'' + key + '\']');
|
||||
});
|
||||
var paragraphStyleKeys = ['justification'];
|
||||
Base.each(paragraphStyleKeys, function(key) {
|
||||
equals(function() {
|
||||
return item2.paragraphStyle[key];
|
||||
}, item.paragraphStyle[key], 'item.paragraphStyle[\'' + key
|
||||
+ '\'] == item2.paragraphStyle[\'' + key + '\']');
|
||||
});
|
||||
}
|
||||
|
||||
// PointText specific:
|
||||
if (item instanceof PointText) {
|
||||
equals(item.point.toString(), item2.point.toString());
|
||||
}
|
||||
|
||||
if (item._style) {
|
||||
// Path Style
|
||||
if (item.fillColor) {
|
||||
// The fillColor should not point to the same color object:
|
||||
equals(function() {
|
||||
return item.fillColor != item2.fillColor;
|
||||
}, true, 'The fillColor should not point to the same color object:');
|
||||
if (item.fillColor instanceof GradientColor) {
|
||||
// TODO!
|
||||
} else {
|
||||
equals(item.fillColor.toString(), item2.fillColor.toString());
|
||||
}
|
||||
}
|
||||
|
||||
if (item.strokeColor) {
|
||||
equals(function() {
|
||||
return item.strokeColor != item2.strokeColor;
|
||||
}, true, 'The strokeColor should not point to the same color object:');
|
||||
if (item.strokeColor instanceof GradientColor) {
|
||||
// TODO
|
||||
} else {
|
||||
equals(item.strokeColor.toString(), item2.strokeColor.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Base.each(['strokeCap', 'strokeJoin', 'dashOffset', 'miterLimit',
|
||||
'strokeOverprint', 'fillOverprint'], function(key) {
|
||||
if (item[key]) {
|
||||
equals(function() {
|
||||
return item[key] == item2[key];
|
||||
}, true, 'item[\'' + key + '\'] == item2[\'' + key + '\']');
|
||||
}
|
||||
});
|
||||
|
||||
if (item.dashArray) {
|
||||
equals(item.dashArray.toString(), item2.dashArray.toString(),
|
||||
'item.dashArray.toString(), item2.dashArray.toString()');
|
||||
}
|
||||
}
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue