From 67e2b2144b8af88a1da1378ddf310d8f4e7287d6 Mon Sep 17 00:00:00 2001 From: Ryan Reilly Date: Mon, 9 Oct 2023 16:51:37 -0400 Subject: [PATCH] Updated Rectangle() constructor Rectangle.ts - added ?: to all parameters Rectangle.test.ts - modified tests accordingly --- src/AEG/Rectangle.ts | 14 +++++++------- tests/Rectangle.test.ts | 6 ++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/AEG/Rectangle.ts b/src/AEG/Rectangle.ts index c2445557..8876d621 100644 --- a/src/AEG/Rectangle.ts +++ b/src/AEG/Rectangle.ts @@ -29,19 +29,19 @@ export class Rectangle { * @param w The width of the rectangle. * @param h The height of the rectangle. */ - public constructor(vertex: Point | null, w: number, h: number) { + public constructor(vertex?: Point, w?: number, h?: number) { if (!Number.isFinite(w) || !Number.isFinite(h)) { throw new Error( "Infinity/NaN passed in for width/height while constructing a Rectangle." ); - } else if (w <= 0) { - throw new Error("Nonpositive value passed for width while constructing a Rectangle."); - } else if (h <= 0) { - throw new Error("Nonpositive value passed for height while constructing a Rectangle."); + } else if (w !== undefined && w < 0) { + throw new Error("Negative value passed for width while constructing a Rectangle."); + } else if (h !== undefined && h < 0) { + throw new Error("Negative value passed for height while constructing a Rectangle."); } this.startVertex = vertex ?? new Point(0, 0); - this.width = w; - this.height = h; + this.width = w ?? 0; + this.height = h ?? 0; } /** diff --git a/tests/Rectangle.test.ts b/tests/Rectangle.test.ts index 0c463246..8fc478bf 100644 --- a/tests/Rectangle.test.ts +++ b/tests/Rectangle.test.ts @@ -10,7 +10,7 @@ import {Point} from "../src/AEG/Point"; describe("Rectangle constructor soliloquy:", () => { const expectedVertex = new Point(0, 0); test.each([ - [new Rectangle(null, 1, 1), expectedVertex], + [new Rectangle(new Point(0, 0), 1, 1), expectedVertex], [new Rectangle(new Point(0, 0), 5, 5), expectedVertex], ])("These constructors should produce a startingVertex of (0, 0).", (r, expectedVertex) => { expect(r.startVertex).toStrictEqual(expectedVertex); @@ -19,14 +19,12 @@ describe("Rectangle constructor soliloquy:", () => { test.fails.each([ [-1, 1], [1, -1], - [0, 1], - [1, 0], [NaN, NaN], [Infinity, Infinity], [-Infinity, Infinity], [Infinity, -Infinity], [-Infinity, -Infinity], ])("Rectangle constructions of vertex (0, 0), w: %i, h: %i throw errors.", (w, h) => { - new Rectangle(null, w, h); + new Rectangle(new Point(0, 0), w, h); }); });