avocado/packages/math/test/rectangle.js
2021-03-19 09:05:51 -05:00

71 lines
2.6 KiB
JavaScript

import {expect} from 'chai';
import * as Rectangle from '../src/rectangle'
describe('Rectangle', function() {
it('can calculate intersections', function() {
expect(Rectangle.intersects([0, 0, 16, 16], [8, 8, 24, 24])).to.be.true;
expect(Rectangle.intersects([0, 0, 16, 16], [16, 16, 32, 32])).to.be.false;
expect(Rectangle.isTouching([0, 0, 16, 16], [0, 0])).to.be.true;
expect(Rectangle.isTouching([0, 0, 16, 16], [16, 16])).to.be.false;
expect(Rectangle.intersection([0, 0, 16, 16], [8, 8, 24, 24])).to.deep.equal([8, 8, 8, 8]);
expect(Rectangle.united([0, 0, 4, 4], [4, 4, 8, 8])).to.deep.equal([0, 0, 12, 12]);
});
it('can compose and decompose', function() {
var rectangle;
rectangle = Rectangle.compose([0, 0], [16, 16]);
expect(Rectangle.equals(rectangle, [0, 0, 16, 16])).to.be.true;
expect(Rectangle.position(rectangle)).to.deep.equal([0, 0]);
expect(Rectangle.size(rectangle)).to.deep.equal([16, 16]);
});
it('can make a deep copy', function() {
var rectangle, rectangle2;
rectangle = [0, 0, 16, 16];
rectangle2 = Rectangle.copy(rectangle);
expect(Rectangle.equals(rectangle, rectangle2)).to.be.true;
rectangle[0] = 6;
expect(Rectangle.equals(rectangle, rectangle2)).to.be.false;
});
it('can convert to an object', function() {
var rectangle;
rectangle = [0, 0, 16, 16];
expect(Rectangle.toObject(rectangle)).to.deep.equal({
x: 0,
y: 0,
width: 16,
height: 16
});
expect(Rectangle.toObject(rectangle, true)).to.deep.equal({
x: 0,
y: 0,
w: 16,
h: 16
});
});
it('can translate by vector', function() {
expect(Rectangle.translated([0, 0, 16, 16], [8, 8])).to.deep.equal([8, 8, 16, 16]);
});
it('can check for null', function() {
expect(Rectangle.isNull(null)).to.be.true;
expect(Rectangle.isNull(3)).to.be.true;
expect(Rectangle.isNull([1])).to.be.true;
expect(Rectangle.isNull([1, 1])).to.be.true;
expect(Rectangle.isNull([1, 1, 1])).to.be.true;
expect(Rectangle.isNull([1, 1, 1, 1, 1])).to.be.true;
expect(Rectangle.isNull([0, 0, 1, 1])).to.be.false;
expect(Rectangle.isNull([0, 0, 1, 0])).to.be.true;
});
it('can round', function() {
expect(Rectangle.round([3.14, 4.70, 5.32, 1.8])).to.deep.equal([3, 5, 5, 2]);
});
it('can floor', function() {
expect(Rectangle.floor([3.14, 4.70, 5.32, 1.8])).to.deep.equal([3, 4, 5, 1]);
});
it('can test inside', () => {
expect(Rectangle.isInside(
[-22.55753963192675, -36.22420629859356, 64, 64],
[-6.557539631926751, -20.224206298593554, 32, 32],
)).to.be.true;
});
});