Skip to content

Commit

Permalink
Updated Rectangle() constructor
Browse files Browse the repository at this point in the history
Rectangle.ts - added ?: to all parameters
Rectangle.test.ts - modified tests accordingly
  • Loading branch information
RyanR712 committed Oct 9, 2023
1 parent 37415a9 commit 67e2b21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/AEG/Rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions tests/Rectangle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
});

0 comments on commit 67e2b21

Please sign in to comment.