✅ Tests for utils
This commit is contained in:
parent
6b1c3fbaaf
commit
9d486fd617
5 changed files with 138 additions and 20 deletions
|
@ -47,14 +47,15 @@ export function beamsCross(a1, a2, b1, b2) {
|
||||||
/** Find intersection of two line segments */
|
/** Find intersection of two line segments */
|
||||||
export function linesCross(a1, a2, b1, b2) {
|
export function linesCross(a1, a2, b1, b2) {
|
||||||
let p = beamsCross(a1, a2, b1, b2);
|
let p = beamsCross(a1, a2, b1, b2);
|
||||||
|
if (!p) return false;
|
||||||
if (p) {
|
if (p) {
|
||||||
let lenA = a1.dist(a2);
|
let lenA = a1.dist(a2);
|
||||||
let lenB = b1.dist(b2);
|
let lenB = b1.dist(b2);
|
||||||
let lenC = a1.dist(p) + p.dist(a2);
|
let lenC = a1.dist(p) + p.dist(a2);
|
||||||
let lenD = b1.dist(p) + p.dist(b2);
|
let lenD = b1.dist(p) + p.dist(b2);
|
||||||
if (round(lenA) == round(lenC) && round(lenB) == round(lenD)) return p;
|
if (round(lenA) == round(lenC) && round(lenB) == round(lenD)) return p;
|
||||||
|
else return false;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Find where an (endless) line crosses a certain Y-value */
|
/** Find where an (endless) line crosses a certain Y-value */
|
||||||
|
|
|
@ -118,22 +118,3 @@ it("Should attach the debug hook", () => {
|
||||||
obj.debug();
|
obj.debug();
|
||||||
expect(count).to.equal(5);
|
expect(count).to.equal(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
//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"');
|
|
||||||
//});
|
|
||||||
|
|
31
tests/snippet.test.js
Normal file
31
tests/snippet.test.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
var freesewing = require("../dist/index.js");
|
||||||
|
|
||||||
|
it("Should create a snippet", () => {
|
||||||
|
var snip1 = new freesewing.Snippet(
|
||||||
|
"test",
|
||||||
|
new freesewing.Point(12, 34),
|
||||||
|
"this is an example"
|
||||||
|
);
|
||||||
|
expect(snip1.def).to.equal("test");
|
||||||
|
expect(snip1.anchor.x).to.equal(12);
|
||||||
|
expect(snip1.anchor.y).to.equal(34);
|
||||||
|
expect(snip1.description).to.equal("this is an example");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should default to empty description", () => {
|
||||||
|
var snip2 = new freesewing.Snippet("test", new freesewing.Point(12, 34));
|
||||||
|
expect(snip2.description).to.equal("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should clone a snippet", () => {
|
||||||
|
var snip3 = new freesewing.Snippet(
|
||||||
|
"boo",
|
||||||
|
new freesewing.Point(56, 78),
|
||||||
|
"clone me"
|
||||||
|
);
|
||||||
|
expect(snip3.clone().def).to.equal("boo");
|
||||||
|
expect(snip3.clone().anchor.x).to.equal(56);
|
||||||
|
expect(snip3.clone().anchor.y).to.equal(78);
|
||||||
|
expect(snip3.clone().description).to.equal("clone me");
|
||||||
|
});
|
20
tests/store.test.js
Normal file
20
tests/store.test.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
var freesewing = require("../dist/index.js");
|
||||||
|
|
||||||
|
var pattern = new freesewing.Pattern();
|
||||||
|
var store = pattern.store;
|
||||||
|
|
||||||
|
it("Store should be a Map", () => {
|
||||||
|
expect(store.data.toString()).to.equal("[object Map]");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should set/get a store value", () => {
|
||||||
|
store.set("foo", "bar");
|
||||||
|
expect(store.get("foo")).to.equal("bar");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should set a store value only if unset", () => {
|
||||||
|
store.setIfUnset("few", "baz");
|
||||||
|
store.setIfUnset("few", "schmoo");
|
||||||
|
expect(store.get("few")).to.equal("baz");
|
||||||
|
});
|
85
tests/utils.test.js
Normal file
85
tests/utils.test.js
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
var expect = require("chai").expect;
|
||||||
|
var freesewing = require("../dist/index.js");
|
||||||
|
var utils = freesewing.utils;
|
||||||
|
|
||||||
|
it("Should return the correct macro name", () => {
|
||||||
|
expect(utils.macroName("test")).to.equal("_macro_test");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should find the intersection of two endless line segments", () => {
|
||||||
|
var a = new freesewing.Point(10, 20);
|
||||||
|
var b = new freesewing.Point(20, 24);
|
||||||
|
var c = new freesewing.Point(90, 19);
|
||||||
|
var d = new freesewing.Point(19, 70);
|
||||||
|
var X = freesewing.utils.beamsCross(a, b, c, d);
|
||||||
|
expect(X.x).to.equal(60.49);
|
||||||
|
expect(X.y).to.equal(40.2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should detect parallel lines", () => {
|
||||||
|
var a = new freesewing.Point(10, 20);
|
||||||
|
var b = new freesewing.Point(20, 20);
|
||||||
|
var c = new freesewing.Point(90, 40);
|
||||||
|
var d = new freesewing.Point(19, 40);
|
||||||
|
expect(freesewing.utils.beamsCross(a, b, c, d)).to.equal(false);
|
||||||
|
expect(freesewing.utils.linesCross(a, b, c, d)).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should detect vertical lines", () => {
|
||||||
|
var a = new freesewing.Point(10, 20);
|
||||||
|
var b = new freesewing.Point(10, 90);
|
||||||
|
var c = new freesewing.Point(90, 40);
|
||||||
|
var d = new freesewing.Point(19, 40);
|
||||||
|
var X = freesewing.utils.beamsCross(a, b, c, d);
|
||||||
|
expect(X.x).to.equal(10);
|
||||||
|
expect(X.y).to.equal(40);
|
||||||
|
var X = freesewing.utils.beamsCross(c, d, a, b);
|
||||||
|
expect(X.x).to.equal(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should swap direction prior to finding beam intersection", () => {
|
||||||
|
var a = new freesewing.Point(10, 20);
|
||||||
|
var b = new freesewing.Point(00, 90);
|
||||||
|
var c = new freesewing.Point(90, 40);
|
||||||
|
var d = new freesewing.Point(19, 40);
|
||||||
|
var X = freesewing.utils.beamsCross(a, b, c, d);
|
||||||
|
expect(X.x).to.equal(7.14);
|
||||||
|
expect(X.y).to.equal(40);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should return false when two lines don't intersect", () => {
|
||||||
|
var a = new freesewing.Point(10, 20);
|
||||||
|
var b = new freesewing.Point(20, 24);
|
||||||
|
var c = new freesewing.Point(90, 19);
|
||||||
|
var d = new freesewing.Point(19, 70);
|
||||||
|
expect(freesewing.utils.linesCross(a, b, c, d)).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should find the intersection of two line segments", () => {
|
||||||
|
var a = new freesewing.Point(10, 10);
|
||||||
|
var b = new freesewing.Point(90, 74);
|
||||||
|
var c = new freesewing.Point(90, 19);
|
||||||
|
var d = new freesewing.Point(11, 70);
|
||||||
|
var X = freesewing.utils.beamsCross(a, b, c, d);
|
||||||
|
expect(X.x).to.equal(51.95);
|
||||||
|
expect(X.y).to.equal(43.56);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should find the intersection of an endles line and a give Y-value", () => {
|
||||||
|
var a = new freesewing.Point(10, 10);
|
||||||
|
var b = new freesewing.Point(90, 74);
|
||||||
|
var X = freesewing.utils.beamCrossesY(a, b, 69);
|
||||||
|
expect(X.x).to.equal(83.75);
|
||||||
|
expect(X.y).to.equal(69);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should detect horizontal lines never pass a give Y-value", () => {
|
||||||
|
var a = new freesewing.Point(10, 10);
|
||||||
|
var b = new freesewing.Point(90, 10);
|
||||||
|
expect(freesewing.utils.beamCrossesY(a, b, 69)).to.equal(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should correctly format units", () => {
|
||||||
|
expect(freesewing.utils.units(123.456)).to.equal("12.35cm");
|
||||||
|
expect(freesewing.utils.units(123.456, "imperial")).to.equal('4.86"');
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue