Tests: Improve compareItems function.

This commit is contained in:
Jonathan Puckey 2011-05-21 18:38:49 +02:00
parent 572b62590e
commit cadace42ce

View file

@ -88,20 +88,20 @@ function compareGrayColors(color1, color2, message) {
(message || '') + ' gray'); (message || '') + ' gray');
} }
function compareGradientColors(color1, color2) { function compareGradientColors(gradientColor, gradientColor2) {
Base.each(['origin', 'destination', 'hilite'], function(key) { Base.each(['origin', 'destination', 'hilite'], function(key) {
equals(color1[key].toString(), color2[key].toString(), equals(gradientColor[key].toString(), gradientColor2[key].toString(),
'color1[' + key + '].toString() == color2[' + key + '].toString()'); 'Compare GradientColor#' + key);
}); });
equals(function() { equals(function() {
return color1.gradient.equals(color2.gradient); return gradientColor.gradient.equals(gradientColor2.gradient);
}, true); }, true);
} }
function cloneAndCompare(item) { function cloneAndCompare(item) {
var copy = item.clone(); var copy = item.clone();
equals(function() { equals(function() {
return item._parent == copy._parent; return item.parent == copy.parent;
}, true); }, true);
equals(function() { equals(function() {
return item.nextSibling == copy; return item.nextSibling == copy;
@ -116,42 +116,94 @@ function cloneAndCompare(item) {
copy.remove(); copy.remove();
} }
function comparePathStyles(style, style2) {
Base.each(['fillColor', 'strokeColor'], function(key) {
if (style[key]) {
// The color should not point to the same color object:
equals(function() {
return style[key] !== style2[key];
}, true, 'The ' + key + ' should not point to the same color object:');
if (style[key] instanceof GradientColor) {
equals(function() {
return style[key].gradient == style2[key].gradient;
}, true, 'The ' + key + '.gradient should point to the same object:');
compareGradientColors(style[key], style2[key]);
} else {
equals(style[key].toString(), style2[key].toString(),
'Compare PathStyle#' + key);
}
}
});
Base.each(['strokeCap', 'strokeJoin', 'dashOffset', 'miterLimit',
'strokeOverprint', 'fillOverprint'], function(key) {
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 compareCharacterStyles(characterStyle, characterStyle2) {
var keys = ['fontSize', 'font'];
Base.each(keys, function(key) {
equals(function() {
return characterStyle[key] == characterStyle2[key];
}, true, 'Compare CharacterStyle#' + key);
});
}
function compareParagraphStyles(paragraphStyle, paragraphStyle2) {
var keys = ['justification'];
Base.each(keys, function(key) {
equals(function() {
return style[key] == style2[key];
}, true, 'Compare ParagraphStyle#' + key);
});
}
function compareItems(item, item2) { function compareItems(item, item2) {
equals(function() { equals(function() {
return item != item2; return item != item2;
}, true); }, true);
equals(function() {
return item.constructor == item2.constructor;
}, true);
var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name', var itemProperties = ['opacity', 'locked', 'visible', 'blendMode', 'name',
'closed', 'selected']; 'closed', 'selected'];
Base.each(itemProperties, function(key) { Base.each(itemProperties, function(key) {
equals(function() { equals(function() {
return item[key] == item2[key]; return item[key] == item2[key];
}, true, 'item[\'' + key + '\'] == item2[\'' + key + '\']'); }, true, 'compare Item#' + key);
}); });
equals(function() { equals(function() {
return item.id != item2.id; return item.id != item2.id;
}, true); }, true);
if (item._matrix) { equals(item.bounds.toString(), item2.bounds.toString(),
equals(function() { 'Compare Item#bounds');
return item._matrix != item2._matrix;
}, true);
equals(item._matrix.toString(), item2._matrix.toString(),
'item._matrix.toString() == item2._matrix.toString()');
}
if (item.matrix) { if (item.matrix) {
equals(function() { equals(function() {
return item.matrix != item2.matrix; return item.matrix != item2.matrix;
}, true); }, true);
equals(item.matrix.toString(), item2.matrix.toString(), equals(item.matrix.toString(), item2.matrix.toString(),
'item.matrix.toString() == item2.matrix.toString()'); 'Compare Item#matrix');
} }
if (item2.segments) { if (item2.segments) {
equals(item.segments.toString(), item2.segments.toString(), equals(item.segments.toString(), item2.segments.toString(),
'item.segments.toString() == item2.segments.toString()'); 'Compare Item#segments');
} }
// Path specific // Path specific
@ -164,7 +216,7 @@ function compareItems(item, item2) {
// Group specific // Group specific
if (item instanceof Group) { if (item instanceof Group) {
equals(function() { equals(function() {
return item._clipped == item2._clipped; return item.clipped == item2.clipped;
}, true); }, true);
} }
@ -184,6 +236,7 @@ function compareItems(item, item2) {
// Raster specific // Raster specific
if (item instanceof Raster) { if (item instanceof Raster) {
// TODO: remove access of private fields:
if (item._canvas) { if (item._canvas) {
equals(function() { equals(function() {
return item._canvas != item2._canvas; return item._canvas != item2._canvas;
@ -191,30 +244,17 @@ function compareItems(item, item2) {
} }
if (item._image) { if (item._image) {
equals(function() { equals(function() {
return item._image = item2._image; return item._image == item2._image;
}, true); }, true);
} }
equals(item._size.toString(), item2._size.toString(), equals(item._size.toString(), item2._size.toString(),
'item._size.toString() == item2._size.toString()'); 'Compare Item#size');
} }
// TextItem specific: // TextItem specific:
if (item instanceof TextItem) { if (item instanceof TextItem) {
equals(item.content, item2.content, 'item.content == item2.content'); equals(item.content, item2.content, 'Compare Item#content');
var characterStyleKeys = ['fontSize', 'font']; compareCharacterStyles(item.characterStyle, item2.characterStyle);
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: // PointText specific:
@ -222,41 +262,9 @@ function compareItems(item, item2) {
equals(item.point.toString(), item2.point.toString()); equals(item.point.toString(), item2.point.toString());
} }
if (item._style) { if (item.style) {
// Path Style // Path Style
comparePathStyles(item.style, item2.style);
Base.each(['fillColor', 'strokeColor'], function(key) {
if (item[key]) {
// The color should not point to the same color object:
equals(function() {
return item[key] !== item2[key];
}, true, 'The ' + key + ' should not point to the same color object:');
if (item[key] instanceof GradientColor) {
equals(function() {
return item[key].gradient == item2[key].gradient;
}, true, 'The ' + key + '.gradient should point to the same object:');
compareGradientColors(item[key], item2[key],
'Compare item[' + key + '] and item2[' + key + ']');
} else {
equals(item[key].toString(), item2[key].toString(),
'item[' + key + '] == item2[' + key + ']');
}
}
});
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: // Check length of children and recursively compare them: