Tests: Add compareRectangles() function and use it in Rectangle tests.

This commit is contained in:
Jürg Lehni 2011-02-13 14:35:48 +00:00
parent 4b2341b3c2
commit 9c675929b2
2 changed files with 34 additions and 57 deletions

View file

@ -1,5 +1,5 @@
function compareSegmentLists(list1, list2) { function compareSegmentLists(list1, list2) {
equal(list1.length, list2.length, 'segment count'); equals(list1.length, list2.length, 'segment count');
if(list1.length == list2.length) { if(list1.length == list2.length) {
for(var i = 0, l = list1.length; i < l; i++) { for(var i = 0, l = list1.length; i < l; i++) {
compareSegments(list1[i], list2[i]); compareSegments(list1[i], list2[i]);
@ -17,6 +17,15 @@ function compareSegments(segment1, segment2) {
} }
function comparePoints(point1, point2, message) { function comparePoints(point1, point2, message) {
equals(Math.round(point1.x * 100), Math.round(point2.x * 100), message + ' x'); equals(Math.round(point1.x * 100), Math.round(point2.x * 100),
equals(Math.round(point1.y * 100), Math.round(point2.y * 100), message + ' y'); message ? message + ' x' : undefined);
equals(Math.round(point1.y * 100), Math.round(point2.y * 100),
message ? message + ' y' : undefined);
}
function compareRectangles(rect1, rect2) {
equals(rect1.x, rect2.x);
equals(rect1.y, rect2.y);
equals(rect1.width, rect2.width);
equals(rect1.height, rect2.height);
} }

View file

@ -1,42 +1,27 @@
module('Rectangle'); module('Rectangle');
test('new Rectangle(new Point(10, 20), new Size(30, 40));', function() { test('new Rectangle(new Point(10, 20), new Size(30, 40));', function() {
var rect = new Rectangle(new Point(10, 20), new Size(30, 40)); var rect = new Rectangle(new Point(10, 20), new Size(30, 40));
equals(rect.x, 10); compareRectangles(rect, { x: 10, y: 20, width: 30, height: 40 });
equals(rect.y, 20);
equals(rect.width, 30);
equals(rect.height, 40);
}); });
test('new Rectangle([10, 20], [30, 40]);', function() { test('new Rectangle([10, 20], [30, 40]);', function() {
var rect = new Rectangle([10, 20], [30, 40]); var rect = new Rectangle([10, 20], [30, 40]);
equals(rect.x, 10); compareRectangles(rect, { x: 10, y: 20, width: 30, height: 40 });
equals(rect.y, 20);
equals(rect.width, 30);
equals(rect.height, 40);
}); });
test('new Rectangle(new Point(10, 20), new Point(30, 40));', function() { test('new Rectangle(new Point(10, 20), new Point(30, 40));', function() {
var rect = new Rectangle(new Point(10, 20), new Point(30, 40)); var rect = new Rectangle(new Point(10, 20), new Point(30, 40));
equals(rect.x, 10); compareRectangles(rect, { x: 10, y: 20, width: 20, height: 20 });
equals(rect.y, 20);
equals(rect.width, 20);
equals(rect.height, 20);
}); });
test('new Rectangle(10, 20, 30, 40);', function() { test('new Rectangle(10, 20, 30, 40);', function() {
var rect = new Rectangle(10, 20, 30, 40); var rect = new Rectangle(10, 20, 30, 40);
equals(rect.x, 10); compareRectangles(rect, { x: 10, y: 20, width: 30, height: 40 });
equals(rect.y, 20);
equals(rect.width, 30);
equals(rect.height, 40);
}); });
test('new Rectangle({x: 10, y: 20, width: 30, height: 40});', function() { test('new Rectangle({x: 10, y: 20, width: 30, height: 40});', function() {
var rect = new Rectangle({x: 10, y: 20, width: 30, height: 40}); var rect = new Rectangle({x: 10, y: 20, width: 30, height: 40});
equals(rect.x, 10); compareRectangles(rect, { x: 10, y: 20, width: 30, height: 40 });
equals(rect.y, 20);
equals(rect.width, 30);
equals(rect.height, 40);
}); });
test('get size', function() { test('get size', function() {
@ -47,128 +32,111 @@ test('get size', function() {
test('set size', function() { test('set size', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.size = new Size(30, 30); rect.size = new Size(30, 30);
equals(rect.width, 30); compareRectangles(rect, { x: 10, y: 10, width: 30, height: 30 });
equals(rect.height, 30);
}); });
test('topLeft', function() { test('topLeft', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topLeft; var point = rect.topLeft;
equals(point.x, 10); comparePoints(point, { x: 10, y: 10 });
equals(point.y, 10);
}); });
test('set topLeft', function() { test('set topLeft', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.topLeft = [10, 15]; rect.topLeft = [10, 15];
var point = rect.topLeft; var point = rect.topLeft;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get topRight', function() { test('get topRight', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topRight; var point = rect.topRight;
equals(point.x, 30); comparePoints(point, { x: 30, y: 10 });
equals(point.y, 10);
}); });
test('set topRight', function() { test('set topRight', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.topRight = [10, 15]; rect.topRight = [10, 15];
var point = rect.topRight; var point = rect.topRight;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get bottomLeft', function() { test('get bottomLeft', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomLeft; var point = rect.bottomLeft;
equals(point.x, 10); comparePoints(point, { x: 10, y: 30 });
equals(point.y, 30);
}); });
test('set bottomLeft', function() { test('set bottomLeft', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.bottomLeft = [10, 15]; rect.bottomLeft = [10, 15];
var point = rect.bottomLeft; var point = rect.bottomLeft;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get bottomRight', function() { test('get bottomRight', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomRight; var point = rect.bottomRight;
equals(point.x, 30); comparePoints(point, { x: 30, y: 30 });
equals(point.y, 30);
}); });
test('set bottomRight', function() { test('set bottomRight', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.bottomRight = [10, 15]; rect.bottomRight = [10, 15];
var point = rect.bottomRight; var point = rect.bottomRight;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get bottomCenter', function() { test('get bottomCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.bottomCenter; var point = rect.bottomCenter;
equals(point.x, 20); comparePoints(point, { x: 20, y: 30 });
equals(point.y, 30);
}); });
test('set bottomCenter', function() { test('set bottomCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.bottomCenter = [10, 15]; rect.bottomCenter = [10, 15];
var point = rect.bottomCenter; var point = rect.bottomCenter;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get topCenter', function() { test('get topCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.topCenter; var point = rect.topCenter;
equals(point.x, 20); comparePoints(point, { x: 20, y: 10 });
equals(point.y, 10);
}); });
test('set topCenter', function() { test('set topCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.topCenter = [10, 15]; rect.topCenter = [10, 15];
var point = rect.topCenter; var point = rect.topCenter;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get leftCenter', function() { test('get leftCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.leftCenter; var point = rect.leftCenter;
equals(point.x, 10); comparePoints(point, { x: 10, y: 20 });
equals(point.y, 20);
}); });
test('set leftCenter', function() { test('set leftCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.leftCenter = [10, 15]; rect.leftCenter = [10, 15];
var point = rect.leftCenter; var point = rect.leftCenter;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('get rightCenter', function() { test('get rightCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
var point = rect.rightCenter; var point = rect.rightCenter;
equals(point.x, 30); comparePoints(point, { x: 30, y: 20 });
equals(point.y, 20);
}); });
test('set rightCenter', function() { test('set rightCenter', function() {
var rect = new Rectangle(10, 10, 20, 20); var rect = new Rectangle(10, 10, 20, 20);
rect.rightCenter = [10, 15]; rect.rightCenter = [10, 15];
var point = rect.rightCenter; var point = rect.rightCenter;
equals(point.x, 10); comparePoints(point, { x: 10, y: 15 });
equals(point.y, 15);
}); });
test('intersects(rect)', function() { test('intersects(rect)', function() {