1
0
Fork 0

Point and Attributes tests

This commit is contained in:
Joost De Cock 2018-08-11 19:17:39 +02:00
parent 1c4507c5bd
commit 7803d1b996
9 changed files with 2935 additions and 134 deletions

View file

@ -8,6 +8,6 @@
] ]
], ],
"plugins": [ "plugins": [
"external-helpers", "syntax-object-rest-spread", "transform-object-rest-spread" "external-helpers", "syntax-object-rest-spread", "transform-object-rest-spread", "istanbul"
] ]
} }

2787
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -20,7 +20,8 @@
"minor": "npm version minor -m ':bookmark: v%s' && npm run build", "minor": "npm version minor -m ':bookmark: v%s' && npm run build",
"major": "npm version major -m ':bookmark: v%s' && npm run build", "major": "npm version major -m ':bookmark: v%s' && npm run build",
"precommit": "npm run pretty && lint-staged", "precommit": "npm run pretty && lint-staged",
"test": "mocha -r ts-node/register tests/*.test.js", "test": "nyc mocha tests/*.test.js",
"report": "nyc report --reporter=html mocha --compilers js:babel-core/register tests/*.test.js",
"clean": "rimraf dist", "clean": "rimraf dist",
"pretty": "npx prettier --write 'src/*.js'", "pretty": "npx prettier --write 'src/*.js'",
"lint": "eslint --fix 'src/*.js'", "lint": "eslint --fix 'src/*.js'",
@ -60,6 +61,7 @@
"husky": "^0.14.3", "husky": "^0.14.3",
"lint-staged": "^7.2.0", "lint-staged": "^7.2.0",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"nyc": "12.0.2",
"prettier": "^1.13.7", "prettier": "^1.13.7",
"rimraf": "^2.6.2", "rimraf": "^2.6.2",
"rollup": "0.63.4", "rollup": "0.63.4",

View file

@ -1,11 +1,5 @@
function Attributes(init = false) { function Attributes() {
this.list = {}; this.list = {};
if (init) {
for (let key in init) {
let val = init[key];
this.add(key, val);
}
}
} }
/** Adds an attribute */ /** Adds an attribute */

View file

@ -422,4 +422,33 @@ Path.prototype.reverse = function() {
return rev; return rev;
}; };
///** Returns all points where this intersects with that */
//Path.prototype.intersectsWith(that) {
// let sections = [];
// let current;
// let closed = false;
// for (let i in this.ops) {
// let op = this.ops[i];
// if (op.type === "line") {
// if (!op.to.sitsOn(current))
// sections.push(new Path().move(op.to).line(current));
// } else if (op.type === "curve") {
// sections.push(new Path().move(op.to).curve(op.cp2, op.cp1, current));
// } else if (op.type === "close") {
// closed = true;
// }
// if (op.to) current = op.to;
// }
// if (closed) rev.close();
//
//}
//
///** Returns an array of paths that make up this path
// * It's basically the opposite of Path.join()
// */
//Path.prototype.divide() {
//
//
//}
export default Path; export default Path;

View file

