2018-08-12 18:50:48 +02:00
|
|
|
let expect = require("chai").expect;
|
2019-05-31 14:51:59 +02:00
|
|
|
let freesewing = require("./dist");
|
2018-08-12 18:50:48 +02:00
|
|
|
|
|
|
|
it("Pattern constructor should initialize object", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
foo: "bar",
|
|
|
|
options: {
|
|
|
|
constant: 2,
|
2018-09-03 12:20:51 +02:00
|
|
|
percentage: { pct: 30, min: 0, max: 100 }
|
2018-08-12 18:50:48 +02:00
|
|
|
}
|
|
|
|
});
|
2019-01-13 15:00:01 +01:00
|
|
|
expect(pattern.width).to.equal(0);
|
|
|
|
expect(pattern.height).to.equal(0);
|
2018-09-12 17:34:53 +02:00
|
|
|
expect(pattern.settings.complete).to.equal(true);
|
2018-08-12 18:50:48 +02:00
|
|
|
expect(pattern.parts).to.eql({});
|
2018-08-27 16:45:03 +02:00
|
|
|
expect(pattern.settings.units).to.equal("metric");
|
2018-08-12 18:50:48 +02:00
|
|
|
expect(pattern.config.foo).to.equal("bar");
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.options.constant).to.equal(2);
|
|
|
|
expect(pattern.settings.options.percentage).to.equal(0.3);
|
2018-08-12 18:50:48 +02:00
|
|
|
});
|
|
|
|
|
2018-08-16 11:58:20 +02:00
|
|
|
it("Should load percentage options", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
2018-09-03 12:20:51 +02:00
|
|
|
test: { pct: 30 }
|
2018-08-16 11:58:20 +02:00
|
|
|
}
|
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.options.test).to.equal(0.3);
|
2018-08-16 11:58:20 +02:00
|
|
|
});
|
|
|
|
|
2018-09-03 12:20:51 +02:00
|
|
|
it("Should load millimeter options", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test: { mm: 30 }
|
|
|
|
}
|
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.options.test).to.equal(30);
|
2018-09-03 12:20:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should load degree options", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test: { deg: 15 }
|
|
|
|
}
|
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.options.test).to.equal(15);
|
2018-09-03 12:20:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should load an array option", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test: { dflt: "foo" }
|
|
|
|
}
|
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.options.test).to.equal("foo");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should load a count option", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test: { count: 3 }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(pattern.settings.options.test).to.equal(3);
|
2018-09-03 12:20:51 +02:00
|
|
|
});
|
|
|
|
|
2019-04-15 16:09:02 +02:00
|
|
|
it("Should load a boolean option", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test1: { bool: false },
|
|
|
|
test2: { bool: true }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(pattern.settings.options.test1).to.be.false;
|
|
|
|
expect(pattern.settings.options.test2).to.be.true;
|
|
|
|
});
|
|
|
|
|
2018-09-03 12:20:51 +02:00
|
|
|
it("Should throw an error for an unknown option", () => {
|
|
|
|
expect(
|
|
|
|
() =>
|
|
|
|
new freesewing.Pattern({
|
|
|
|
options: {
|
|
|
|
test: { foo: "bar" }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
).to.throw();
|
|
|
|
});
|
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
it("Should merge settings with default settings", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let settings = {
|
|
|
|
foo: "bar",
|
|
|
|
deep: {
|
|
|
|
free: "ze"
|
|
|
|
}
|
|
|
|
};
|
2018-12-22 11:14:42 +01:00
|
|
|
pattern.apply(settings);
|
2018-12-16 18:06:01 +01:00
|
|
|
expect(pattern.settings.foo).to.equal("bar");
|
|
|
|
expect(pattern.settings.locale).to.equal("en");
|
|
|
|
expect(pattern.settings.margin).to.equal(2);
|
|
|
|
expect(pattern.settings.idPrefix).to.equal("fs-");
|
|
|
|
expect(pattern.settings.deep.free).to.equal("ze");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should draft according to settings", () => {
|
2019-04-15 16:09:02 +02:00
|
|
|
const Test = new freesewing.Design({
|
2018-12-16 18:06:01 +01:00
|
|
|
name: "test",
|
|
|
|
dependencies: { back: "front" },
|
|
|
|
inject: { back: "front" },
|
|
|
|
hide: ["back"]
|
2019-04-15 16:09:02 +02:00
|
|
|
});
|
2018-12-16 18:06:01 +01:00
|
|
|
Test.prototype.draftBack = function(part) {
|
|
|
|
this.count++;
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
Test.prototype.draftFront = function(part) {
|
|
|
|
this.count++;
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
|
|
|
|
let pattern = new Test();
|
|
|
|
pattern.count = 0;
|
|
|
|
pattern.draft();
|
|
|
|
expect(pattern.count).to.equal(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should throw an error if per-part draft method is missing", () => {
|
2019-04-15 16:09:02 +02:00
|
|
|
const Test = new freesewing.Design({
|
2018-12-16 18:06:01 +01:00
|
|
|
name: "test",
|
|
|
|
dependencies: { back: "front" },
|
|
|
|
inject: { back: "front" },
|
|
|
|
hide: ["back"]
|
2019-04-15 16:09:02 +02:00
|
|
|
});
|
|
|
|
Test.prototype.draftBack = part => part;
|
|
|
|
let pattern = new Test();
|
|
|
|
expect(() => pattern.draft()).to.throw();
|
|
|
|
});
|
|
|
|
|
2018-08-12 18:50:48 +02:00
|
|
|
it("Should sample an option", () => {
|
|
|
|
let pattern = new freesewing.Pattern({
|
|
|
|
options: {
|
2018-09-03 12:20:51 +02:00
|
|
|
len: { pct: 30, min: 0, max: 100 },
|
2018-08-12 18:50:48 +02:00
|
|
|
bonus: 10
|
|
|
|
}
|
|
|
|
});
|
|
|
|
pattern.draft = function() {
|
2018-12-17 12:00:14 +01:00
|
|
|
pattern.parts.a = new pattern.Part();
|
|
|
|
pattern.parts.b = new pattern.Part();
|
2018-08-12 18:50:48 +02:00
|
|
|
let a = pattern.parts.a;
|
|
|
|
a.points.from = new a.Point(0, 0);
|
|
|
|
a.points.to = new a.Point(
|
2018-12-16 18:06:01 +01:00
|
|
|
100 * a.context.settings.options.len,
|
|
|
|
a.context.settings.options.bonus
|
2018-08-12 18:50:48 +02:00
|
|
|
);
|
|
|
|
a.paths.test = new a.Path().move(a.points.from).line(a.points.to);
|
2018-12-16 18:06:01 +01:00
|
|
|
pattern.parts.b.inject(a);
|
2018-08-12 18:50:48 +02:00
|
|
|
};
|
|
|
|
pattern.settings.sample = {
|
|
|
|
type: "option",
|
|
|
|
option: "len"
|
|
|
|
};
|
|
|
|
pattern.sample();
|
|
|
|
expect(pattern.parts.a.paths.test_1.render).to.equal(true);
|
|
|
|
expect(pattern.parts.b.paths.test_10.ops[1].to.y).to.equal(10);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should register a hook via on", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let count = 0;
|
2018-09-30 17:03:24 +02:00
|
|
|
pattern._draft = () => {};
|
2018-12-09 14:17:46 +01:00
|
|
|
pattern.on("preDraft", function(pattern) {
|
2018-08-12 18:50:48 +02:00
|
|
|
count++;
|
|
|
|
});
|
|
|
|
pattern.draft();
|
|
|
|
expect(count).to.equal(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should register a hook from a plugin", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let count = 0;
|
2018-09-30 17:03:24 +02:00
|
|
|
pattern._draft = () => {};
|
2018-08-12 18:50:48 +02:00
|
|
|
let plugin = {
|
|
|
|
name: "test",
|
|
|
|
version: "0.1-test",
|
|
|
|
hooks: {
|
2018-12-09 14:17:46 +01:00
|
|
|
preDraft: function(pattern) {
|
2018-08-12 18:50:48 +02:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-12-22 10:27:05 +01:00
|
|
|
pattern.use(plugin);
|
2018-08-12 18:50:48 +02:00
|
|
|
pattern.draft();
|
|
|
|
expect(count).to.equal(1);
|
|
|
|
});
|
2018-08-15 18:49:55 +02:00
|
|
|
|
2018-12-17 14:39:50 +01:00
|
|
|
it("Should register multiple methods on a single hook", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let count = 0;
|
|
|
|
pattern._draft = () => {};
|
|
|
|
let plugin = {
|
|
|
|
name: "test",
|
|
|
|
version: "0.1-test",
|
|
|
|
hooks: {
|
|
|
|
preDraft: [
|
|
|
|
function(pattern) {
|
|
|
|
count++;
|
|
|
|
},
|
|
|
|
function(pattern) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2018-12-22 10:27:05 +01:00
|
|
|
pattern.use(plugin);
|
2018-12-17 14:39:50 +01:00
|
|
|
pattern.draft();
|
|
|
|
expect(count).to.equal(2);
|
|
|
|
});
|
|
|
|
|
2018-08-16 12:09:57 +02:00
|
|
|
it("Should check whether a part is needed", () => {
|
2018-12-16 18:06:01 +01:00
|
|
|
let config = {
|
|
|
|
name: "test",
|
|
|
|
dependencies: { back: "front", side: "back" },
|
|
|
|
inject: { back: "front" },
|
|
|
|
hide: ["back"]
|
|
|
|
};
|
|
|
|
const Test = function(settings = false) {
|
|
|
|
freesewing.Pattern.call(this, config);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
Test.prototype = Object.create(freesewing.Pattern.prototype);
|
|
|
|
Test.prototype.constructor = Test;
|
|
|
|
Test.prototype.draftBack = function(part) {
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
Test.prototype.draftFront = function(part) {
|
|
|
|
return part;
|
|
|
|
};
|
2018-08-15 18:49:55 +02:00
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
let pattern = new Test();
|
|
|
|
pattern.settings.only = "back";
|
|
|
|
expect(pattern.needs("back")).to.equal(true);
|
|
|
|
expect(pattern.needs("front")).to.equal(true);
|
|
|
|
expect(pattern.needs("side")).to.equal(false);
|
|
|
|
pattern.settings.only = ["back", "side"];
|
|
|
|
expect(pattern.needs("back")).to.equal(true);
|
|
|
|
expect(pattern.needs("front")).to.equal(true);
|
|
|
|
expect(pattern.needs("side")).to.equal(true);
|
2018-08-15 18:49:55 +02:00
|
|
|
});
|
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
it("Should check whether a part is wanted", () => {
|
|
|
|
let config = {
|
|
|
|
name: "test",
|
|
|
|
dependencies: { back: "front", side: "back" },
|
|
|
|
inject: { back: "front" },
|
|
|
|
hide: ["back"]
|
|
|
|
};
|
|
|
|
const Test = function(settings = false) {
|
|
|
|
freesewing.Pattern.call(this, config);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
Test.prototype = Object.create(freesewing.Pattern.prototype);
|
|
|
|
Test.prototype.constructor = Test;
|
|
|
|
Test.prototype.draftBack = function(part) {
|
|
|
|
return part;
|
|
|
|
};
|
|
|
|
Test.prototype.draftFront = function(part) {
|
|
|
|
return part;
|
|
|
|
};
|
2018-08-20 12:16:13 +02:00
|
|
|
|
2018-12-16 18:06:01 +01:00
|
|
|
let pattern = new Test();
|
|
|
|
pattern.settings.only = "back";
|
|
|
|
expect(pattern.wants("back")).to.equal(true);
|
|
|
|
expect(pattern.wants("front")).to.equal(false);
|
|
|
|
expect(pattern.wants("side")).to.equal(false);
|
|
|
|
pattern.settings.only = ["back", "side"];
|
|
|
|
expect(pattern.wants("back")).to.equal(true);
|
|
|
|
expect(pattern.wants("front")).to.equal(false);
|
|
|
|
expect(pattern.wants("side")).to.equal(true);
|
2018-08-15 18:49:55 +02:00
|
|
|
});
|
2018-12-09 14:17:46 +01:00
|
|
|
|
|
|
|
it("Should check whether created parts get the pattern context", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
2018-12-17 12:00:14 +01:00
|
|
|
let part = new pattern.Part();
|
|
|
|
expect(part.context.settings).to.equal(pattern.settings);
|
2018-12-09 14:17:46 +01:00
|
|
|
});
|
2018-12-17 14:39:50 +01:00
|
|
|
|
|
|
|
it("Should correctly merge settings", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
let settings = {
|
|
|
|
complete: false,
|
|
|
|
only: [1, 2, 3],
|
|
|
|
margin: 5
|
|
|
|
};
|
2018-12-22 11:14:42 +01:00
|
|
|
pattern.apply(settings);
|
2018-12-17 14:39:50 +01:00
|
|
|
expect(pattern.settings.complete).to.equal(false);
|
|
|
|
expect(pattern.settings.only[1]).to.equal(2);
|
|
|
|
expect(pattern.settings.margin).to.equal(5);
|
|
|
|
expect(pattern.settings.only.length).to.equal(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should correctly merge settings for existing array", () => {
|
|
|
|
let pattern = new freesewing.Pattern();
|
|
|
|
pattern.settings.only = [1];
|
|
|
|
let settings = {
|
|
|
|
complete: false,
|
|
|
|
only: [2, 3, 4],
|
|
|
|
margin: 5
|
|
|
|
};
|
2018-12-22 11:14:42 +01:00
|
|
|
pattern.apply(settings);
|
2018-12-17 14:39:50 +01:00
|
|
|
expect(pattern.settings.complete).to.equal(false);
|
|
|
|
expect(pattern.settings.only.length).to.equal(4);
|
|
|
|
expect(pattern.settings.margin).to.equal(5);
|
|
|
|
});
|