1
0
Fork 0

wip(core): Work on part unit tests

This commit is contained in:
joostdecock 2022-09-10 16:00:43 +02:00
parent 904e0044c5
commit cf0c70f4c6
4 changed files with 165 additions and 320 deletions

View file

@ -1,38 +1,26 @@
import chai from 'chai'
import { Pattern, Path } from '../src/index.mjs'
import { Design, Pattern, Path } from '../src/index.mjs'
const expect = chai.expect
describe('Part', () => {
it('Svg constructor should initialize object', () => {
let pattern = new Pattern()
let part = new pattern.Part()
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 Pattern()
let part = new pattern.Part()
const pattern = new Pattern()
const part = pattern.__createPartWithContext()
expect(typeof part.macroClosure()).to.equal('function')
})
it('Should not run an unknown macro', () => {
let pattern = new Pattern()
let part = new pattern.Part()
let macro = part.macroClosure()
const pattern = new Pattern()
const part = pattern.__createPartWithContext()
const macro = part.macroClosure()
expect(macro('unknown')).to.equal(undefined)
})
it('Should register and run a macro', () => {
let pattern = new Pattern()
let plugin = {
const pattern = new Pattern()
const plugin = {
name: 'test',
version: '0.1-test',
macros: {
@ -43,37 +31,38 @@ describe('Part', () => {
},
}
pattern.use(plugin)
let part = new pattern.Part()
let macro = part.macroClosure()
const part = pattern.__createPartWithContext()
const 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 return a free ID', () => {
let pattern = new Pattern()
let part = new pattern.Part()
let free = part.getId()
const pattern = new Pattern()
const part = pattern.__createPartWithContext()
const free = part.getId()
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
})
it('Should return a function from unitsClosure', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const pattern = new Pattern()
const part = pattern.__createPartWithContext()
expect(typeof part.unitsClosure()).to.equal('function')
})
it('Should convert units', () => {
let pattern = new Pattern()
let part = new pattern.Part()
let units = part.unitsClosure()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const 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 Pattern()
let part = new pattern.Part()
const pattern = new Pattern()
const part = pattern.__createPartWithContext()
part.attr('foo', 'bar')
expect(part.attributes.get('foo')).to.equal('bar')
part.attr('foo', 'baz')
@ -82,83 +71,50 @@ describe('Part', () => {
expect(part.attributes.get('foo')).to.equal('schmoo')
})
it('Should inject a part', () => {
let pattern = new Pattern()
let part = new pattern.Part()
part.points.a = new part.Point(12, 23)
part.points.b = new part.Point(10, 10)
part.points.c = new part.Point(20, 20)
part.paths.bar = new Path()
.move(part.points.a)
.line(part.points.b)
.curve(part.points.c, part.points.b, part.points.a)
const { Snippet, snippets } = part.shorthand()
snippets.d = new Snippet('notch', part.points.a)
let test = new pattern.Part()
test.inject(part)
expect(test.points.a.x).to.equal(12)
expect(test.points.a.y).to.equal(23)
expect(test.paths.bar.ops.length).to.equal(3)
for (let i = 0; i < 3; i++) {
expect(test.paths.bar.ops[i].type).to.equal(part.paths.bar.ops[i].type)
expect(test.paths.bar.ops[i].to.x).to.equal(part.paths.bar.ops[i].to.x)
expect(test.paths.bar.ops[i].to.y).to.equal(part.paths.bar.ops[i].to.y)
}
expect(test.snippets.d.anchor.x).to.equal(part.points.a.x)
expect(test.snippets.d.anchor.y).to.equal(part.points.a.y)
})
it('Should return shorthand', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
pattern.settings.paperless = true
let part = new pattern.Part()
let short = part.shorthand()
expect(short.complete).to.equal(true)
expect(short.paperless).to.equal(true)
})
it('Should raise a warning when setting a non-Point value in points', () => {
const pattern = new Pattern()
pattern.settings.mode = 'draft'
const part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
pattern.init()
const { points } = part.shorthand()
points.a = 'banana'
expect(pattern.events.warning.length).to.equal(4)
expect(pattern.events.warning[0]).to.equal(
expect(pattern.store.logs.warning.length).to.equal(4)
expect(pattern.store.logs.warning[0]).to.equal(
'`points.a` was set with a value that is not a `Point` object'
)
expect(pattern.events.warning[1]).to.equal(
expect(pattern.store.logs.warning[1]).to.equal(
'`points.a` was set with a `x` parameter that is not a `number`'
)
expect(pattern.events.warning[2]).to.equal(
expect(pattern.store.logs.warning[2]).to.equal(
'`points.a` was set with a `y` parameter that is not a `number`'
)
})
it('Should raise a warning when setting a non-Snippet value in snippets', () => {
const pattern = new Pattern()
pattern.settings.mode = 'draft'
const part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
pattern.init()
const { snippets } = part.shorthand()
snippets.a = 'banana'
expect(pattern.events.warning.length).to.equal(4)
expect(pattern.events.warning[0]).to.equal(
expect(pattern.store.logs.warning.length).to.equal(4)
expect(pattern.store.logs.warning[0]).to.equal(
'`snippets.a` was set with a value that is not a `Snippet` object'
)
expect(pattern.events.warning[1]).to.equal(
expect(pattern.store.logs.warning[1]).to.equal(
'`snippets.a` was set with a `def` parameter that is not a `string`'
)
expect(pattern.events.warning[2]).to.equal(
expect(pattern.store.logs.warning[2]).to.equal(
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
)
})
it('Should calculate the part boundary with default margin', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
let part = new pattern.Part()
let short = part.shorthand()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
pattern.init()
const 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)
@ -173,11 +129,11 @@ describe('Part', () => {
})
it('Should calculate the part boundary with custom margin', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
pattern.settings.margin = 5
let part = new pattern.Part()
let short = part.shorthand()
const design = new Design()
const pattern = new design({ margin: 5 })
const part = pattern.__createPartWithContext()
pattern.init()
const 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)
@ -192,12 +148,11 @@ describe('Part', () => {
})
it('Should calculate the part boundary for paperless', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
pattern.settings.margin = 5
pattern.settings.paperless = true
let part = new pattern.Part()
let short = part.shorthand()
const design = new Design()
const pattern = new design({ paperless: true })
const part = pattern.__createPartWithContext()
pattern.init()
const 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)
@ -210,16 +165,23 @@ describe('Part', () => {
expect(boundary.width).to.equal(124)
expect(boundary.height).to.equal(400)
})
/*
it('Should stack a part', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
let part = new pattern.Part()
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()
const part = {
name: 'test',
draft: (part) => {
const { points, Point, paths, Path } = part.shorthand()
points.from = new Point(123, 456)
points.to = new Point(19, 76)
paths.test = new Path().move(points.from).line(points.to)
return aprt
}
}
const design = new Design({ parts: [ part ]})
const pattern = new design({ paperless: true })
pattern.draft()
pattern.parts.test.stack()
console.log(pattern.parts.test.attributes)
expect(part.attributes.get('transform')).to.equal('translate(-17, -74)')
})
@ -236,11 +198,12 @@ describe('Part', () => {
part.stack()
expect(part.attributes.get('transform')).to.equal(false)
})
*/
it('Should run hooks', () => {
let count = 0
const pattern = new Pattern()
const part = new pattern.Part()
const design = new Design()
const pattern = new design({ paperless: true })
const part = pattern.__createPartWithContext()
part.hooks.preDraft = [
{
method: function (p) {
@ -253,22 +216,23 @@ describe('Part', () => {
})
it('Should get the units closure to raise a debug when passing a non-number', () => {
const pattern = new Pattern()
pattern.settings.mode = 'draft'
pattern.settings.debug = true
const part = new pattern.Part()
const design = new Design()
const pattern = new design({ margin: 5 })
const part = pattern.__createPartWithContext()
pattern.init()
const short = part.shorthand()
short.units('a')
expect(pattern.events.debug.length).to.equal(1)
expect(pattern.events.debug[0]).to.equal(
expect(pattern.store.logs.warning.length).to.equal(1)
expect(pattern.store.logs.warning[0]).to.equal(
'Calling `units(value)` but `value` is not a number (`string`)'
)
})
it('Should generate the part transforms', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
let part = new pattern.Part()
const design = new Design()
const pattern = new design({ margin: 5 })
const part = pattern.__createPartWithContext()
pattern.init()
let short = part.shorthand()
part.points.from = new short.Point(2, 2)
part.points.to = new short.Point(19, 76)
@ -283,33 +247,36 @@ describe('Part', () => {
expect(part.attributes.list.transform.length).to.equal(1)
expect(part.attributes.list.transform[0]).to.equal('translate(10 20)')
})
describe('isEmpty', () => {
it('Should return true if the part has no paths or snippets', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
expect(part.isEmpty()).to.be.true
})
it('Should return true if the part has paths but they have no length', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Path, paths, Point } = part.shorthand()
paths.seam = new Path()
expect(part.isEmpty()).to.be.true
})
it("Should return true if the part has paths but they don't render", () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Path, paths, Point } = part.shorthand()
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3)).setRender(false)
expect(part.isEmpty()).to.be.true
})
it('Should return false if the part has a path with length', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Path, paths, Point } = part.shorthand()
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3))
@ -317,8 +284,9 @@ describe('Part', () => {
})
it('Should return false if the part has a snippet', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Point, snippets, Snippet } = part.shorthand()
snippets.test = new Snippet('test', new Point(0, 0))
@ -326,8 +294,9 @@ describe('Part', () => {
})
it('Should return false if the part has a point that has text', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Point, points } = part.shorthand()
points.test = new Point(0, 0)
points.test.attributes.set('data-text', 'text')
@ -335,8 +304,9 @@ describe('Part', () => {
})
it('Should return false if the part has a point that has a circle', () => {
let pattern = new Pattern()
let part = new pattern.Part()
const design = new Design()
const pattern = new design()
const part = pattern.__createPartWithContext()
const { Point, points } = part.shorthand()
points.test = new Point(0, 0)
points.test.attributes.set('data-circle', 10)