@ -71,11 +71,6 @@ Point.prototype.copy = function() {
return new Point(this.x, this.y); return new Point(this.x, this.y);
}; };
/** checks whether this point is equal to that point */
Point.prototype.equals = function(that) {
return this.x === that.x && this.y === that.y ? true : false;
};
/** Mirrors this point around X value of that point */ /** Mirrors this point around X value of that point */
Point.prototype.flipX = function(that) { Point.prototype.flipX = function(that) {
return new Point(that.x + this.dx(that), that.y); return new Point(that.x + this.dx(that), that.y);

30
tests/attributes.test.js Normal file
View file

@ -0,0 +1,30 @@
var expect = require("chai").expect;
var Point = require("../dist/index.js").Point;
function newAttr() {
return new Point(0, 0).attributes;
}
it("should return false when getting an unset attribute", () => {
let a = newAttr();
expect(a.get("test")).to.equal(false);
});
it("should render attributes correctly", () => {
let a = newAttr()
.set("class", "test")
.add("class", "render")
.set("transform", "scale(1)");
expect(a.render()).to.equal(' class="test render" transform="scale(1)"');
});
it("should render attributes with given prefix only", () => {
let a = newAttr()
.set("class", "test")
.add("class", "render")
.add("data-text", "foo")
.add("data-text", "bar")
.add("data-mode", "test")
.set("transform", "scale(1)");
expect(a.renderIfPrefixIs("data-")).to.equal(' text="foo bar" mode="test"');
});

View file

@ -1,70 +1,69 @@
var expect = require('chai').expect; var expect = require("chai").expect;
var Point = require('../dist/lib/point').Point; var Point = require("../dist/index.js").Point;
it("should return point object", () => {
it('should return point object', () => { let result = new Point(2, 4);
let result = new Point(2,4); expect(result).to.be.a("object");
expect(result).to.be.a('object');
expect(result.x).to.equal(2); expect(result.x).to.equal(2);
expect(result.y).to.equal(4); expect(result.y).to.equal(4);
}); });
it('should limit point precision', () => { it("should limit point precision", () => {
let result = new Point(2.12345,4.98765); let result = new Point(2.12345, 4.98765);
expect(result.x).to.equal(2.12); expect(result.x).to.equal(2.12);
expect(result.y).to.equal(4.99); expect(result.y).to.equal(4.99);
}); });
it('should return distance', () => { it("should return distance", () => {
expect(new Point(2,4).dist(new Point(-123, -32423))).to.equal(32427.24); expect(new Point(2, 4).dist(new Point(-123, -32423))).to.equal(32427.24);
}); });
it('should return slope', () => { it("should return slope", () => {
let from = new Point(0,0); let from = new Point(0, 0);
expect(from.slope(new Point( 0, -10))).to.equal(-Infinity); expect(from.slope(new Point(0, -10))).to.equal(-Infinity);
expect(from.slope(new Point( 10, 0))).to.equal(0); expect(from.slope(new Point(10, 0))).to.equal(0);
expect(from.slope(new Point( 0, 10))).to.equal(Infinity); expect(from.slope(new Point(0, 10))).to.equal(Infinity);
expect(from.slope(new Point(-10, 0))).to.equal(-0); expect(from.slope(new Point(-10, 0))).to.equal(-0);
expect(from.slope(new Point( 10, 10))).to.equal(1); expect(from.slope(new Point(10, 10))).to.equal(1);
expect(from.slope(new Point(-10, 5))).to.equal(-0.5); expect(from.slope(new Point(-10, 5))).to.equal(-0.5);
}); });
it('should return angle', () => { it("should return angle", () => {
let from = new Point(0,0); let from = new Point(0, 0);
expect(from.angle(new Point( 10, 0))).to.equal(0); expect(from.angle(new Point(10, 0))).to.equal(0);
expect(from.angle(new Point(-20, 0))).to.equal(180); expect(from.angle(new Point(-20, 0))).to.equal(180);
expect(from.angle(new Point( 0, -10))).to.equal(90); expect(from.angle(new Point(0, -10))).to.equal(90);
expect(from.angle(new Point( 0, 10))).to.equal(270); expect(from.angle(new Point(0, 10))).to.equal(270);
expect(from.angle(new Point( 10, -10))).to.equal(45); expect(from.angle(new Point(10, -10))).to.equal(45);
expect(from.angle(new Point( 10, 10))).to.equal(315); expect(from.angle(new Point(10, 10))).to.equal(315);
}); });
it('should copy a point', () => { it("should copy a point", () => {
let result = new Point(2,4).copy(); let result = new Point(2, 4).copy();
expect(result.x).to.equal(2); expect(result.x).to.equal(2);
expect(result.y).to.equal(4); expect(result.y).to.equal(4);
}); });
it('should check points for equality', () => { it("should check points for equality", () => {
let a = new Point(-123,456); let a = new Point(-123, 456);
let b = new Point(-123,456); let b = new Point(-123, 456);
expect(a).to.deep.equal(b); expect(a).to.deep.equal(b);
}); });
it('should flip point around X value', () => { it("should flip point around X value", () => {
let result = new Point(2,4).flipX(new Point(-20, 4)); let result = new Point(2, 4).flipX(new Point(-20, 4));
expect(result.x).to.equal(-42); expect(result.x).to.equal(-42);
expect(result.y).to.equal(4); expect(result.y).to.equal(4);
}); });
it('should flip point around Y value', () => { it("should flip point around Y value", () => {
let result = new Point(2,4).flipY(new Point(2, -14)); let result = new Point(2, 4).flipY(new Point(2, -14));
expect(result.x).to.equal(2); expect(result.x).to.equal(2);
expect(result.y).to.equal(-32); expect(result.y).to.equal(-32);
}); });
it('should shift a point', () => { it("should shift a point", () => {
let origin = new Point(0,0); let origin = new Point(0, 0);
let n = origin.shift(90, 10); let n = origin.shift(90, 10);
let e = origin.shift(0, 10); let e = origin.shift(0, 10);
let s = origin.shift(270, 10); let s = origin.shift(270, 10);
@ -82,12 +81,12 @@ it('should shift a point', () => {
expect(rand.y).to.equal(382.43); expect(rand.y).to.equal(382.43);
}); });
it('should shift a point towards another', () => { it("should shift a point towards another", () => {
let origin = new Point(0,0); let origin = new Point(0, 0);
let n = new Point( 0, -10); let n = new Point(0, -10);
let e = new Point( 10, 0); let e = new Point(10, 0);
let s = new Point( 0, 10); let s = new Point(0, 10);
let w = new Point(-10, 0); let w = new Point(-10, 0);
let sn = origin.shiftTowards(n, 123); let sn = origin.shiftTowards(n, 123);
let se = origin.shiftTowards(e, 123); let se = origin.shiftTowards(e, 123);
let ss = origin.shiftTowards(s, 123); let ss = origin.shiftTowards(s, 123);
@ -104,12 +103,12 @@ it('should shift a point towards another', () => {
expect(ss.shiftTowards(se, 200).y).to.equal(-18.42); expect(ss.shiftTowards(se, 200).y).to.equal(-18.42);
}); });
it('should shift a point a fraction towards another', () => { it("should shift a point a fraction towards another", () => {
let origin = new Point(0,0); let origin = new Point(0, 0);
let n = new Point( 0, -10); let n = new Point(0, -10);
let e = new Point( 10, 0); let e = new Point(10, 0);
let s = new Point( 0, 10); let s = new Point(0, 10);
let w = new Point(-10, 0); let w = new Point(-10, 0);
let sn = origin.shiftFractionTowards(n, 1.5); let sn = origin.shiftFractionTowards(n, 1.5);
let se = origin.shiftFractionTowards(e, 1.5); let se = origin.shiftFractionTowards(e, 1.5);
let ss = origin.shiftFractionTowards(s, 0.5); let ss = origin.shiftFractionTowards(s, 0.5);
@ -126,12 +125,12 @@ it('should shift a point a fraction towards another', () => {
expect(ss.shiftFractionTowards(se, 200).y).to.equal(-994.91); expect(ss.shiftFractionTowards(se, 200).y).to.equal(-994.91);
}); });
it('should shift a point beyond another', () => { it("should shift a point beyond another", () => {
let origin = new Point(0,0); let origin = new Point(0, 0);
let n = new Point( 0, -10); let n = new Point(0, -10);
let e = new Point( 10, 0); let e = new Point(10, 0);
let s = new Point( 0, 10); let s = new Point(0, 10);
let w = new Point(-10, 0); let w = new Point(-10, 0);
let sn = origin.shiftOutwards(n, 100); let sn = origin.shiftOutwards(n, 100);
let se = origin.shiftOutwards(e, 100); let se = origin.shiftOutwards(e, 100);
let ss = origin.shiftOutwards(s, 100); let ss = origin.shiftOutwards(s, 100);
@ -148,9 +147,9 @@ it('should shift a point beyond another', () => {
expect(ss.shiftOutwards(se, 200).y).to.equal(-141.42); expect(ss.shiftOutwards(se, 200).y).to.equal(-141.42);
}); });
it('should rotate a point around another', () => { it("should rotate a point around another", () => {
let sun = new Point(0,0); let sun = new Point(0, 0);
let moon = new Point(10,0); let moon = new Point(10, 0);
let a = moon.rotate(90, sun); let a = moon.rotate(90, sun);
expect(a.x).to.equal(0); expect(a.x).to.equal(0);
expect(a.y).to.equal(-10); expect(a.y).to.equal(-10);
@ -160,9 +159,34 @@ it('should rotate a point around another', () => {
let c = moon.rotate(180, sun); let c = moon.rotate(180, sun);
expect(c.x).to.equal(-10); expect(c.x).to.equal(-10);
expect(c.y).to.equal(0); expect(c.y).to.equal(0);
let sun2 = new Point(222,44); let sun2 = new Point(222, 44);
let moon2 = new Point(212,41); let moon2 = new Point(212, 41);
let d = moon2.rotate(90, sun2); let d = moon2.rotate(90, sun2);
expect(d.x).to.equal(219); expect(d.x).to.equal(219);
expect(d.y).to.equal(54); expect(d.y).to.equal(54);
}); });
it("should set an attribute", () => {
let p = new Point(0, 0).attr("class", "test");
expect(p.attributes.get("class")).to.equal("test");
p.attr("class", "more");
expect(p.attributes.get("class")).to.equal("test more");
p.attr("class", "less", true);
expect(p.attributes.get("class")).to.equal("less");
});
it("should detect points in the same location", () => {
let p1 = new Point(123, 456);
let p2 = new Point(123, 456);
expect(p1.sitsOn(p2)).to.equal(true);
p2.x = 122.99;
expect(p1.sitsOn(p2)).to.equal(false);
});
it("should clone a point", () => {
let p1 = new Point(123, 456).attr("class", "something");
p1.attr("class", "else");
let p2 = p1.clone();
expect(p2.sitsOn(p1)).to.equal(true);
expect(p2.attributes.get("class")).to.equal("something else");
});

View file

@ -1,60 +0,0 @@
var expect = require('chai').expect;
var Point = require('../dist/lib/point').Point;
var utils = require('../dist/lib/utils');
it('should round a value', () => {
expect(utils.round(1.2345)).to.equal(1.23);
expect(utils.round(-9.876)).to.equal(-9.88);
expect(utils.round(12)).to.equal(12);
});
it('should convert radians to degrees', () => {
expect(utils.rad2deg(Math.PI/2)).to.equal(90);
expect(utils.rad2deg(Math.PI)).to.equal(180);
expect(utils.rad2deg(Math.PI*2)).to.equal(360);
});
it('should convert degrees to radians', () => {
expect(utils.deg2rad(90)).to.equal(Math.PI/2);
expect(utils.deg2rad(180)).to.equal(Math.PI);
expect(utils.deg2rad(360)).to.equal(Math.PI*2);
});
it('should return a line intersection', () => {
let a = new Point(-10,-10);
let b = new Point(10,-10);
let c = new Point(10,10);
let d = new Point(-10,10);
let result = utils.beamsCross(a,c,b,d);
expect(result.x).to.equal(0);
expect(result.y).to.equal(0);
expect(utils.beamsCross(a,b,c,d)).to.be.false;
// Debug
let e = new Point(213,222);
let f = new Point(220,214);
let g = new Point(213,44);
let h = new Point(248,222);
result = utils.beamsCross(e,f,g,h);
expect(result.x).to.equal(241.58);
expect(result.y).to.equal(189.34);
});
it('should return a line segment intersection', () => {
let a = new Point(-10,-10);
let b = new Point(10,-10);
let c = new Point(10,10);
let d = new Point(-10,10);
let result = utils.linesCross(a,c,b,d);
expect(result.x).to.equal(0);
expect(result.y).to.equal(0);
expect(utils.linesCross(a,b,c,d)).to.be.false;
let e = new Point(5,-5);
expect(utils.linesCross(a,b,c,e)).to.be.false;
let f = new Point(1,10);
let g = new Point(0,49);
let h = new Point(-20,40);
let i = new Point(20,40);
});