Skip to content

Commit

Permalink
Point
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Dec 9, 2019
1 parent 2b4c2f4 commit ce172af
Showing 1 changed file with 90 additions and 6 deletions.
96 changes: 90 additions & 6 deletions spec/geometry/point.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,92 @@
import { Point, toPoint } from "../../src/geometry/Point";

describe('Point', () => {
let x = 1.1;
let y = 1.1;
let x = 1.5;
let y = 1.5;
let p = new Point(x, y);

describe('#clone', () => {
expect(p.clone()).not.toBe(p);
expect(p.clone()).toEqual(p);
});

describe('#add', () => {
expect(p.add(p)).not.toBe(p);
expect(p.add(p)).toEqual(new Point(2.0 * x, 2.0 * y));
});

describe('#subtract', () => {
expect(p.subtract(p)).not.toBe(p);
expect(p.subtract(p)).toEqual(new Point(0.0, 0.0));
});

describe('#divideBy', () => {
expect(p.divideBy(2.0)).not.toBe(p);
expect(p.divideBy(2.0)).toEqual(new Point(x / 2.0, y / 2.0));
});

describe('#multiplyBy', () => {
expect(p.multiplyBy(2.0)).not.toBe(p);
expect(p.multiplyBy(2.0)).toEqual(new Point(2.0 * x, 2.0 * y));
});

describe('#scaleBy', () => {
let s = new Point(2.0, 0.5);

expect(p.scaleBy(s)).not.toBe(p);
expect(p.scaleBy(s)).toEqual(new Point(2.0 * x, 0.5 * y));
});

describe('#unscaleBy', () => {
let s = new Point(2.0, 0.5);

expect(p.unscaleBy(s)).not.toBe(p);
expect(p.unscaleBy(s)).toEqual(new Point(x / 2.0, y / 0.5));
});

describe('#round', () => {
expect(p.round()).not.toBe(p);
expect(p.round()).toEqual(new Point(x, y, true));
});

describe('#floor', () => {
expect(p.floor()).not.toBe(p);
expect(p.floor()).toEqual(new Point(Math.floor(x), Math.floor(y)));
});

describe('#ceil', () => {
expect(p.ceil()).not.toBe(p);
expect(p.ceil()).toEqual(new Point(Math.ceil(x), Math.ceil(y)));
});

describe('#distanceTo', () => {
expect(p.distanceTo(p.clone())).toEqual(0.0);
expect(p.distanceTo(new Point(0.0, 0.0))).toEqual(Math.sqrt(x * x + y * y));
expect(p.distanceTo(p.multiplyBy(2.0))).toEqual(Math.sqrt(x * x + y * y));
});

describe('#equals', () => {
expect(p.equals(p.clone())).toBeTrue();
expect(p.equals(p.multiplyBy(2.0))).toBeFalse();
});

describe('#contains', () => {
expect(p.contains(p.clone())).toBeTrue();
expect(p.contains(p.multiplyBy(2.0))).toBeFalse();
expect(p.contains(p.divideBy(2.0))).toBeTrue();
});

describe('#toString', () => {
expect(p.toString()).toEqual("Point(1.5, 1.5)");
});

describe('toPoint', () => {
it('instance of Point', () => {
let point = new Point(x, y);
let p = toPoint(point);

expect(p instanceof Point).toBeTrue();
expect(p).toEqual(point);
expect(p).toBe(point);
});

it('two element array', () => {
Expand Down Expand Up @@ -39,23 +115,31 @@ describe('Point', () => {
});

it('x, y', () => {
let p = new Point(x, y);
let p = toPoint(x, y);

expect(p instanceof Point).toBeTrue();
expect(p.x).toStrictEqual(x);
expect(p.y).toStrictEqual(y);
});

it('x, y, true', () => {
let p = new Point(x, y, true);
let p = toPoint(x, y, true);

expect(p instanceof Point).toBeTrue();
expect(p.x).toStrictEqual(Math.round(x));
expect(p.y).toStrictEqual(Math.round(y));
});

it('string x, string y', () => {
let p = new Point('x', 'y');
let p = toPoint('x', 'y');

expect(p instanceof Point).toBeTrue();
expect(p.x).toBeNaN();
expect(p.y).toBeNaN();
});

it('string x, string y, true', () => {
let p = toPoint('x', 'y', true);

expect(p instanceof Point).toBeTrue();
expect(p.x).toBeNaN();
Expand Down

0 comments on commit ce172af

Please sign in to comment.