import { expect, describe, it } from 'vitest'; import { TOP_LEFT } from '../core/const.js'; import { Point } from './point.js'; import { Rectangle } from './rectangle.js'; describe('Rectangle', (): void => { describe('offset()', (): void => { it('should update x and y properties when dx and dy are provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.offset(10, 20); expect(rect.x).toBe(10); expect(rect.y).toBe(20); }); }); describe('offsetPoint()', (): void => { it('should call offset() with point.x and point.y as arguments when a Point object is provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); const point = new Point(15, 25); rect.offsetPoint(point); expect(rect.x).toBe(15); expect(rect.y).toBe(25); }); }); describe('setTo()', (): void => { it('should update x, y, width and height properties when all arguments are provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.setTo(100, 200, 300, 400); expect(rect.x).toBe(100); expect(rect.y).toBe(200); expect(rect.width).toBe(300); expect(rect.height).toBe(400); }); }); describe('scale()', (): void => { it('should update width and height properties when x is provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.scale(2); expect(rect.width).toBe(20); expect(rect.height).toBe(20); }); it('should update width and height properties when both x and y are provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); const point = new Point(3, 4); rect.scale(point.x, point.y); expect(rect.width).toBe(30); expect(rect.height).toBe(40); }); }); describe('centerOn()', (): void => { it('should update centerX and centerY properties when both x and y are provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); const point = new Point(150, 250); rect.centerOn(point.x, point.y); expect(rect.centerX).toBe(point.x); expect(rect.centerY).toBe(point.y); }); }); describe('floor()', (): void => { it('should update x and y properties by rounding down', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.floor(); expect(rect.x).toBe(Math.floor(rect.x)); expect(rect.y).toBe(Math.floor(rect.y)); }); }); describe('floorAll()', (): void => { it('should update x, y, width and height properties by rounding down', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.floorAll(); expect(rect.x).toBe(Math.floor(rect.x)); expect(rect.y).toBe(Math.floor(rect.y)); expect(rect.width).toBe(Math.floor(rect.width)); expect(rect.height).toBe(Math.floor(rect.height)); }); }); describe('ceil()', (): void => { it('should update x and y properties by rounding up', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.ceil(); expect(rect.x).toBe(Math.ceil(rect.x)); expect(rect.y).toBe(Math.ceil(rect.y)); }); }); describe('ceilAll()', (): void => { it('should update x, y, width and height properties by rounding up', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.ceilAll(); expect(rect.x).toBe(Math.ceil(rect.x)); expect(rect.y).toBe(Math.ceil(rect.y)); expect(rect.width).toBe(Math.ceil(rect.width)); expect(rect.height).toBe(Math.ceil(rect.height)); }); }); describe('copyFrom()', (): void => { it('should return a new Rectangle object with the same properties as the provided rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(500, 600, 700, 800); const copy = rect.copyFrom(source); expect(copy.x).toBe(source.x); expect(copy.y).toBe(source.y); expect(copy.width).toBe(source.width); expect(copy.height).toBe(source.height); }); }); describe('copyTo()', (): void => { it('should update the provided rectangle with the same properties as this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const dest = new Rectangle(); rect.setTo(50, 60, 70, 80); rect.copyTo(dest); expect(dest.x).toBe(rect.x); expect(dest.y).toBe(rect.y); expect(dest.width).toBe(rect.width); expect(dest.height).toBe(rect.height); }); }); describe('inflate()', (): void => { it('should call the inflate() function from another module with this rectangle and dx and dy as arguments', (): void => { const rect = new Rectangle(0, 0, 10, 10); const inflated = rect.inflate(10, 20); expect(inflated.x).toBe(-10); expect(inflated.y).toBe(-20); }); }); describe('size()', (): void => { it('should call the size() function from another module with this rectangle and an output point as arguments', (): void => { const rect = new Rectangle(0, 0, 10, 10); const point = new Point(); const sized = rect.size(point); expect(sized.x).toBe(rect.width); expect(sized.y).toBe(rect.height); }); }); describe('resize()', (): void => { it('should update width and height properties when both x and y are provided', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.resize(120, 140); expect(rect.width).toBe(120); expect(rect.height).toBe(140); }); }); describe('clone()', (): void => { it('should return a new Rectangle object with the same properties as this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const clone = rect.clone(); expect(clone.x).toBe(rect.x); expect(clone.y).toBe(rect.y); expect(clone.width).toBe(rect.width); expect(clone.height).toBe(rect.height); }); }); describe('contains()', (): void => { it('should return true if the point x, y is within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.setTo(100, 200, 300, 400); const contains = rect.contains(150, 250); expect(contains).toBe(true); }); it('should return false if the point x, y is outside this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); rect.setTo(100, 200, 300, 400); const contains = rect.contains(500, 600); expect(contains).toBe(false); }); }); describe('containsRect()', (): void => { it('should return true if the rectangle b is within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const contains = rect.containsRect(source); expect(contains).toBe(false); }); it('should return false if the rectangle b is outside this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(-500, -600, 700, 800); const contains = rect.containsRect(source); expect(contains).toBe(false); }); }); describe('equals()', (): void => { it('should return true if the rectangle b has the same properties as this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const equals = rect.equals(source); expect(equals).toBe(false); }); it('should return false if the rectangle b does not have the same properties as this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(-500, -600, 700, 800); const equals = rect.equals(source); expect(equals).toBe(false); }); }); describe('intersection()', (): void => { it('should return a new Rectangle object that represents the intersection of this rectangle and b', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const intersection = rect.intersection(source); expect(intersection.x).toBe(0); expect(intersection.y).toBe(0); expect(intersection.width).toBe(0); expect(intersection.height).toBe(0); }); }); describe('intersects()', (): void => { it('should return true if the rectangle b intersects with this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const intersects = rect.intersects(source); expect(intersects).toBe(false); }); it('should return false if the rectangle b does not intersect with this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(-500, -600, 700, 800); const intersects = rect.intersects(source); expect(intersects).toBe(true); }); }); describe('intersectsRaw()', (): void => { it('should return true if the rectangle b intersects with this rectangle at left, right, top and bottom', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const intersects = rect.intersectsRaw(-500, -600, -700, -800, 0.1); expect(intersects).toBe(false); }); it('should return false if the rectangle b does not intersect with this rectangle at left, right, top and bottom', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(-500, -600, 700, 800); const intersects = rect.intersectsRaw(0, 10, -1000, 1500, 0.1); expect(intersects).toBe(true); }); }); describe('union()', (): void => { it('should return a new Rectangle object that represents the union of this rectangle and b', (): void => { const rect = new Rectangle(0, 0, 10, 10); const source = new Rectangle(); source.setTo(100, 200, 300, 400); const union = rect.union(source); expect(union.x).toBe(Math.min(rect.left, source.left)); expect(union.y).toBe(Math.min(rect.top, source.top)); expect(union.width).toBe(Math.max(rect.right, source.right) - Math.min(rect.left, source.left)); expect(union.height).toBe(Math.max(rect.bottom, source.bottom) - Math.min(rect.top, source.top)); }); }); describe('random()', (): void => { it('should return a new Point object with x and y coordinates within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const random = rect.random(); expect(random.x >= rect.left && random.x <= rect.right).toBe(true); expect(random.y >= rect.top && random.y <= rect.bottom).toBe(true); }); }); describe('getPoint()', (): void => { it('should return a new Point object at the specified position within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const point = rect.getPoint(TOP_LEFT); expect(point.x).toBe(rect.left); expect(point.y).toBe(rect.top); }); }); describe('toString()', (): void => { it('should return a string representation of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const string = rect.toString(); expect(string).toContain(`x=${rect.x}`); expect(string).toContain(`y=${rect.y}`); expect(string).toContain(`width=${rect.width}`); expect(string).toContain(`height=${rect.height}`); }); }); describe('halfWidth', (): void => { it('should return half the width of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const half = rect.halfWidth; expect(half).toBe(Math.round(rect.width / 2)); }); }); describe('halfHeight', (): void => { it('should return half the height of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const half = rect.halfHeight; expect(half).toBe(Math.round(rect.height / 2)); }); }); describe('top', (): void => { it('should get and set the top coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { top } = rect; expect(top).toBe(rect.y); rect.top = 1000; expect(rect.top).toBe(1000); }); }); describe('left', (): void => { it('should get and set the left coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { left } = rect; expect(left).toBe(rect.x); rect.left = 1500; expect(rect.left).toBe(1500); }); }); describe('right', (): void => { it('should get and set the right coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { right } = rect; expect(right).toBe(rect.x + rect.width); rect.right = 1600; expect(rect.right).toBe(1600); }); }); describe('bottom', (): void => { it('should get and set the bottom coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { bottom } = rect; expect(bottom).toBe(rect.y + rect.height); rect.bottom = 1700; expect(rect.bottom).toBe(1700); }); }); describe('topLeft', (): void => { it('should get and set the top-left coordinates of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { topLeft } = rect; expect(topLeft.x).toBe(rect.left); expect(topLeft.y).toBe(rect.top); rect.topLeft = new Point(1800, 1900); expect(rect.left).toBe(1800); expect(rect.top).toBe(1900); }); }); describe('topRight', (): void => { it('should get and set the top-right coordinates of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { topRight } = rect; expect(topRight.x).toBe(rect.right); expect(topRight.y).toBe(rect.top); rect.topRight = new Point(2000, 2100); expect(rect.right).toBe(2000); expect(rect.top).toBe(2100); }); }); describe('bottomLeft', (): void => { it('should get and set the bottom-left coordinates of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { bottomLeft } = rect; expect(bottomLeft.x).toBe(rect.left); expect(bottomLeft.y).toBe(rect.bottom); rect.bottomLeft = new Point(2200, 2300); expect(rect.left).toBe(2200); expect(rect.bottom).toBe(2300); }); }); describe('bottomRight', (): void => { it('should get and set the bottom-right coordinates of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { bottomRight } = rect; expect(bottomRight.x).toBe(rect.right); expect(bottomRight.y).toBe(rect.bottom); rect.bottomRight = new Point(2400, 2500); expect(rect.right).toBe(2400); expect(rect.bottom).toBe(2500); }); }); describe('centerX', (): void => { it('should get and set the center x coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { centerX } = rect; expect(centerX).toBe(rect.x + rect.halfWidth); rect.centerX = 2600; expect(rect.centerX).toBe(2600); }); }); describe('centerY', (): void => { it('should get and set the center y coordinate of this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { centerY } = rect; expect(centerY).toBe(rect.y + rect.halfHeight); rect.centerY = 2700; expect(rect.centerY).toBe(2700); }); }); describe('randomX', (): void => { it('should return a random x coordinate within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { randomX } = rect; expect(randomX >= rect.left && randomX <= rect.right).toBe(true); }); }); describe('randomY', (): void => { it('should return a random y coordinate within this rectangle', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { randomY } = rect; expect(randomY >= rect.top && randomY <= rect.bottom).toBe(true); }); }); describe('empty', (): void => { it('should get and set whether this rectangle is empty or not', (): void => { const rect = new Rectangle(0, 0, 10, 10); const { empty } = rect; expect(empty).toBe(!rect.width || !rect.height); rect.empty = true; expect(rect.empty).toBe(true); }); }); });