malwoden
Version:
   
90 lines • 4.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rect_1 = require("./rect");
describe("rect", function () {
it("Can create a basic rectangle", function () {
var rect = new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 });
expect(rect.v1).toEqual({ x: 0, y: 0 });
expect(rect.v2).toEqual({ x: 5, y: 10 });
});
it("Will remap v1/v2 to be the min/max corners", function () {
var rect = new rect_1.Rect({ x: 5, y: 10 }, { x: -5, y: -10 });
expect(rect.v1).toEqual({ x: -5, y: -10 });
expect(rect.v2).toEqual({ x: 5, y: 10 });
var rect2 = new rect_1.Rect({ x: 5, y: -10 }, { x: -5, y: 10 });
expect(rect2.v1).toEqual({ x: -5, y: -10 });
expect(rect2.v2).toEqual({ x: 5, y: 10 });
});
it("Can get width and height", function () {
var rect = new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 });
expect(rect.width()).toEqual(6);
expect(rect.height()).toEqual(11);
});
it("Can check if a rectangle intersects", function () {
var tests = [
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
true,
],
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: -5, y: -5 }, { x: 0, y: 0 }),
true,
],
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: -5, y: -5 }, { x: -1, y: -1 }),
false,
],
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: 5, y: -5 }, { x: 5, y: -1 }),
false,
],
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: 5, y: 10 }, { x: 6, y: 11 }),
true,
],
[
new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 10 }),
new rect_1.Rect({ x: 6, y: 11 }, { x: 6, y: 11 }),
false,
],
];
for (var _i = 0, tests_1 = tests; _i < tests_1.length; _i++) {
var _a = tests_1[_i], r1 = _a[0], r2 = _a[1], bool = _a[2];
expect(r1.intersects(r2)).toEqual(bool);
expect(r2.intersects(r1)).toEqual(bool);
}
});
it("can get a center", function () {
var rectEven = new rect_1.Rect({ x: 0, y: 0 }, { x: 5, y: 5 });
var rectOdd = new rect_1.Rect({ x: 0, y: 0 }, { x: 4, y: 4 });
expect(rectOdd.center()).toEqual({ x: 2, y: 2 });
expect(rectEven.center()).toEqual({ x: 2, y: 2 });
});
it("can create a rect from width/height", function () {
var r = rect_1.Rect.FromWidthHeight({ x: 3, y: 5 }, 2, 5);
expect(r.v1).toEqual({ x: 3, y: 5 });
expect(r.v2).toEqual({ x: 4, y: 9 });
});
it("will throw an error on invalid width/height", function () {
expect(function () { return rect_1.Rect.FromWidthHeight({ x: 0, y: 0 }, 0, 0); }).toThrow();
expect(function () { return rect_1.Rect.FromWidthHeight({ x: 0, y: 0 }, 0, 1); }).toThrow();
expect(function () { return rect_1.Rect.FromWidthHeight({ x: 0, y: 0 }, 1, 0); }).toThrow();
});
it("can detect if it contains a point", function () {
var r = new rect_1.Rect({ x: 0, y: 0 }, { x: 10, y: 10 });
expect(r.contains({ x: -1, y: 0 })).toBeFalsy();
expect(r.contains({ x: 0, y: 0 })).toBeTruthy();
expect(r.contains({ x: 10, y: 0 })).toBeTruthy();
expect(r.contains({ x: 11, y: 0 })).toBeFalsy();
expect(r.contains({ x: 5, y: -1 })).toBeFalsy();
expect(r.contains({ x: 5, y: 0 })).toBeTruthy();
expect(r.contains({ x: 5, y: 10 })).toBeTruthy();
expect(r.contains({ x: 5, y: 11 })).toBeFalsy();
});
});
//# sourceMappingURL=rect.spec.js.map