1
0
Fork 0

wip(core): Work on porting unit tests to v3

This commit is contained in:
Joost De Cock 2022-08-27 09:27:07 +02:00
parent 3671f44e90
commit ba7c663941
13 changed files with 4392 additions and 4470 deletions

View file

@ -6,6 +6,8 @@ const expect = chai.expect
const newAttr = () => new Point(0, 0).attributes;
const a = newAttr();
describe('Attributes', () => {
it("Should set an attribute", () => {
a.set('hold', 'me')
expect(a.get("hold")).to.equal('me');
@ -80,4 +82,5 @@ it("Should return attributes as props and handle special class case", () => {
const props = a.asPropsIfPrefixIs('')
expect(props.className).to.equal("fabric");
});
})

View file

@ -3,8 +3,9 @@ import freesewing from "./dist/index.mjs"
const expect = chai.expect
describe('Design', () => {
it("Design constructor should return pattern constructor", () => {
let design = new freesewing.Design({
const design = new freesewing.Design({
foo: "bar",
options: {
constant: 2,
@ -12,7 +13,7 @@ it("Design constructor should return pattern constructor", () => {
}
});
let pattern = new design();
const pattern = new design();
expect(pattern.width).to.equal(0);
expect(pattern.height).to.equal(0);
expect(pattern.settings.complete).to.equal(true);
@ -23,23 +24,7 @@ it("Design constructor should return pattern constructor", () => {
expect(pattern.settings.options.percentage).to.equal(0.3);
});
it("Design constructor should load single plugin (legacy)", () => {
let plugin = {
name: "example",
version: 1,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example", version);
}
}
};
let design = new freesewing.Design({}, plugin);
let pattern = new design();
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should load single plugin (2022)", () => {
it("Design constructor should load single plugin", () => {
let plugin = {
name: "example",
version: 1,
@ -55,32 +40,7 @@ it("Design constructor should load single plugin (2022)", () => {
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should load array of plugins (legacy)", () => {
let plugin1 = {
name: "example1",
version: 1,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example1", version);
}
}
};
let plugin2 = {
name: "example2",
version: 2,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example2", version);
}
}
};
let design = new freesewing.Design({}, [plugin1, plugin2]);
let pattern = new design();
expect(pattern.hooks.preRender.length).to.equal(2);
});
it("Design constructor should load array of plugins (2022)", () => {
it("Design constructor should load array of plugins", () => {
let plugin1 = {
name: "example1",
version: 1,
@ -105,23 +65,7 @@ it("Design constructor should load array of plugins (2022)", () => {
expect(pattern.hooks.preRender.length).to.equal(2);
});
it("Design constructor should load conditional plugin (legacy)", () => {
const plugin = {
name: "example",
version: 1,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example", version);
}
}
};
const condition = () => true
const design = new freesewing.Design({}, [], { plugin, condition });
const pattern = new design();
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should load conditional plugin (2022)", () => {
it("Design constructor should load conditional plugin", () => {
const plugin = {
name: "example",
version: 1,
@ -137,23 +81,7 @@ it("Design constructor should load conditional plugin (2022)", () => {
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should not load conditional plugin (legacy)", () => {
const plugin = {
name: "example",
version: 1,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example", version);
}
}
};
const condition = () => false
const design = new freesewing.Design({}, [], { plugin, condition });
const pattern = new design();
expect(pattern.hooks.preRender.length).to.equal(0);
});
it("Design constructor should not load conditional plugin (2022)", () => {
it("Design constructor should not load conditional plugin", () => {
const plugin = {
name: "example",
version: 1,
@ -169,27 +97,7 @@ it("Design constructor should not load conditional plugin (2022)", () => {
expect(pattern.hooks.preRender.length).to.equal(0);
});
it("Design constructor should load multiple conditional plugins (legacy)", () => {
const plugin = {
name: "example",
version: 1,
hooks: {
preRender: function(svg, attributes) {
svg.attributes.add("freesewing:plugin-example", version);
}
}
};
const condition1 = () => true
const condition2 = () => false
const design = new freesewing.Design({}, [], [
{ plugin, condition: condition1 },
{ plugin, condition: condition2 },
]);
const pattern = new design();
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should load multiple conditional plugins (2022)", () => {
it("Design constructor should load multiple conditional plugins", () => {
const plugin = {
name: "example",
version: 1,
@ -209,19 +117,7 @@ it("Design constructor should load multiple conditional plugins (2022)", () => {
expect(pattern.hooks.preRender.length).to.equal(1);
});
it("Design constructor should construct basic part order", () => {
let design = new freesewing.Design({
dependencies: { step4: "step3" },
inject: { step4: "step3" },
parts: ["step1", "step2"]
});
let pattern = new design().init();
expect(pattern.config.draftOrder[0]).to.equal("step3");
expect(pattern.config.draftOrder[1]).to.equal("step4");
expect(pattern.config.draftOrder[2]).to.equal("step1");
expect(pattern.config.draftOrder[3]).to.equal("step2");
});
/*
it("Design constructor should not require depencies for injected parts", () => {
let design = new freesewing.Design({
inject: { step4: "step3" },
@ -304,6 +200,8 @@ it("Design constructor should handle Simon", () => {
let pattern = new design().init();
});
*/
it("Pattern constructor should add default hide() method to options", () => {
const design = new freesewing.Design({
foo: "bar",
@ -329,18 +227,5 @@ it("Pattern constructor should add default hide() method to options", () => {
expect(pattern.config.options.withHide.hide(pattern.settings)).to.be.true
})
it("Should warn when passing plugins both as parameter and in the config", () => {
const design = new freesewing.Design({plugins: [{}]}, {});
expect(design.config.warnings.length).to.equal(2)
expect(design.config.warnings[0]).to.equal('Passing plugins to the Design constructor both as a second parameter and in the config is unsupported')
expect(design.config.warnings[1]).to.equal('Ignoring plugins passed as parameter. Only config.plugins will be used.')
})
it("Should warn when passing conditionalPlugins both as parameter and in the config", () => {
const design = new freesewing.Design({conditionalPlugins: [{}]}, false, {});
expect(design.config.warnings.length).to.equal(2)
expect(design.config.warnings[0]).to.equal('Passing conditionalPlugins to the Design constructor both as a third parameter and in the config is unsupported.')
expect(design.config.warnings[1]).to.equal('Ignoring conditionalPlugins passes as parameter. Only config.conditionalPlugins will be used.')
})

View file

@ -3,10 +3,11 @@ import { Pattern } from "./dist/index.mjs"
const expect = chai.expect
describe('Hooks', () => {
it("Should contain all hooks", () => {
let pattern = new Pattern();
let h = pattern.hooks;
let test = {
const pattern = new Pattern();
const h = pattern.hooks;
const test = {
preDraft: [],
postDraft: [],
postLayout: [],
@ -18,3 +19,4 @@ it("Should contain all hooks", () => {
};
expect(h).to.eql(test);
});
});

View file

@ -247,6 +247,7 @@ it("Should run hooks", () => {
it("Should get the units closure to raise a debug when passing a non-number", () => {
const pattern = new freesewing.Pattern();
pattern.settings.mode = "draft";
pattern.settings.debug = true
const part = new pattern.Part();
const short = part.shorthand();
short.units('a')

View file

@ -4,6 +4,7 @@ import freesewing from "./dist/index.mjs"
const expect = chai.expect
const round = freesewing.utils.round
describe('Path', () => {
it("Should offset a line", () => {
let pattern = new freesewing.Pattern();
pattern.parts.a = new pattern.Part();
@ -1171,3 +1172,4 @@ it("Should raise a warning when splitting a path on a non-point", () => {
}
expect(invalid).to.equal(true);
});
});

View file

@ -3,6 +3,7 @@ import freesewing from "./dist/index.mjs"
const expect = chai.expect
describe('Pattern', () => {
it("Pattern constructor should initialize object", () => {
let pattern = new freesewing.Pattern({
foo: "bar",
@ -106,37 +107,31 @@ it("Should merge settings with default settings", () => {
});
it("Should draft according to settings", () => {
let count = 0
const back = {
name: 'back',
hide: true,
draft: function(part) {
count ++
return part;
}
}
const front = {
name: 'front',
from: back,
draft: function(part) {
count ++
return part;
}
}
const Test = new freesewing.Design({
name: "test",
dependencies: { back: "front" },
inject: { back: "front" },
hide: ["back"]
parts: [ back, front ],
});
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;
const pattern = new Test();
pattern.draft();
expect(pattern.count).to.equal(2);
});
it("Should throw an error if per-part draft method is missing", () => {
const Test = new freesewing.Design({
name: "test",
dependencies: { back: "front" },
inject: { back: "front" },
hide: ["back"]
});
Test.prototype.draftBack = part => part;
let pattern = new Test();
expect(() => pattern.draft()).to.throw();
expect(count).to.equal(2);
});
it("Should sample an option", () => {
@ -166,19 +161,17 @@ it("Should sample an option", () => {
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 sample a list option", () => {
const Test = new freesewing.Design({
name: "test",
parts: ['front'],
const front = {
name: 'front',
options: {
len: {
dflt: 1,
list: [1,2,3]
}
},
})
Test.prototype.draftFront = function(part) {
draft: function(part) {
const { Point, points, Path, paths, options } = part.shorthand()
points.from = new Point(0, 0);
points.to = new Point( 100 * options.len, 0)
@ -187,7 +180,12 @@ it("Should sample a list option", () => {
.line(points.to)
return part
};
}
}
const Test = new freesewing.Design({
name: "test",
parts: [front],
})
const pattern = new Test({
sample: {
type: 'option',
@ -195,6 +193,7 @@ it("Should sample a list option", () => {
}
})
pattern.sample();
console.log(pattern.parts)
expect(pattern.parts.front.paths.line_1.ops[1].to.x).to.equal(100);
expect(pattern.parts.front.paths.line_2.ops[1].to.x).to.equal(200);
expect(pattern.parts.front.paths.line_3.ops[1].to.x).to.equal(300);
@ -282,6 +281,7 @@ it("Should sample models", () => {
expect(pattern.parts.front.paths[`line_-1`].ops[1].to.x).to.equal(50);
expect(pattern.parts.front.paths[`line_0`].ops[1].to.x).to.equal(100);
});
*/
it("Should register a hook via on", () => {
let pattern = new freesewing.Pattern();
@ -581,13 +581,14 @@ it("Should correctly merge settings for existing array", () => {
});
it("Should return all render props", () => {
const front = {
name: 'front',
draft: function(part) { return part }
}
const Test = new freesewing.Design({
name: "test",
parts: ['front'],
parts: [front],
});
Test.prototype.draftFront = function(part) {
return part;
};
const pattern = new Test()
pattern.draft();
const rp = pattern.getRenderProps()
@ -605,14 +606,22 @@ it("Should not pack a pattern with errors", () => {
expect(pattern.events.warning[0]).to.equal('One or more errors occured. Not packing pattern parts')
});
/*
it("Should generate an auto layout if there is no set layout", () => {
const Test = new freesewing.Design({ name: "test", parts: ['front'] })
Test.prototype.draftFront = function(part) {
const Test = new freesewing.Design({
name: "test",
parts: [
{
name: 'front',
draft: function(part) {
const {Path, paths, Point} = part.shorthand()
paths.seam = new Path().move(new Point(0,0))
.line(new Point(5,5))
return part
}
}
]
})
const pattern = new Test()
pattern.parts.front = new pattern.Part('front')
pattern.draftFront(pattern.parts.front);
@ -621,6 +630,7 @@ it("Should generate an auto layout if there is no set layout", () => {
expect(pattern.autoLayout.parts.front.move.y).to.equal(2)
expect(pattern.autoLayout.parts.front.move.x).to.equal(2)
})
*/
it("Should handle custom layouts", () => {
const Test = new freesewing.Design({ name: "test", parts: ['front'] })
@ -683,13 +693,10 @@ it("Should handle a simple snapped option", () => {
it("Should handle a list snapped option", () => {
const Test = new freesewing.Design({
name: "test",
parts: ['front'],
measurements: [ 'head' ],
options: {
len: { pct: 50, min: 22, max: 78, snap: [10,14,19,28], ...freesewing.pctBasedOn('head') }
}
})
Test.prototype.draftFront = function(part) {
parts: [
{
name: 'front',
draft: function(part) {
const { Point, points, Path, paths, absoluteOptions } = part.shorthand()
points.from = new Point(0, 0);
points.to = new Point( absoluteOptions.len, 0)
@ -698,7 +705,14 @@ it("Should handle a list snapped option", () => {
.line(points.to)
return part
};
}
}
],
measurements: [ 'head' ],
options: {
len: { pct: 50, min: 22, max: 78, snap: [10,14,19,28], ...freesewing.pctBasedOn('head') }
}
})
let pattern = new Test({
sample: {
type: 'option',
@ -727,13 +741,15 @@ it("Should handle a list snapped option", () => {
it("Should retrieve the cutList", () => {
const Test = new freesewing.Design({
name: "test",
parts: ['front'],
})
Test.prototype.draftFront = function(part) {
part.addCut(4, 'lining', true)
parts: [{
name: 'front',
draft: function(part) {
const { addCut } = part.shorthand()
addCut(4, 'lining', true)
return part
};
}
}],
})
const pattern = new Test()
expect(JSON.stringify(pattern.getCutList())).to.equal(JSON.stringify({}))
pattern.draft()
@ -744,7 +760,7 @@ it("Should retrieve the cutList", () => {
// 2022 style part inheritance
// I am aware this does too much for one unit test, but this is to simplify TDD
// we can split it up later
it("Design constructor should resolve nested injections (2022)", () => {
it("Design constructor should resolve nested injections", () => {
const partA = {
name: "partA",
options: { optionA: { bool: true } },
@ -1136,4 +1152,5 @@ it("Pattern should merge optiongroups", () => {
expect(og.subnested.subnested1.indexOf('subnested1b3') === -1).to.equal(false)
// FIXME: Some work to be done still with deep-nesting of groups with the same name
})
})

View file

@ -5,6 +5,7 @@ const expect = chai.expect
const Point = freesewing.Point
const round = freesewing.utils.round
describe('Point', () => {
it("Should return point object", () => {
let result = new Point(2, 4);
expect(result).to.be.a("object");
@ -497,4 +498,5 @@ it("Should set the data-circle-class property in a chainable way", () => {
expect(p1.attributes.get('data-circle')).to.equal('20');
expect(p1.attributes.get('data-circle-class')).to.equal('fabric');
});
});

View file

@ -1,81 +0,0 @@
import chai from "chai"
import freesewing from "../dist/index.js"
const expect = chai.expect
const measurements = { head: 400 }
const toAbs = (val, { measurements }) => measurements.head * val
it("Should snap a percentage options to equal steps", () => {
const design = new freesewing.Design({
options: {
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs }
}
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
expect(patternA.settings.absoluteOptions.test).to.equal(60)
expect(patternB.settings.absoluteOptions.test).to.equal(108)
});
it("Should snap a percentage options to the Fibonacci sequence", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: [ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ],
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
const patternC = new design({ options: { test: 0.97 }, measurements })
expect(patternA.settings.absoluteOptions.test).to.equal(55)
expect(patternB.settings.absoluteOptions.test).to.equal(89)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
});
it("Should snap a percentage options to imperial snaps", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: {
metric: [ 25, 50, 75, 100 ],
imperial: [ 25.4, 50.8, 76.2, 101.6 ],
}
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements, units:'metric' })
const patternB = new design({ options: { test: 0.27 }, measurements, units:'metric' })
const patternC = new design({ options: { test: 0.97 }, measurements, units:'metric' })
const patternD = new design({ options: { test: 0.01 }, measurements, units:'metric' })
expect(patternA.settings.absoluteOptions.test).to.equal(50)
expect(patternB.settings.absoluteOptions.test).to.equal(100)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
expect(patternD.settings.absoluteOptions.test).to.equal(4)
});
it("Should snap a percentage options to metrics snaps", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: {
metric: [ 25, 50, 75, 100 ],
imperial: [ 25.4, 50.8, 76.2, 101.6 ],
}
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements, units:'imperial' })
const patternB = new design({ options: { test: 0.27 }, measurements, units:'imperial' })
const patternC = new design({ options: { test: 0.97 }, measurements, units:'imperial' })
const patternD = new design({ options: { test: 0.01 }, measurements, units:'imperial' })
expect(patternA.settings.absoluteOptions.test).to.equal(50.8)
expect(patternB.settings.absoluteOptions.test).to.equal(101.6)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
expect(patternD.settings.absoluteOptions.test).to.equal(4)
});

View file

@ -0,0 +1,83 @@
import chai from "chai"
import freesewing from "../dist/index.js"
const expect = chai.expect
const measurements = { head: 400 }
const toAbs = (val, { measurements }) => measurements.head * val
describe('Snapped options', () => {
it("Should snap a percentage options to equal steps", () => {
const design = new freesewing.Design({
options: {
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs }
}
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
expect(patternA.settings.absoluteOptions.test).to.equal(60)
expect(patternB.settings.absoluteOptions.test).to.equal(108)
});
it("Should snap a percentage options to the Fibonacci sequence", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: [ 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ],
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
const patternC = new design({ options: { test: 0.97 }, measurements })
expect(patternA.settings.absoluteOptions.test).to.equal(55)
expect(patternB.settings.absoluteOptions.test).to.equal(89)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
});
it("Should snap a percentage options to imperial snaps", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: {
metric: [ 25, 50, 75, 100 ],
imperial: [ 25.4, 50.8, 76.2, 101.6 ],
}
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements, units:'metric' })
const patternB = new design({ options: { test: 0.27 }, measurements, units:'metric' })
const patternC = new design({ options: { test: 0.97 }, measurements, units:'metric' })
const patternD = new design({ options: { test: 0.01 }, measurements, units:'metric' })
expect(patternA.settings.absoluteOptions.test).to.equal(50)
expect(patternB.settings.absoluteOptions.test).to.equal(100)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
expect(patternD.settings.absoluteOptions.test).to.equal(4)
});
it("Should snap a percentage options to metrics snaps", () => {
const design = new freesewing.Design({
options: {
test: {
pct: 30, min: 0, max: 100, toAbs,
snap: {
metric: [ 25, 50, 75, 100 ],
imperial: [ 25.4, 50.8, 76.2, 101.6 ],
}
}
}
})
const patternA = new design({ options: { test: 0.13 }, measurements, units:'imperial' })
const patternB = new design({ options: { test: 0.27 }, measurements, units:'imperial' })
const patternC = new design({ options: { test: 0.97 }, measurements, units:'imperial' })
const patternD = new design({ options: { test: 0.01 }, measurements, units:'imperial' })
expect(patternA.settings.absoluteOptions.test).to.equal(50.8)
expect(patternB.settings.absoluteOptions.test).to.equal(101.6)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
expect(patternD.settings.absoluteOptions.test).to.equal(4)
});
});

View file

@ -3,6 +3,7 @@ import freesewing from "./dist/index.mjs"
const expect = chai.expect
describe('Snippet', () => {
it("Should create a snippet", () => {
let snip1 = new freesewing.Snippet("test", new freesewing.Point(12, 34));
expect(snip1.def).to.equal("test");
@ -28,4 +29,5 @@ it("Should set an attribute", () => {
s.attr("class", "less", true);
expect(s.attributes.get("class")).to.equal("less");
});
});

View file

@ -3,9 +3,10 @@ import freesewing from "./dist/index.mjs"
const expect = chai.expect
let pattern = new freesewing.Pattern();
let store = pattern.store;
const pattern = new freesewing.Pattern();
const store = pattern.store;
describe('Store', () => {
it("Store should be a Map", () => {
expect(store.data.toString()).to.equal("[object Map]");
});
@ -26,4 +27,5 @@ it("Should raise a warning when retrieving a invalid key", () => {
expect(pattern.events.warning.length).to.equal(1)
expect(pattern.events.warning[0]).to.equal('Tried to access `nope` in the `store` but it is not set')
});
});

View file

@ -8,6 +8,7 @@ chai.use(chaiString)
const expect = chai.expect
const { version } = pkg
describe('Svg', () => {
it("Svg constructor should initialize object", () => {
let pattern = new freesewing.Pattern();
pattern.render();
@ -276,5 +277,6 @@ it("Should tab in and out", () => {
svg.tabs = 2
expect(svg.tab()).to.equal(' ')
});
});

View file

@ -5,6 +5,7 @@ const { expect } = chai
const utils = freesewing.utils
const round = utils.round
describe('Utils', () => {
it("Should return the correct macro name", () => {
expect(utils.macroName("test")).to.equal("_macro_test");
});
@ -562,5 +563,6 @@ it("Should generate a part transform", () => {
const transform = utils.generatePartTransform(30,60,90,true,true,part)
expect(transform.transform).to.equal(`translate(${30 + part.topLeft.x + part.bottomRight.x} ${60 + part.topLeft.y + part.bottomRight.y}) scale(-1 -1) rotate(90 ${part.topLeft.x + part.width/2} ${part.topLeft.y + part.height/2})`);
});
});