2018-08-12 17:02:49 +02:00
|
|
|
let expect = require("chai").expect;
|
2018-08-16 11:58:20 +02:00
|
|
|
let freesewing = require("./dist/index.js");
|
2018-08-12 17:02:49 +02:00
|
|
|
|
|
|
|
it("Svg constructor should initialize object", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
expect(part.paths).to.eql({});
|
|
|
|
expect(part.snippets).to.eql({});
|
|
|
|
expect(part.freeId).to.equal(0);
|
|
|
|
expect(part.topLeft).to.equal(false);
|
|
|
|
expect(part.bottomRight).to.equal(false);
|
|
|
|
expect(part.width).to.equal(false);
|
|
|
|
expect(part.height).to.equal(false);
|
|
|
|
expect(part.render).to.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should return a function from macroClosure", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
expect(typeof part.macroClosure()).to.equal("function");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should no run an unknown macro", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
let macro = part.macroClosure();
|
|
|
|
expect(macro("unknown")).to.equal(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should register and run a macro", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let plugin = {
|
|
|
|
name: "test",
|
|
|
|
version: "0.1-test",
|
|
|
|
macros: {
|
|
|
|
test: function(so) {
|
|
|
|
let points = this.points;
|
|
|
|
points.macro = new this.Point(so.x, so.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-12-22 10:27:05 +01:00
|
|
|
pattern.use(plugin);
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
let macro = part.macroClosure();
|
|
|
|
macro("test", { x: 123, y: 456 });
|
|
|
|
expect(part.points.macro.x).to.equal(123);
|
|
|
|
expect(part.points.macro.y).to.equal(456);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should run debug", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
let debug = part.debugClosure();
|
|
|
|
expect(typeof debug).to.equal("function");
|
|
|
|
let d = 1;
|
|
|
|
let e = 11;
|
|
|
|
let b = 111;
|
|
|
|
let u = 1111;
|
|
|
|
let g = 11111;
|
|
|
|
expect(debug(d, e, b, u, g)).to.equal(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should return a free ID", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-23 15:57:23 +02:00
|
|
|
let free = part.getId();
|
|
|
|
expect(part.getId()).to.equal("" + (parseInt(free) + 1));
|
2018-08-12 17:02:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should return a function from unitsClosure", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
expect(typeof part.unitsClosure()).to.equal("function");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should convert units", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
let units = part.unitsClosure();
|
|
|
|
expect(units(123.456)).to.equal("12.35cm");
|
|
|
|
expect(part.units(123.456)).to.equal("12.35cm");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should set part attributes", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
part.attr("foo", "bar");
|
|
|
|
expect(part.attributes.get("foo")).to.equal("bar");
|
|
|
|
part.attr("foo", "baz");
|
|
|
|
expect(part.attributes.get("foo")).to.equal("bar baz");
|
|
|
|
part.attr("foo", "schmoo", true);
|
|
|
|
expect(part.attributes.get("foo")).to.equal("schmoo");
|
|
|
|
});
|
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
it("Should inject a part", () => {
|
2018-08-12 17:02:49 +02:00
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
part.points.foo = new part.Point(12, 23);
|
|
|
|
let test = new pattern.Part();
|
2018-12-16 18:06:01 +01:00
|
|
|
test.inject(part);
|
2018-08-12 17:02:49 +02:00
|
|
|
expect(test.points.foo.x).to.equal(12);
|
2018-08-31 09:44:12 +02:00
|
|
|
expect(test.points.foo.y).to.equal(23);
|
2018-08-12 17:02:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should return shorthand", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
|
|
|
pattern.settings.paperless = true;
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-12 17:02:49 +02:00
|
|
|
let short = part.shorthand();
|
2018-09-12 17:34:53 +02:00
|
|
|
expect(short.complete).to.equal(true);
|
2018-08-12 17:02:49 +02:00
|
|
|
expect(short.paperless).to.equal(true);
|
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
/*
|
|
|
|
it("Should not allow a margin below 10 for paperless patterns", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.paperless = true;
|
|
|
|
pattern.settings.margin = 2;
|
2018-12-17 12:00:14 +01:00
|
|
|
pattern.parts.a = new pattern.Part();
|
2018-12-16 18:06:01 +01:00
|
|
|
let a = pattern.parts.a;
|
|
|
|
a.points.a = new pattern.Point(0,0);
|
|
|
|
a.points.b = new pattern.Point(0,100);
|
|
|
|
a.paths.a = new pattern.Path()
|
|
|
|
.move(a.points.a)
|
|
|
|
.line(a.points.b);
|
|
|
|
pattern.draft();
|
|
|
|
expect(pattern.width).to.equal(120);
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
it("Should calculate the part boundary with default margin", () => {
|
2018-08-16 11:58:20 +02:00
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-16 11:58:20 +02:00
|
|
|
let short = part.shorthand();
|
|
|
|
part.points.from = new short.Point(123, 456);
|
|
|
|
part.points.to = new short.Point(19, 76);
|
|
|
|
part.paths.test = new short.Path()
|
|
|
|
.move(part.points.from)
|
|
|
|
.line(part.points.to);
|
|
|
|
let boundary = part.boundary();
|
2018-09-11 11:39:50 +02:00
|
|
|
expect(boundary.topLeft.x).to.equal(17);
|
|
|
|
expect(boundary.topLeft.y).to.equal(74);
|
|
|
|
expect(boundary.bottomRight.x).to.equal(125);
|
|
|
|
expect(boundary.bottomRight.y).to.equal(458);
|
2018-08-16 11:58:20 +02:00
|
|
|
boundary = part.boundary();
|
2018-09-11 11:39:50 +02:00
|
|
|
expect(boundary.width).to.equal(108);
|
|
|
|
expect(boundary.height).to.equal(384);
|
2018-08-16 11:58:20 +02:00
|
|
|
});
|
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
it("Should calculate the part boundary with custom margin", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
|
|
|
pattern.settings.margin = 5;
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-12-16 18:06:01 +01:00
|
|
|
let short = part.shorthand();
|
|
|
|
part.points.from = new short.Point(123, 456);
|
|
|
|
part.points.to = new short.Point(19, 76);
|
|
|
|
part.paths.test = new short.Path()
|
|
|
|
.move(part.points.from)
|
|
|
|
.line(part.points.to);
|
|
|
|
let boundary = part.boundary();
|
|
|
|
expect(boundary.topLeft.x).to.equal(14);
|
|
|
|
expect(boundary.topLeft.y).to.equal(71);
|
|
|
|
expect(boundary.bottomRight.x).to.equal(128);
|
|
|
|
expect(boundary.bottomRight.y).to.equal(461);
|
|
|
|
boundary = part.boundary();
|
|
|
|
expect(boundary.width).to.equal(114);
|
|
|
|
expect(boundary.height).to.equal(390);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should calculate the part boundary for paperless", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
|
|
|
pattern.settings.margin = 5;
|
|
|
|
pattern.settings.paperless = true;
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-12-16 18:06:01 +01:00
|
|
|
let short = part.shorthand();
|
|
|
|
part.points.from = new short.Point(123, 456);
|
|
|
|
part.points.to = new short.Point(19, 76);
|
|
|
|
part.paths.test = new short.Path()
|
|
|
|
.move(part.points.from)
|
|
|
|
.line(part.points.to);
|
|
|
|
let boundary = part.boundary();
|
|
|
|
expect(boundary.topLeft.x).to.equal(9);
|
|
|
|
expect(boundary.topLeft.y).to.equal(66);
|
|
|
|
expect(boundary.bottomRight.x).to.equal(133);
|
|
|
|
expect(boundary.bottomRight.y).to.equal(466);
|
|
|
|
boundary = part.boundary();
|
|
|
|
expect(boundary.width).to.equal(124);
|
|
|
|
expect(boundary.height).to.equal(400);
|
|
|
|
});
|
|
|
|
|
2018-08-16 11:58:20 +02:00
|
|
|
it("Should stack a part", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-16 11:58:20 +02:00
|
|
|
let short = part.shorthand();
|
|
|
|
part.points.from = new short.Point(123, 456);
|
|
|
|
part.points.to = new short.Point(19, 76);
|
|
|
|
part.paths.test = new short.Path()
|
|
|
|
.move(part.points.from)
|
|
|
|
.line(part.points.to);
|
|
|
|
part.stack();
|
2018-09-11 11:39:50 +02:00
|
|
|
expect(part.attributes.get("transform")).to.equal("translate(-17, -74)");
|
2018-08-16 11:58:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should only stack a part if needed", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.mode = "draft";
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
2018-08-16 11:58:20 +02:00
|
|
|
let short = part.shorthand();
|
2018-09-11 11:39:50 +02:00
|
|
|
part.points.from = new short.Point(2, 2);
|
2018-08-16 11:58:20 +02:00
|
|
|
part.points.to = new short.Point(19, 76);
|
|
|
|
part.paths.test = new short.Path()
|
|
|
|
.move(part.points.from)
|
|
|
|
.line(part.points.to);
|
|
|
|
part.stack();
|
|
|
|
expect(part.attributes.get("transform")).to.equal(false);
|
|
|
|
part.stack();
|
|
|
|
expect(part.attributes.get("transform")).to.equal(false);
|
|
|
|
});
|