feat(core): Tests for multisets
This commit is contained in:
parent
0b18d81e14
commit
e163ed1782
7 changed files with 559 additions and 625 deletions
80
packages/core/tests/multi.test.mjs
Normal file
80
packages/core/tests/multi.test.mjs
Normal file
|
@ -0,0 +1,80 @@
|
|||
import chai from 'chai'
|
||||
import { Design } from '../src/index.mjs'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
if (!expect) console.log('shut up eslint REMOVE')
|
||||
|
||||
describe('Multisets', () => {
|
||||
describe('FIXME', () => {
|
||||
const partA = {
|
||||
name: 'test.partA',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { pct: 40, min: 20, max: 80 },
|
||||
},
|
||||
draft: ({ points, Point, paths, Path, part, store, measurements, options }) => {
|
||||
store.set('size', measurements.head * options.size)
|
||||
points.from = new Point(0, 0)
|
||||
points.to = new Point(0, store.get('size'))
|
||||
paths.line = new Path().move(points.from).line(points.to)
|
||||
return part
|
||||
},
|
||||
//stack: 'box',
|
||||
}
|
||||
const partB = {
|
||||
name: 'test.partB',
|
||||
measurements: ['head'],
|
||||
after: partA,
|
||||
draft: ({ points, Point, paths, Path, part, store }) => {
|
||||
points.from = new Point(0, store.get('size'))
|
||||
points.to = new Point(store.get('size'), store.get('size'))
|
||||
paths.line = new Path().move(points.from).line(points.to)
|
||||
return part
|
||||
},
|
||||
//stack: 'box',
|
||||
}
|
||||
const partC = {
|
||||
name: 'test.partC',
|
||||
after: partB,
|
||||
draft: ({ points, Point, paths, Path, part, store }) => {
|
||||
points.from = new Point(store.get('size'), store.get('size'))
|
||||
points.to = new Point(store.get('size'), 0)
|
||||
paths.line = new Path().move(points.from).line(points.to)
|
||||
return part
|
||||
},
|
||||
//stack: 'box',
|
||||
}
|
||||
const partD = {
|
||||
name: 'test.partD',
|
||||
after: partC,
|
||||
draft: ({ points, Point, paths, Path, part, store }) => {
|
||||
points.from = new Point(store.get('size'), 0)
|
||||
points.to = new Point(0, 0)
|
||||
paths.line = new Path().move(points.from).line(points.to)
|
||||
return part
|
||||
},
|
||||
// stack: 'box',
|
||||
}
|
||||
|
||||
const Pattern = new Design({
|
||||
data: {
|
||||
name: 'test',
|
||||
version: '1.2.3',
|
||||
},
|
||||
parts: [partD],
|
||||
})
|
||||
const pattern = new Pattern([
|
||||
{
|
||||
measurements: { head: 400 },
|
||||
},
|
||||
{
|
||||
measurements: { head: 400 },
|
||||
},
|
||||
])
|
||||
pattern.draft()
|
||||
console.log(pattern)
|
||||
console.log(pattern.render())
|
||||
//pattern.render()
|
||||
})
|
||||
})
|
|
@ -1,5 +1,5 @@
|
|||
import chai from 'chai'
|
||||
import { Design, Pattern } from '../src/index.mjs'
|
||||
import { Design, Part } from '../src/index.mjs'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
|
@ -21,20 +21,24 @@ describe('Part', () => {
|
|||
})
|
||||
|
||||
it('Should return a function from macroClosure', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
expect(typeof part.macroClosure()).to.equal('function')
|
||||
})
|
||||
|
||||
it('Should not run an unknown macro', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
const macro = part.macroClosure()
|
||||
expect(macro('unknown')).to.equal(undefined)
|
||||
})
|
||||
|
||||
it('Should register and run a macro', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ part, macro }) => {
|
||||
macro('test', { x: 123, y: 456 })
|
||||
return part
|
||||
},
|
||||
}
|
||||
const plugin = {
|
||||
name: 'test',
|
||||
version: '0.1-test',
|
||||
|
@ -45,39 +49,34 @@ describe('Part', () => {
|
|||
},
|
||||
},
|
||||
}
|
||||
pattern.use(plugin)
|
||||
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)
|
||||
const design = new Design({ parts: [part], plugins: [plugin] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.parts[0].test.points.macro.x).to.equal(123)
|
||||
expect(pattern.parts[0].test.points.macro.y).to.equal(456)
|
||||
})
|
||||
|
||||
it('Should return a free ID', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
const free = part.getId()
|
||||
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
||||
})
|
||||
|
||||
it('Should return a function from unitsClosure', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
expect(typeof part.unitsClosure()).to.equal('function')
|
||||
})
|
||||
|
||||
it('Should convert units', () => {
|
||||
const design = new Design()
|
||||
const pattern = new design()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
part.context = { settings: { units: 'metric' } }
|
||||
const units = part.unitsClosure()
|
||||
expect(units(123.456)).to.equal('12.35cm')
|
||||
expect(part.units(123.456)).to.equal('12.35cm')
|
||||
expect(units(123.456)).to.equal('12.35cm')
|
||||
})
|
||||
|
||||
it('Should set part attributes', () => {
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext()
|
||||
const part = new Part()
|
||||
part.attr('foo', 'bar')
|
||||
expect(part.attributes.get('foo')).to.equal('bar')
|
||||
part.attr('foo', 'baz')
|
||||
|
@ -87,60 +86,72 @@ describe('Part', () => {
|
|||
})
|
||||
|
||||
it('Should raise a warning when setting a non-Point value in points', () => {
|
||||
const design = new Design()
|
||||
const pattern = new design()
|
||||
const part = pattern.__createPartWithContext()
|
||||
pattern.init()
|
||||
const { points } = part.shorthand()
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ points, part }) => {
|
||||
points.a = 'banana'
|
||||
expect(pattern.store.logs.warning.length).to.equal(4)
|
||||
expect(pattern.store.logs.warning[0]).to.equal(
|
||||
return part
|
||||
},
|
||||
}
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.stores[0].logs.warning.length).to.equal(4)
|
||||
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
||||
'`points.a` was set with a value that is not a `Point` object'
|
||||
)
|
||||
expect(pattern.store.logs.warning[1]).to.equal(
|
||||
expect(pattern.stores[0].logs.warning[1]).to.equal(
|
||||
'`points.a` was set with a `x` parameter that is not a `number`'
|
||||
)
|
||||
expect(pattern.store.logs.warning[2]).to.equal(
|
||||
expect(pattern.stores[0].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 design = new Design()
|
||||
const pattern = new design()
|
||||
const part = pattern.__createPartWithContext()
|
||||
pattern.init()
|
||||
const { snippets } = part.shorthand()
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ snippets, part }) => {
|
||||
snippets.a = 'banana'
|
||||
expect(pattern.store.logs.warning.length).to.equal(4)
|
||||
expect(pattern.store.logs.warning[0]).to.equal(
|
||||
return part
|
||||
},
|
||||
}
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.stores[0].logs.warning.length).to.equal(4)
|
||||
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
||||
'`snippets.a` was set with a value that is not a `Snippet` object'
|
||||
)
|
||||
expect(pattern.store.logs.warning[1]).to.equal(
|
||||
expect(pattern.stores[0].logs.warning[1]).to.equal(
|
||||
'`snippets.a` was set with a `def` parameter that is not a `string`'
|
||||
)
|
||||
expect(pattern.store.logs.warning[2]).to.equal(
|
||||
expect(pattern.stores[0].logs.warning[2]).to.equal(
|
||||
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
|
||||
)
|
||||
})
|
||||
|
||||
it('Should calculate the part boundary', () => {
|
||||
const design = new Design()
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ points, Point, paths, Path, part }) => {
|
||||
points.from = new Point(123, 456)
|
||||
points.to = new Point(19, 76)
|
||||
paths.test = new Path().move(points.from).line(points.to)
|
||||
return part
|
||||
},
|
||||
}
|
||||
const design = new Design({ parts: [part] })
|
||||
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)
|
||||
let boundary = part.boundary()
|
||||
expect(boundary.topLeft.x).to.equal(19)
|
||||
expect(boundary.topLeft.y).to.equal(76)
|
||||
expect(boundary.bottomRight.x).to.equal(123)
|
||||
expect(boundary.bottomRight.y).to.equal(456)
|
||||
boundary = part.boundary()
|
||||
expect(boundary.width).to.equal(104)
|
||||
expect(boundary.height).to.equal(380)
|
||||
pattern.draft()
|
||||
const boundary = pattern.parts[0].test.boundary()
|
||||
const { topLeft, bottomRight, width, height } = boundary
|
||||
expect(topLeft.x).to.equal(19)
|
||||
expect(topLeft.y).to.equal(76)
|
||||
expect(bottomRight.x).to.equal(123)
|
||||
expect(bottomRight.y).to.equal(456)
|
||||
expect(width).to.equal(104)
|
||||
expect(height).to.equal(380)
|
||||
})
|
||||
|
||||
/*
|
||||
|
@ -176,7 +187,6 @@ describe('Part', () => {
|
|||
part.home()
|
||||
expect(part.attributes.get('transform')).to.equal(false)
|
||||
})
|
||||
*/
|
||||
it('Should run hooks', () => {
|
||||
let count = 0
|
||||
const design = new Design()
|
||||
|
@ -192,20 +202,25 @@ describe('Part', () => {
|
|||
part.runHooks('preDraft')
|
||||
expect(count).to.equal(1)
|
||||
})
|
||||
*/
|
||||
|
||||
it('Should get the units closure to raise a debug when passing a non-number', () => {
|
||||
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.store.logs.warning.length).to.equal(1)
|
||||
expect(pattern.store.logs.warning[0]).to.equal(
|
||||
it('Units closure should log a warning when passing a non-number', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ units, part }) => {
|
||||
units('a')
|
||||
return part
|
||||
},
|
||||
}
|
||||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.stores[0].logs.warning.length).to.equal(1)
|
||||
expect(pattern.stores[0].logs.warning[0]).to.equal(
|
||||
'Calling `units(value)` but `value` is not a number (`string`)'
|
||||
)
|
||||
})
|
||||
|
||||
/*
|
||||
describe('isEmpty', () => {
|
||||
it('Should return true if the part has no paths or snippets', () => {
|
||||
const design = new Design()
|
||||
|
@ -272,4 +287,5 @@ describe('Part', () => {
|
|||
expect(part.isEmpty()).to.be.false
|
||||
})
|
||||
})
|
||||
*/
|
||||
})
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import chai from 'chai'
|
||||
import { round, Pattern, Path, Point, Design } from '../src/index.mjs'
|
||||
import { round, Path, Point, Design } from '../src/index.mjs'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
|
@ -16,8 +16,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(pattern.parts.test.paths.offset.bottomRight.x).to.equal(-10)
|
||||
expect(pattern.parts.test.paths.offset.bottomRight.y).to.equal(40)
|
||||
expect(pattern.parts[0].test.paths.offset.bottomRight.x).to.equal(-10)
|
||||
expect(pattern.parts[0].test.paths.offset.bottomRight.y).to.equal(40)
|
||||
})
|
||||
|
||||
it('Should offset a curve', () => {
|
||||
|
@ -34,8 +34,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.18)
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.y)).to.equal(38.26)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.x)).to.equal(72.18)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.y)).to.equal(38.26)
|
||||
})
|
||||
|
||||
it('Should offset a curve where cp1 = start', () => {
|
||||
|
@ -50,8 +50,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.63)
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.y)).to.equal(26.48)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.x)).to.equal(72.63)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.y)).to.equal(26.48)
|
||||
})
|
||||
|
||||
it('Should offset a curve where cp2 = end', () => {
|
||||
|
@ -66,8 +66,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(119.26)
|
||||
expect(round(pattern.parts.test.paths.offset.bottomRight.y)).to.equal(43.27)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.x)).to.equal(119.26)
|
||||
expect(round(pattern.parts[0].test.paths.offset.bottomRight.y)).to.equal(43.27)
|
||||
})
|
||||
|
||||
/*
|
||||
|
@ -101,7 +101,7 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(pattern.parts.test.paths.line.length()).to.equal(40)
|
||||
expect(pattern.parts[0].test.paths.line.length()).to.equal(40)
|
||||
})
|
||||
|
||||
it('Should return the length of a curve', () => {
|
||||
|
@ -118,7 +118,7 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(round(pattern.parts.test.paths.curve.length())).to.equal(145.11)
|
||||
expect(round(pattern.parts[0].test.paths.curve.length())).to.equal(145.11)
|
||||
})
|
||||
|
||||
it('Should return the path start point', () => {
|
||||
|
@ -135,8 +135,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(pattern.parts.test.paths.curve.start().x).to.equal(123)
|
||||
expect(pattern.parts.test.paths.curve.start().y).to.equal(456)
|
||||
expect(pattern.parts[0].test.paths.curve.start().x).to.equal(123)
|
||||
expect(pattern.parts[0].test.paths.curve.start().y).to.equal(456)
|
||||
})
|
||||
|
||||
it('Should return the path end point', () => {
|
||||
|
@ -153,35 +153,26 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft().render()
|
||||
expect(pattern.parts.test.paths.curve.end().x).to.equal(123)
|
||||
expect(pattern.parts.test.paths.curve.end().y).to.equal(456)
|
||||
expect(pattern.parts[0].test.paths.curve.end().x).to.equal(123)
|
||||
expect(pattern.parts[0].test.paths.curve.end().y).to.equal(456)
|
||||
})
|
||||
|
||||
it('Should calculate that path boundary', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
a.paths.curve.boundary()
|
||||
expect(a.paths.curve.topLeft.x).to.equal(71.6413460920667)
|
||||
expect(a.paths.curve.topLeft.y).to.equal(4)
|
||||
a.paths.curve.boundary()
|
||||
expect(a.paths.curve.bottomRight.x).to.equal(230)
|
||||
expect(a.paths.curve.bottomRight.y).to.equal(456)
|
||||
const curve = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
curve.boundary()
|
||||
expect(curve.topLeft.x).to.equal(71.6413460920667)
|
||||
expect(curve.topLeft.y).to.equal(4)
|
||||
expect(curve.bottomRight.x).to.equal(230)
|
||||
expect(curve.bottomRight.y).to.equal(456)
|
||||
})
|
||||
|
||||
it('Should clone a path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
let b = a.paths.curve.clone()
|
||||
const curve = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
let b = curve.clone()
|
||||
b.boundary()
|
||||
expect(b.topLeft.x).to.equal(71.6413460920667)
|
||||
expect(b.topLeft.y).to.equal(4)
|
||||
|
@ -191,129 +182,79 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should join paths', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path().move(new a.Point(0, 0)).line(new a.Point(0, 40))
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
a.paths.joint = a.paths.curve.join(a.paths.line)
|
||||
expect(a.paths.joint.ops.length).to.equal(4)
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
|
||||
const curve = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
const joint = curve.join(line)
|
||||
expect(joint.ops.length).to.equal(4)
|
||||
})
|
||||
|
||||
it('Should throw error when joining a closed paths', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path().move(new a.Point(0, 0)).line(new a.Point(0, 40))
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
|
||||
const curve = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
.close()
|
||||
expect(() => a.paths.curve.join(a.paths.line)).to.throw()
|
||||
expect(() => curve.join(line)).to.throw()
|
||||
})
|
||||
|
||||
it('Should shift along a line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path().move(new a.Point(0, 0)).line(new a.Point(0, 40))
|
||||
expect(a.paths.line.shiftAlong(20).y).to.equal(20)
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
|
||||
expect(line.shiftAlong(20).y).to.equal(20)
|
||||
})
|
||||
|
||||
it('Should not shift along a path/line if we end up on the end point', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path().move(new a.Point(0, 0)).line(new a.Point(10, 0))
|
||||
expect(a.paths.line.shiftAlong(10).x).to.equal(10)
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(10, 0))
|
||||
expect(line.shiftAlong(10).x).to.equal(10)
|
||||
})
|
||||
|
||||
it('Should shift along lines', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.line(new a.Point(0, 40))
|
||||
.line(new a.Point(100, 40))
|
||||
expect(a.paths.line.shiftAlong(50).x).to.equal(10)
|
||||
expect(a.paths.line.shiftAlong(50).y).to.equal(40)
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).line(new Point(100, 40))
|
||||
expect(line.shiftAlong(50).x).to.equal(10)
|
||||
expect(line.shiftAlong(50).y).to.equal(40)
|
||||
})
|
||||
|
||||
it('Should shift along curve + line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.test = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.line(new a.Point(0, 40))
|
||||
.curve(new a.Point(40, 40), new a.Point(40, 0), new a.Point(200, 0))
|
||||
.line(new a.Point(200, 400))
|
||||
expect(round(a.paths.test.shiftAlong(500).x)).to.equal(200)
|
||||
expect(round(a.paths.test.shiftAlong(500).y)).to.equal(253.74)
|
||||
const test = new Path()
|
||||
.move(new Point(0, 0))
|
||||
.line(new Point(0, 40))
|
||||
.curve(new Point(40, 40), new Point(40, 0), new Point(200, 0))
|
||||
.line(new Point(200, 400))
|
||||
expect(round(test.shiftAlong(500).x)).to.equal(200)
|
||||
expect(round(test.shiftAlong(500).y)).to.equal(253.74)
|
||||
})
|
||||
|
||||
it("Should throw error when shifting along path further than it's long", () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.test = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.line(new a.Point(0, 40))
|
||||
.line(new a.Point(200, 400))
|
||||
expect(() => a.paths.test.shiftAlong(500)).to.throw()
|
||||
it("Should throw an error when shifting along path further than it's long", () => {
|
||||
const test = new Path().move(new Point(0, 0)).line(new Point(0, 40)).line(new Point(200, 400))
|
||||
expect(() => test.shiftAlong(500)).to.throw()
|
||||
})
|
||||
|
||||
it('Should shift along with sufficient precision', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.test = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.curve(new a.Point(123, 123), new a.Point(-123, 456), new a.Point(456, -123))
|
||||
a.points.a = a.paths.test.shiftAlong(100)
|
||||
a.points.b = a.paths.test.reverse().shiftAlong(a.paths.test.length() - 100)
|
||||
expect(a.points.a.dist(a.points.b)).to.below(0.05)
|
||||
const test = new Path()
|
||||
.move(new Point(0, 0))
|
||||
.curve(new Point(123, 123), new Point(-123, 456), new Point(456, -123))
|
||||
const a = test.shiftAlong(100)
|
||||
const b = test.reverse().shiftAlong(test.length() - 100)
|
||||
expect(a.dist(b)).to.below(0.05)
|
||||
})
|
||||
|
||||
it('Should shift fraction with sufficient precision', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.test = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.curve(new a.Point(123, 123), new a.Point(-123, 456), new a.Point(456, -123))
|
||||
a.points.a = a.paths.test.shiftFractionAlong(0.5)
|
||||
a.points.b = a.paths.test.reverse().shiftFractionAlong(0.5)
|
||||
expect(a.points.a.dist(a.points.b)).to.below(0.05)
|
||||
const test = new Path()
|
||||
.move(new Point(0, 0))
|
||||
.curve(new Point(123, 123), new Point(-123, 456), new Point(456, -123))
|
||||
const a = test.shiftFractionAlong(0.5)
|
||||
const b = test.reverse().shiftFractionAlong(0.5)
|
||||
expect(a.dist(b)).to.below(0.05)
|
||||
})
|
||||
|
||||
it('Should shift a fraction along a line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.line = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.line(new a.Point(0, 40))
|
||||
.line(new a.Point(100, 40))
|
||||
expect(round(a.paths.line.shiftFractionAlong(0.5).x)).to.equal(30)
|
||||
expect(round(a.paths.line.shiftFractionAlong(0.5).y)).to.equal(40)
|
||||
const line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).line(new Point(100, 40))
|
||||
expect(round(line.shiftFractionAlong(0.5).x)).to.equal(30)
|
||||
expect(round(line.shiftFractionAlong(0.5).y)).to.equal(40)
|
||||
})
|
||||
|
||||
it('Should find the bounding box of a line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let Path = pattern.parts.a.Path
|
||||
let Point = pattern.parts.a.Point
|
||||
|
||||
let line = new Path().move(new Point(3, 2)).line(new Point(10, 40))
|
||||
let box = line.bbox()
|
||||
expect(box.topLeft.x).to.equal(3)
|
||||
|
@ -371,15 +312,12 @@ describe('Path', () => {
|
|||
expect(box.bottomRight.y).to.equal(12)
|
||||
})
|
||||
|
||||
it('Should find the bounding box of a line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
it('Should find the bounding box of a curve', () => {
|
||||
const curve = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
.close()
|
||||
let box = a.paths.curve.bbox()
|
||||
const box = curve.bbox()
|
||||
expect(round(box.topLeft.x)).to.equal(71.64)
|
||||
expect(box.topLeft.y).to.equal(4)
|
||||
expect(box.bottomRight.x).to.equal(230)
|
||||
|
@ -387,13 +325,10 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should reverse a path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
let test = new a.Path()
|
||||
.move(new a.Point(123, 456))
|
||||
.line(new a.Point(12, 23))
|
||||
.curve(new a.Point(0, 40), new a.Point(123, 34), new a.Point(230, 4))
|
||||
const test = new Path()
|
||||
.move(new Point(123, 456))
|
||||
.line(new Point(12, 23))
|
||||
.curve(new Point(0, 40), new Point(123, 34), new Point(230, 4))
|
||||
.close()
|
||||
let rev = test.reverse()
|
||||
let tb = test.bbox()
|
||||
|
@ -407,98 +342,69 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should find the edges of a path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(45, 60)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(-60, 90)
|
||||
a.points.E = new a.Point(90, 190)
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.E, a.points.D, a.points.A)
|
||||
const test = new Path()
|
||||
.move(new Point(45, 60))
|
||||
.line(new Point(10, 30))
|
||||
.curve(new Point(40, 20), new Point(50, -30), new Point(90, 30))
|
||||
.curve(new Point(90, 190), new Point(-60, 90), new Point(45, 60))
|
||||
.close()
|
||||
expect(round(a.paths.test.edge('topLeft').x)).to.equal(7.7)
|
||||
expect(round(a.paths.test.edge('topLeft').y)).to.equal(0.97)
|
||||
expect(round(a.paths.test.edge('bottomLeft').x)).to.equal(7.7)
|
||||
expect(round(a.paths.test.edge('bottomLeft').y)).to.equal(118.46)
|
||||
expect(round(a.paths.test.edge('bottomRight').x)).to.equal(90)
|
||||
expect(round(a.paths.test.edge('bottomRight').y)).to.equal(118.46)
|
||||
expect(round(a.paths.test.edge('topRight').x)).to.equal(90)
|
||||
expect(round(a.paths.test.edge('topRight').y)).to.equal(0.97)
|
||||
expect(round(a.paths.test.edge('left').x)).to.equal(7.7)
|
||||
expect(round(a.paths.test.edge('left').y)).to.equal(91.8)
|
||||
expect(round(a.paths.test.edge('bottom').x)).to.equal(40.63)
|
||||
expect(round(a.paths.test.edge('bottom').y)).to.equal(118.46)
|
||||
expect(round(a.paths.test.edge('right').x)).to.equal(89.76)
|
||||
expect(round(a.paths.test.edge('right').y)).to.equal(29.64)
|
||||
expect(round(a.paths.test.edge('top').x)).to.equal(55.98)
|
||||
expect(round(a.paths.test.edge('top').y)).to.equal(0.97)
|
||||
expect(round(test.edge('topLeft').x)).to.equal(7.7)
|
||||
expect(round(test.edge('topLeft').y)).to.equal(0.97)
|
||||
expect(round(test.edge('bottomLeft').x)).to.equal(7.7)
|
||||
expect(round(test.edge('bottomLeft').y)).to.equal(118.46)
|
||||
expect(round(test.edge('bottomRight').x)).to.equal(90)
|
||||
expect(round(test.edge('bottomRight').y)).to.equal(118.46)
|
||||
expect(round(test.edge('topRight').x)).to.equal(90)
|
||||
expect(round(test.edge('topRight').y)).to.equal(0.97)
|
||||
expect(round(test.edge('left').x)).to.equal(7.7)
|
||||
expect(round(test.edge('left').y)).to.equal(91.8)
|
||||
expect(round(test.edge('bottom').x)).to.equal(40.63)
|
||||
expect(round(test.edge('bottom').y)).to.equal(118.46)
|
||||
expect(round(test.edge('right').x)).to.equal(89.76)
|
||||
expect(round(test.edge('right').y)).to.equal(29.64)
|
||||
expect(round(test.edge('top').x)).to.equal(55.98)
|
||||
expect(round(test.edge('top').y)).to.equal(0.97)
|
||||
})
|
||||
|
||||
it('Should find the edges of a path for corner cases', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(-45, -60)
|
||||
a.points.B = new a.Point(45, 60)
|
||||
a.points.C = new a.Point(-90, -160)
|
||||
a.paths.test = new a.Path().move(a.points.A).line(a.points.B)
|
||||
expect(round(a.paths.test.edge('top').x)).to.equal(-45)
|
||||
expect(round(a.paths.test.edge('top').y)).to.equal(-60)
|
||||
expect(round(a.paths.test.edge('left').x)).to.equal(-45)
|
||||
expect(round(a.paths.test.edge('left').y)).to.equal(-60)
|
||||
expect(round(a.paths.test.edge('bottom').x)).to.equal(45)
|
||||
expect(round(a.paths.test.edge('bottom').y)).to.equal(60)
|
||||
expect(round(a.paths.test.edge('right').x)).to.equal(45)
|
||||
expect(round(a.paths.test.edge('right').y)).to.equal(60)
|
||||
a.paths.test = new a.Path().move(a.points.B).line(a.points.A)
|
||||
expect(round(a.paths.test.edge('top').x)).to.equal(-45)
|
||||
expect(round(a.paths.test.edge('top').y)).to.equal(-60)
|
||||
expect(round(a.paths.test.edge('left').x)).to.equal(-45)
|
||||
expect(round(a.paths.test.edge('left').y)).to.equal(-60)
|
||||
expect(round(a.paths.test.edge('bottom').x)).to.equal(45)
|
||||
expect(round(a.paths.test.edge('bottom').y)).to.equal(60)
|
||||
expect(round(a.paths.test.edge('right').x)).to.equal(45)
|
||||
expect(round(a.paths.test.edge('right').y)).to.equal(60)
|
||||
let test = new Path().move(new Point(-45, -60)).line(new Point(45, 60))
|
||||
expect(round(test.edge('top').x)).to.equal(-45)
|
||||
expect(round(test.edge('top').y)).to.equal(-60)
|
||||
expect(round(test.edge('left').x)).to.equal(-45)
|
||||
expect(round(test.edge('left').y)).to.equal(-60)
|
||||
expect(round(test.edge('bottom').x)).to.equal(45)
|
||||
expect(round(test.edge('bottom').y)).to.equal(60)
|
||||
expect(round(test.edge('right').x)).to.equal(45)
|
||||
expect(round(test.edge('right').y)).to.equal(60)
|
||||
test = new Path().move(new Point(45, 60)).line(new Point(-45, -60))
|
||||
expect(round(test.edge('top').x)).to.equal(-45)
|
||||
expect(round(test.edge('top').y)).to.equal(-60)
|
||||
expect(round(test.edge('left').x)).to.equal(-45)
|
||||
expect(round(test.edge('left').y)).to.equal(-60)
|
||||
expect(round(test.edge('bottom').x)).to.equal(45)
|
||||
expect(round(test.edge('bottom').y)).to.equal(60)
|
||||
expect(round(test.edge('right').x)).to.equal(45)
|
||||
expect(round(test.edge('right').y)).to.equal(60)
|
||||
})
|
||||
|
||||
it('Should find the edge of a path for this edge-case', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(-109.7, 77, 12)
|
||||
a.points.B = new a.Point(-27.33, 99.19)
|
||||
a.points.C = new a.Point(-39.45, 137.4)
|
||||
a.points.D = new a.Point(-61.52, 219.77)
|
||||
a.paths.test = new a.Path().move(a.points.A).curve(a.points.B, a.points.C, a.points.D)
|
||||
expect(round(a.paths.test.edge('right').x)).to.equal(-45.22)
|
||||
expect(round(a.paths.test.edge('right').y)).to.equal(139.4)
|
||||
const test = new Path()
|
||||
.move(new Point(-109.7, 77))
|
||||
.curve(new Point(-27.33, 99.19), new Point(-39.45, 137.4), new Point(-61.52, 219.77))
|
||||
expect(round(test.edge('right').x)).to.equal(-45.22)
|
||||
expect(round(test.edge('right').y)).to.equal(139.4)
|
||||
})
|
||||
|
||||
it('Should find where a path intersects with an X value', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(95, 50)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(50, 130)
|
||||
a.points.DCp1 = new a.Point(150, 30)
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.DCp1, a.points.DCp1, a.points.D)
|
||||
.close()
|
||||
let intersections = a.paths.test.intersectsX(60)
|
||||
const A = new Point(95, 50)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(50, 130)
|
||||
const DCp1 = new Point(150, 30)
|
||||
const test = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(DCp1, DCp1, D).close()
|
||||
const intersections = test.intersectsX(60)
|
||||
expect(intersections.length).to.equal(4)
|
||||
expect(round(intersections[0].x)).to.equal(60)
|
||||
expect(round(intersections[0].y)).to.equal(41.76)
|
||||
|
@ -511,23 +417,15 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should find where a path intersects with an Y value', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(95, 50)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(50, 130)
|
||||
a.points.DCp1 = new a.Point(150, 30)
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.DCp1, a.points.DCp1, a.points.D)
|
||||
.close()
|
||||
let intersections = a.paths.test.intersectsY(60)
|
||||
const A = new Point(95, 50)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(50, 130)
|
||||
const DCp1 = new Point(150, 30)
|
||||
const test = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(DCp1, DCp1, D).close()
|
||||
let intersections = test.intersectsY(60)
|
||||
expect(intersections.length).to.equal(2)
|
||||
expect(round(intersections[0].x)).to.equal(117.83)
|
||||
expect(round(intersections[0].y)).to.equal(60)
|
||||
|
@ -536,45 +434,31 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should throw an error when not passing a value to path.intersectsX', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.test = new a.Path()
|
||||
expect(() => a.paths.test.intersectsX()).to.throw()
|
||||
expect(() => a.paths.test.intersectsY()).to.throw()
|
||||
const test = new Path()
|
||||
expect(() => test.intersectsX()).to.throw()
|
||||
expect(() => test.intersectsY()).to.throw()
|
||||
})
|
||||
|
||||
it('Should find the intersections between two paths', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(45, 60)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(50, 130)
|
||||
a.points.DCp1 = new a.Point(150, 30)
|
||||
const A = new Point(45, 60)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(50, 130)
|
||||
const DCp1 = new Point(150, 30)
|
||||
|
||||
a.points._A = new a.Point(55, 40)
|
||||
a.points._B = new a.Point(0, 55)
|
||||
a.points._BCp2 = new a.Point(40, -20)
|
||||
a.points._C = new a.Point(90, 40)
|
||||
a.points._CCp1 = new a.Point(50, -30)
|
||||
a.points._D = new a.Point(40, 120)
|
||||
a.points._DCp1 = new a.Point(180, 40)
|
||||
const _A = new Point(55, 40)
|
||||
const _B = new Point(0, 55)
|
||||
const _BCp2 = new Point(40, -20)
|
||||
const _C = new Point(90, 40)
|
||||
const _CCp1 = new Point(50, -30)
|
||||
const _D = new Point(40, 120)
|
||||
const _DCp1 = new Point(180, 40)
|
||||
|
||||
a.paths.example1 = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.DCp1, a.points.DCp1, a.points.D)
|
||||
a.paths.example2 = new a.Path()
|
||||
.move(a.points._A)
|
||||
.line(a.points._B)
|
||||
.curve(a.points._BCp2, a.points._CCp1, a.points._C)
|
||||
.curve(a.points._DCp1, a.points._DCp1, a.points._D)
|
||||
let intersections = a.paths.example1.intersects(a.paths.example2)
|
||||
const example1 = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(DCp1, DCp1, D)
|
||||
const example2 = new Path().move(_A).line(_B).curve(_BCp2, _CCp1, _C).curve(_DCp1, _DCp1, _D)
|
||||
let intersections = example1.intersects(example2)
|
||||
expect(intersections.length).to.equal(6)
|
||||
expect(round(intersections[0].x)).to.equal(29.71)
|
||||
expect(round(intersections[0].y)).to.equal(46.9)
|
||||
|
@ -591,31 +475,20 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should throw an error when running path.intersect on an identical path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.paths.test = new a.Path()
|
||||
expect(() => a.paths.test.intersects(a.paths.test)).to.throw()
|
||||
const test = new Path()
|
||||
expect(() => test.intersects(test)).to.throw()
|
||||
})
|
||||
|
||||
it('Should divide a path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(45, 60)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(-60, 90)
|
||||
a.points.E = new a.Point(90, 190)
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.E, a.points.D, a.points.A)
|
||||
.close()
|
||||
let divided = a.paths.test.divide()
|
||||
const A = new Point(45, 60)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(-60, 90)
|
||||
const E = new Point(90, 190)
|
||||
const test = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(E, D, A).close()
|
||||
let divided = test.divide()
|
||||
expect(divided.length).to.equal(4)
|
||||
expect(divided[0].ops[0].type).to.equal('move')
|
||||
expect(divided[0].ops[0].to.x).to.equal(45)
|
||||
|
@ -652,25 +525,18 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should split a path on a curve', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(45, 60)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(50, 130)
|
||||
a.points.DCp1 = new a.Point(150, 30)
|
||||
const A = new Point(45, 60)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(50, 130)
|
||||
const DCp1 = new Point(150, 30)
|
||||
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.DCp1, a.points.DCp1, a.points.D)
|
||||
const test = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(DCp1, DCp1, D)
|
||||
|
||||
a.points.split = a.paths.test.shiftAlong(120)
|
||||
let halves = a.paths.test.split(a.points.split)
|
||||
const split = test.shiftAlong(120)
|
||||
let halves = test.split(split)
|
||||
let curve = halves[0].ops.pop()
|
||||
expect(curve.type).to.equal('curve')
|
||||
expect(round(curve.cp1.x)).to.equal(35.08)
|
||||
|
@ -682,25 +548,18 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should split a path on a line', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(45, 60)
|
||||
a.points.B = new a.Point(10, 30)
|
||||
a.points.BCp2 = new a.Point(40, 20)
|
||||
a.points.C = new a.Point(90, 30)
|
||||
a.points.CCp1 = new a.Point(50, -30)
|
||||
a.points.D = new a.Point(50, 130)
|
||||
a.points.DCp1 = new a.Point(150, 30)
|
||||
const A = new Point(45, 60)
|
||||
const B = new Point(10, 30)
|
||||
const BCp2 = new Point(40, 20)
|
||||
const C = new Point(90, 30)
|
||||
const CCp1 = new Point(50, -30)
|
||||
const D = new Point(50, 130)
|
||||
const DCp1 = new Point(150, 30)
|
||||
|
||||
a.paths.test = new a.Path()
|
||||
.move(a.points.A)
|
||||
.line(a.points.B)
|
||||
.curve(a.points.BCp2, a.points.CCp1, a.points.C)
|
||||
.curve(a.points.DCp1, a.points.DCp1, a.points.D)
|
||||
const test = new Path().move(A).line(B).curve(BCp2, CCp1, C).curve(DCp1, DCp1, D)
|
||||
|
||||
a.points.split = a.paths.test.shiftAlong(20)
|
||||
let halves = a.paths.test.split(a.points.split)
|
||||
const split = test.shiftAlong(20)
|
||||
let halves = test.split(split)
|
||||
let line = halves[0].ops.pop()
|
||||
expect(line.type).to.equal('line')
|
||||
expect(round(line.to.x)).to.equal(29.81)
|
||||
|
@ -708,22 +567,12 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should trim a path when lines overlap', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(0, 0)
|
||||
a.points.B = new a.Point(100, 100)
|
||||
a.points.C = new a.Point(0, 100)
|
||||
a.points.D = new a.Point(100, 0)
|
||||
const A = new Point(0, 0)
|
||||
const B = new Point(100, 100)
|
||||
const C = new Point(0, 100)
|
||||
const D = new Point(100, 0)
|
||||
|
||||
let test = new a.Path()
|
||||
.move(new a.Point(0, 20))
|
||||
.line(a.points.A)
|
||||
.line(a.points.B)
|
||||
.line(a.points.C)
|
||||
.line(a.points.D)
|
||||
.line(a.points.A)
|
||||
.trim()
|
||||
let test = new Path().move(new Point(0, 20)).line(A).line(B).line(C).line(D).line(A).trim()
|
||||
|
||||
expect(test.ops.length).to.equal(5)
|
||||
expect(test.ops[2].to.x).to.equal(50)
|
||||
|
@ -731,21 +580,18 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should trim a path when a line overlaps with a curve', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(0, 0)
|
||||
a.points.B = new a.Point(100, 100)
|
||||
a.points.C = new a.Point(0, 100)
|
||||
a.points.D = new a.Point(100, 0)
|
||||
const A = new Point(0, 0)
|
||||
const B = new Point(100, 100)
|
||||
const C = new Point(0, 100)
|
||||
const D = new Point(100, 0)
|
||||
|
||||
let test = new a.Path()
|
||||
.move(new a.Point(0, 20))
|
||||
.line(a.points.A)
|
||||
.curve(a.points.D, a.points.B, a.points.B)
|
||||
.line(a.points.C)
|
||||
.line(a.points.D)
|
||||
.line(a.points.A)
|
||||
let test = new Path()
|
||||
.move(new Point(0, 20))
|
||||
.line(A)
|
||||
.curve(D, B, B)
|
||||
.line(C)
|
||||
.line(D)
|
||||
.line(A)
|
||||
.trim()
|
||||
|
||||
expect(test.ops.length).to.equal(5)
|
||||
|
@ -754,21 +600,18 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should trim a path when a curves overlap', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(0, 0)
|
||||
a.points.B = new a.Point(100, 100)
|
||||
a.points.C = new a.Point(0, 100)
|
||||
a.points.D = new a.Point(100, 0)
|
||||
const A = new Point(0, 0)
|
||||
const B = new Point(100, 100)
|
||||
const C = new Point(0, 100)
|
||||
const D = new Point(100, 0)
|
||||
|
||||
let test = new a.Path()
|
||||
.move(new a.Point(0, 20))
|
||||
.line(a.points.A)
|
||||
.curve(a.points.D, a.points.B, a.points.B)
|
||||
.line(a.points.C)
|
||||
.curve(a.points.C, a.points.A, a.points.D)
|
||||
.line(a.points.A)
|
||||
let test = new Path()
|
||||
.move(new Point(0, 20))
|
||||
.line(A)
|
||||
.curve(D, B, B)
|
||||
.line(C)
|
||||
.curve(C, A, D)
|
||||
.line(A)
|
||||
.trim()
|
||||
|
||||
expect(test.ops.length).to.equal(5)
|
||||
|
@ -777,15 +620,12 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should translate a path', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
a.points.A = new a.Point(0, 0)
|
||||
a.points.B = new a.Point(100, 100)
|
||||
a.points.C = new a.Point(0, 100)
|
||||
a.points.D = new a.Point(100, 0)
|
||||
const A = new Point(0, 0)
|
||||
const B = new Point(100, 100)
|
||||
const C = new Point(0, 100)
|
||||
const D = new Point(100, 0)
|
||||
|
||||
let base = new a.Path().move(a.points.A).curve(a.points.B, a.points.C, a.points.D)
|
||||
let base = new Path().move(A).curve(B, C, D)
|
||||
let test = base.translate(10, 20)
|
||||
|
||||
expect(test.ops.length).to.equal(2)
|
||||
|
@ -796,16 +636,12 @@ describe('Path', () => {
|
|||
})
|
||||
|
||||
it('Should add a path attribute', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.line = new a.Path()
|
||||
.move(new a.Point(0, 0))
|
||||
.line(new a.Point(0, 40))
|
||||
const line = new Path()
|
||||
.move(new Point(0, 0))
|
||||
.line(new Point(0, 40))
|
||||
.attr('class', 'foo')
|
||||
.attr('class', 'bar')
|
||||
expect(a.paths.line.attributes.get('class')).to.equal('foo bar')
|
||||
expect(line.attributes.get('class')).to.equal('foo bar')
|
||||
})
|
||||
|
||||
it('Should overwrite a path attribute', () => {
|
||||
|
@ -826,27 +662,23 @@ describe('Path', () => {
|
|||
pattern.draft().render()
|
||||
|
||||
// Paths from shorthand have the log method
|
||||
expect(pattern.parts.test.paths.line.attributes.get('class')).to.equal('overwritten')
|
||||
expect(pattern.parts[0].test.paths.line.attributes.get('class')).to.equal('overwritten')
|
||||
})
|
||||
|
||||
it('Should move along a path even if it lands just on a joint', () => {
|
||||
let pattern = new Pattern()
|
||||
pattern.parts.a = pattern.__createPartWithContext('a')
|
||||
let a = pattern.parts.a
|
||||
|
||||
a.paths.curve = new a.Path()
|
||||
.move(new a.Point(20.979322245694167, -219.8547313525503))
|
||||
const curve = new Path()
|
||||
.move(new Point(20.979322245694167, -219.8547313525503))
|
||||
._curve(
|
||||
new a.Point(35.33122482627704, -153.54225517257478),
|
||||
new a.Point(61.99376179214562, -105.99242252587702)
|
||||
new Point(35.33122482627704, -153.54225517257478),
|
||||
new Point(61.99376179214562, -105.99242252587702)
|
||||
)
|
||||
.curve(
|
||||
new a.Point(88.85254026593002, -58.092613773317105),
|
||||
new a.Point(136.13264764576948, -11.692646171119936),
|
||||
new a.Point(170.69593749999996, -4.180844669736632e-14)
|
||||
new Point(88.85254026593002, -58.092613773317105),
|
||||
new Point(136.13264764576948, -11.692646171119936),
|
||||
new Point(170.69593749999996, -4.180844669736632e-14)
|
||||
)
|
||||
a.points.test = a.paths.curve.shiftAlong(121.36690836797631)
|
||||
expect(a.points.test).to.be.instanceOf(a.Point)
|
||||
const test = curve.shiftAlong(121.36690836797631)
|
||||
expect(test).to.be.instanceOf(Point)
|
||||
})
|
||||
|
||||
it('Should add log methods to a path', () => {
|
||||
|
@ -1083,8 +915,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.store.logs.error.length).to.equal(2)
|
||||
expect(pattern.store.logs.error[0]).to.equal(
|
||||
expect(pattern.stores[0].logs.error.length).to.equal(2)
|
||||
expect(pattern.stores[0].logs.error[0]).to.equal(
|
||||
'Called `Path.offset(distance)` but `distance` is not a number'
|
||||
)
|
||||
})
|
||||
|
@ -1101,8 +933,8 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.store.logs.error.length).to.equal(2)
|
||||
expect(pattern.store.logs.error[0]).to.equal(
|
||||
expect(pattern.stores[0].logs.error.length).to.equal(2)
|
||||
expect(pattern.stores[0].logs.error[0]).to.equal(
|
||||
'Called `Path.join(that)` but `that` is not a `Path` object'
|
||||
)
|
||||
})
|
||||
|
@ -1150,7 +982,7 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.store.logs.error[0]).to.equal(
|
||||
expect(pattern.stores[0].logs.error[0]).to.equal(
|
||||
'Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number'
|
||||
)
|
||||
})
|
||||
|
@ -1166,7 +998,7 @@ describe('Path', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const pattern = new design()
|
||||
pattern.draft()
|
||||
expect(pattern.store.logs.error[0]).to.equal(
|
||||
expect(pattern.stores[0].logs.error[0]).to.equal(
|
||||
'Called `Path.split(point)` but `point` is not a `Point` object'
|
||||
)
|
||||
})
|
||||
|
|
|
@ -14,11 +14,11 @@ describe('Pattern', () => {
|
|||
it('Pattern constructor should add enumerable properties', () => {
|
||||
const Pattern = new Design()
|
||||
const pattern = new Pattern()
|
||||
expect(typeof pattern.settings).to.equal('object')
|
||||
expect(Array.isArray(pattern.settings)).to.equal(true)
|
||||
expect(Array.isArray(pattern.stores)).to.equal(true)
|
||||
expect(typeof pattern.config).to.equal('object')
|
||||
expect(typeof pattern.parts).to.equal('object')
|
||||
expect(typeof pattern.store).to.equal('object')
|
||||
expect(Object.keys(pattern).length).to.equal(5)
|
||||
expect(typeof pattern.store).to.equal('undefined')
|
||||
expect(Object.keys(pattern).length).to.equal(4)
|
||||
})
|
||||
|
||||
it('Pattern constructor should add non-enumerable properties', () => {
|
||||
|
@ -60,7 +60,7 @@ describe('Pattern', () => {
|
|||
}
|
||||
for (const [key, value] of Object.entries(dflts)) {
|
||||
if (typeof value === 'object') expect(Object.keys(value).length).to.equal(0)
|
||||
else expect(pattern.settings[key]).to.equal(value)
|
||||
else expect(pattern.settings[0][key]).to.equal(value)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -154,8 +154,8 @@ describe('Pattern', () => {
|
|||
})
|
||||
|
||||
it('Pattern.init() should set config data in the store', () => {
|
||||
expect(pattern.store.get('data.name')).to.equal('test')
|
||||
expect(pattern.store.get('data.version')).to.equal('1.2.3')
|
||||
expect(pattern.stores[0].get('data.name')).to.equal('test')
|
||||
expect(pattern.stores[0].get('data.version')).to.equal('1.2.3')
|
||||
})
|
||||
|
||||
it('Pattern.init() should resolve dependencies', () => {
|
||||
|
@ -282,58 +282,58 @@ describe('Pattern', () => {
|
|||
expect(pattern.config.draftOrder[2]).to.equal('partC')
|
||||
expect(pattern.config.draftOrder[3]).to.equal('partR')
|
||||
// Points
|
||||
expect(pattern.parts.partA.points.a1.x).to.equal(1)
|
||||
expect(pattern.parts.partA.points.a1.y).to.equal(1)
|
||||
expect(pattern.parts.partA.points.a2.x).to.equal(11)
|
||||
expect(pattern.parts.partA.points.a2.y).to.equal(11)
|
||||
expect(pattern.parts.partB.points.b1.x).to.equal(2)
|
||||
expect(pattern.parts.partB.points.b1.y).to.equal(2)
|
||||
expect(pattern.parts.partB.points.b2.x).to.equal(22)
|
||||
expect(pattern.parts.partB.points.b2.y).to.equal(22)
|
||||
expect(pattern.parts.partC.points.c1.x).to.equal(3)
|
||||
expect(pattern.parts.partC.points.c1.y).to.equal(3)
|
||||
expect(pattern.parts.partC.points.c2.x).to.equal(33)
|
||||
expect(pattern.parts.partC.points.c2.y).to.equal(33)
|
||||
expect(pattern.parts.partR.points.r1.x).to.equal(4)
|
||||
expect(pattern.parts.partR.points.r1.y).to.equal(4)
|
||||
expect(pattern.parts.partR.points.r2.x).to.equal(44)
|
||||
expect(pattern.parts.partR.points.r2.y).to.equal(44)
|
||||
expect(pattern.parts[0].partA.points.a1.x).to.equal(1)
|
||||
expect(pattern.parts[0].partA.points.a1.y).to.equal(1)
|
||||
expect(pattern.parts[0].partA.points.a2.x).to.equal(11)
|
||||
expect(pattern.parts[0].partA.points.a2.y).to.equal(11)
|
||||
expect(pattern.parts[0].partB.points.b1.x).to.equal(2)
|
||||
expect(pattern.parts[0].partB.points.b1.y).to.equal(2)
|
||||
expect(pattern.parts[0].partB.points.b2.x).to.equal(22)
|
||||
expect(pattern.parts[0].partB.points.b2.y).to.equal(22)
|
||||
expect(pattern.parts[0].partC.points.c1.x).to.equal(3)
|
||||
expect(pattern.parts[0].partC.points.c1.y).to.equal(3)
|
||||
expect(pattern.parts[0].partC.points.c2.x).to.equal(33)
|
||||
expect(pattern.parts[0].partC.points.c2.y).to.equal(33)
|
||||
expect(pattern.parts[0].partR.points.r1.x).to.equal(4)
|
||||
expect(pattern.parts[0].partR.points.r1.y).to.equal(4)
|
||||
expect(pattern.parts[0].partR.points.r2.x).to.equal(44)
|
||||
expect(pattern.parts[0].partR.points.r2.y).to.equal(44)
|
||||
// Paths in partA
|
||||
expect(pattern.parts.partA.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partA.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partA.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partA.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[1].to.y).to.equal(11)
|
||||
// Paths in partB
|
||||
expect(pattern.parts.partB.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partB.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partB.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partB.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts.partB.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts.partB.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts.partB.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts.partB.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[1].to.y).to.equal(22)
|
||||
// Paths in partC
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts.partC.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts.partC.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts.partC.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts.partC.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts.partC.paths.c.ops[0].to.x).to.equal(3)
|
||||
expect(pattern.parts.partC.paths.c.ops[0].to.y).to.equal(3)
|
||||
expect(pattern.parts.partC.paths.c.ops[1].to.x).to.equal(33)
|
||||
expect(pattern.parts.partC.paths.c.ops[1].to.y).to.equal(33)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[0].to.x).to.equal(3)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[0].to.y).to.equal(3)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[1].to.x).to.equal(33)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[1].to.y).to.equal(33)
|
||||
// Paths in partR
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts.partR.paths.r.ops[0].to.x).to.equal(4)
|
||||
expect(pattern.parts.partR.paths.r.ops[0].to.y).to.equal(4)
|
||||
expect(pattern.parts.partR.paths.r.ops[1].to.x).to.equal(44)
|
||||
expect(pattern.parts.partR.paths.r.ops[1].to.y).to.equal(44)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partR.paths.r.ops[0].to.x).to.equal(4)
|
||||
expect(pattern.parts[0].partR.paths.r.ops[0].to.y).to.equal(4)
|
||||
expect(pattern.parts[0].partR.paths.r.ops[1].to.x).to.equal(44)
|
||||
expect(pattern.parts[0].partR.paths.r.ops[1].to.y).to.equal(44)
|
||||
})
|
||||
|
||||
it('Pattern.init() should resolve nested dependencies', () => {
|
||||
|
@ -428,54 +428,54 @@ describe('Pattern', () => {
|
|||
expect(pattern.config.draftOrder[2]).to.equal('partC')
|
||||
expect(pattern.config.draftOrder[3]).to.equal('partD')
|
||||
// Points
|
||||
expect(pattern.parts.partA.points.a1.x).to.equal(1)
|
||||
expect(pattern.parts.partA.points.a1.y).to.equal(1)
|
||||
expect(pattern.parts.partA.points.a2.x).to.equal(11)
|
||||
expect(pattern.parts.partA.points.a2.y).to.equal(11)
|
||||
expect(pattern.parts.partB.points.b1.x).to.equal(2)
|
||||
expect(pattern.parts.partB.points.b1.y).to.equal(2)
|
||||
expect(pattern.parts.partB.points.b2.x).to.equal(22)
|
||||
expect(pattern.parts.partB.points.b2.y).to.equal(22)
|
||||
expect(pattern.parts.partC.points.c1.x).to.equal(3)
|
||||
expect(pattern.parts.partC.points.c1.y).to.equal(3)
|
||||
expect(pattern.parts.partC.points.c2.x).to.equal(33)
|
||||
expect(pattern.parts.partC.points.c2.y).to.equal(33)
|
||||
expect(pattern.parts.partD.points.d1.x).to.equal(4)
|
||||
expect(pattern.parts.partD.points.d1.y).to.equal(4)
|
||||
expect(pattern.parts.partD.points.d2.x).to.equal(44)
|
||||
expect(pattern.parts.partD.points.d2.y).to.equal(44)
|
||||
expect(pattern.parts[0].partA.points.a1.x).to.equal(1)
|
||||
expect(pattern.parts[0].partA.points.a1.y).to.equal(1)
|
||||
expect(pattern.parts[0].partA.points.a2.x).to.equal(11)
|
||||
expect(pattern.parts[0].partA.points.a2.y).to.equal(11)
|
||||
expect(pattern.parts[0].partB.points.b1.x).to.equal(2)
|
||||
expect(pattern.parts[0].partB.points.b1.y).to.equal(2)
|
||||
expect(pattern.parts[0].partB.points.b2.x).to.equal(22)
|
||||
expect(pattern.parts[0].partB.points.b2.y).to.equal(22)
|
||||
expect(pattern.parts[0].partC.points.c1.x).to.equal(3)
|
||||
expect(pattern.parts[0].partC.points.c1.y).to.equal(3)
|
||||
expect(pattern.parts[0].partC.points.c2.x).to.equal(33)
|
||||
expect(pattern.parts[0].partC.points.c2.y).to.equal(33)
|
||||
expect(pattern.parts[0].partD.points.d1.x).to.equal(4)
|
||||
expect(pattern.parts[0].partD.points.d1.y).to.equal(4)
|
||||
expect(pattern.parts[0].partD.points.d2.x).to.equal(44)
|
||||
expect(pattern.parts[0].partD.points.d2.y).to.equal(44)
|
||||
// Paths in partA
|
||||
expect(pattern.parts.partA.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partA.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partA.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partA.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partA.paths.a.ops[1].to.y).to.equal(11)
|
||||
// Paths in partB
|
||||
expect(pattern.parts.partB.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partB.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partB.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partB.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts.partB.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts.partB.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts.partB.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts.partB.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partB.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts[0].partB.paths.b.ops[1].to.y).to.equal(22)
|
||||
// Paths in partC
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts.partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts.partC.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts.partC.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts.partC.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts.partC.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts.partC.paths.c.ops[0].to.x).to.equal(3)
|
||||
expect(pattern.parts.partC.paths.c.ops[0].to.y).to.equal(3)
|
||||
expect(pattern.parts.partC.paths.c.ops[1].to.x).to.equal(33)
|
||||
expect(pattern.parts.partC.paths.c.ops[1].to.y).to.equal(33)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.x).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[0].to.y).to.equal(1)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.x).to.equal(11)
|
||||
expect(pattern.parts[0].partC.paths.a.ops[1].to.y).to.equal(11)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[0].to.x).to.equal(2)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[0].to.y).to.equal(2)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[1].to.x).to.equal(22)
|
||||
expect(pattern.parts[0].partC.paths.b.ops[1].to.y).to.equal(22)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[0].to.x).to.equal(3)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[0].to.y).to.equal(3)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[1].to.x).to.equal(33)
|
||||
expect(pattern.parts[0].partC.paths.c.ops[1].to.y).to.equal(33)
|
||||
// Paths in partR
|
||||
expect(pattern.parts.partD.paths.d.ops[0].to.x).to.equal(4)
|
||||
expect(pattern.parts.partD.paths.d.ops[0].to.y).to.equal(4)
|
||||
expect(pattern.parts.partD.paths.d.ops[1].to.x).to.equal(44)
|
||||
expect(pattern.parts.partD.paths.d.ops[1].to.y).to.equal(44)
|
||||
expect(pattern.parts[0].partD.paths.d.ops[0].to.x).to.equal(4)
|
||||
expect(pattern.parts[0].partD.paths.d.ops[0].to.y).to.equal(4)
|
||||
expect(pattern.parts[0].partD.paths.d.ops[1].to.x).to.equal(44)
|
||||
expect(pattern.parts[0].partD.paths.d.ops[1].to.y).to.equal(44)
|
||||
})
|
||||
|
||||
it('Pattern.init() should load a single plugin', () => {
|
||||
|
@ -635,30 +635,35 @@ describe('Pattern', () => {
|
|||
})
|
||||
|
||||
it('Should check whether created parts get the pattern context', () => {
|
||||
const Pattern = new Design()
|
||||
const part = {
|
||||
name: 'test',
|
||||
draft: ({ part }) => part,
|
||||
}
|
||||
const Pattern = new Design({ parts: [part] })
|
||||
const pattern = new Pattern()
|
||||
const part = pattern.__createPartWithContext('test')
|
||||
expect(typeof part.context).to.equal('object')
|
||||
expect(typeof part.context.parts).to.equal('object')
|
||||
expect(typeof part.context.config).to.equal('object')
|
||||
expect(typeof part.context.config.options).to.equal('object')
|
||||
expect(typeof part.context.config.data).to.equal('object')
|
||||
expect(Array.isArray(part.context.config.measurements)).to.equal(true)
|
||||
expect(Array.isArray(part.context.config.optionalMeasurements)).to.equal(true)
|
||||
expect(Array.isArray(part.context.config.parts)).to.equal(true)
|
||||
expect(Array.isArray(part.context.config.plugins)).to.equal(true)
|
||||
expect(part.context.settings).to.equal(pattern.settings)
|
||||
expect(typeof part.context.store).to.equal('object')
|
||||
expect(typeof part.context.store.log).to.equal('object')
|
||||
expect(typeof part.context.store.log.debug).to.equal('function')
|
||||
expect(typeof part.context.store.log.info).to.equal('function')
|
||||
expect(typeof part.context.store.log.warning).to.equal('function')
|
||||
expect(typeof part.context.store.log.error).to.equal('function')
|
||||
expect(typeof part.context.store.logs).to.equal('object')
|
||||
expect(Array.isArray(part.context.store.logs.debug)).to.equal(true)
|
||||
expect(Array.isArray(part.context.store.logs.info)).to.equal(true)
|
||||
expect(Array.isArray(part.context.store.logs.warning)).to.equal(true)
|
||||
expect(Array.isArray(part.context.store.logs.error)).to.equal(true)
|
||||
pattern.draft()
|
||||
const context = pattern.parts[0].test.context
|
||||
expect(typeof context).to.equal('object')
|
||||
expect(typeof context.parts).to.equal('object')
|
||||
expect(typeof context.config).to.equal('object')
|
||||
expect(typeof context.config.options).to.equal('object')
|
||||
expect(typeof pattern.parts[0].test.context.config.data).to.equal('object')
|
||||
expect(Array.isArray(context.config.measurements)).to.equal(true)
|
||||
expect(Array.isArray(context.config.optionalMeasurements)).to.equal(true)
|
||||
expect(Array.isArray(context.config.parts)).to.equal(true)
|
||||
expect(Array.isArray(context.config.plugins)).to.equal(true)
|
||||
expect(context.settings).to.equal(pattern.settings[0])
|
||||
expect(typeof context.store).to.equal('object')
|
||||
expect(typeof context.store.log).to.equal('object')
|
||||
expect(typeof context.store.log.debug).to.equal('function')
|
||||
expect(typeof context.store.log.info).to.equal('function')
|
||||
expect(typeof context.store.log.warning).to.equal('function')
|
||||
expect(typeof context.store.log.error).to.equal('function')
|
||||
expect(typeof context.store.logs).to.equal('object')
|
||||
expect(Array.isArray(context.store.logs.debug)).to.equal(true)
|
||||
expect(Array.isArray(context.store.logs.info)).to.equal(true)
|
||||
expect(Array.isArray(context.store.logs.warning)).to.equal(true)
|
||||
expect(Array.isArray(context.store.logs.error)).to.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -690,27 +695,27 @@ describe('Pattern', () => {
|
|||
pattern.init()
|
||||
|
||||
it('Pattern settings should contain percentage options', () => {
|
||||
expect(pattern.settings.options.pct).to.equal(0.3)
|
||||
expect(pattern.settings[0].options.pct).to.equal(0.3)
|
||||
})
|
||||
|
||||
it('Pattern settings should contain millimeter options', () => {
|
||||
expect(pattern.settings.options.mm).to.equal(12)
|
||||
expect(pattern.settings[0].options.mm).to.equal(12)
|
||||
})
|
||||
|
||||
it('Pattern settings should contain degree options', () => {
|
||||
expect(pattern.settings.options.deg).to.equal(2)
|
||||
expect(pattern.settings[0].options.deg).to.equal(2)
|
||||
})
|
||||
|
||||
it('Pattern settings should contain list options', () => {
|
||||
expect(pattern.settings.options.list).to.equal('d')
|
||||
expect(pattern.settings[0].options.list).to.equal('d')
|
||||
})
|
||||
|
||||
it('Pattern settings should contain count options', () => {
|
||||
expect(pattern.settings.options.count).to.equal(4)
|
||||
expect(pattern.settings[0].options.count).to.equal(4)
|
||||
})
|
||||
|
||||
it('Pattern settings should contain bool options', () => {
|
||||
expect(pattern.settings.options.bool).to.equal(false)
|
||||
expect(pattern.settings[0].options.bool).to.equal(false)
|
||||
})
|
||||
|
||||
it('Pattern should throw an error for an unknown option', () => {
|
||||
|
|
|
@ -10,6 +10,7 @@ describe('Snapped options', () => {
|
|||
it('Should snap a percentage options to equal steps', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs },
|
||||
},
|
||||
|
@ -17,13 +18,13 @@ describe('Snapped options', () => {
|
|||
const design = new Design({ parts: [part] })
|
||||
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
|
||||
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
|
||||
expect(patternA.settings.absoluteOptions.test).to.equal(60)
|
||||
expect(patternB.settings.absoluteOptions.test).to.equal(108)
|
||||
expect(patternA.settings[0].absoluteOptions.test).to.equal(60)
|
||||
expect(patternB.settings[0].absoluteOptions.test).to.equal(108)
|
||||
})
|
||||
|
||||
it('Should snap a percentage options to the Fibonacci sequence', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
test: {
|
||||
pct: 30,
|
||||
|
@ -38,9 +39,9 @@ describe('Snapped options', () => {
|
|||
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
|
||||
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
|
||||
const patternC = new design({ options: { test: 0.97 }, measurements }).draft()
|
||||
expect(patternA.settings.absoluteOptions.test).to.equal(55)
|
||||
expect(patternB.settings.absoluteOptions.test).to.equal(89)
|
||||
expect(patternC.settings.absoluteOptions.test).to.equal(388)
|
||||
expect(patternA.settings[0].absoluteOptions.test).to.equal(55)
|
||||
expect(patternB.settings[0].absoluteOptions.test).to.equal(89)
|
||||
expect(patternC.settings[0].absoluteOptions.test).to.equal(388)
|
||||
})
|
||||
|
||||
it('Should snap a percentage options to imperial snaps', () => {
|
||||
|
@ -64,10 +65,10 @@ describe('Snapped options', () => {
|
|||
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' }).draft()
|
||||
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' }).draft()
|
||||
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'metric' }).draft()
|
||||
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)
|
||||
expect(patternA.settings[0].absoluteOptions.test).to.equal(50)
|
||||
expect(patternB.settings[0].absoluteOptions.test).to.equal(100)
|
||||
expect(patternC.settings[0].absoluteOptions.test).to.equal(388)
|
||||
expect(patternD.settings[0].absoluteOptions.test).to.equal(4)
|
||||
})
|
||||
|
||||
it('Should snap a percentage options to metrics snaps', () => {
|
||||
|
@ -107,9 +108,9 @@ describe('Snapped options', () => {
|
|||
measurements,
|
||||
units: 'imperial',
|
||||
}).draft()
|
||||
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)
|
||||
expect(patternA.settings[0].absoluteOptions.test).to.equal(50.8)
|
||||
expect(patternB.settings[0].absoluteOptions.test).to.equal(101.6)
|
||||
expect(patternC.settings[0].absoluteOptions.test).to.equal(388)
|
||||
expect(patternD.settings[0].absoluteOptions.test).to.equal(4)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -69,8 +69,8 @@ describe('Store', () => {
|
|||
const Test = new Design({ plugins: [plugin], parts: [part] })
|
||||
const pattern = new Test()
|
||||
pattern.draft()
|
||||
expect(pattern.store.get('test.message.warning')).to.equal('hello warning')
|
||||
expect(pattern.store.get('test.message.info')).to.equal('hello info')
|
||||
expect(pattern.stores[0].get('test.message.warning')).to.equal('hello warning')
|
||||
expect(pattern.stores[0].get('test.message.info')).to.equal('hello info')
|
||||
})
|
||||
|
||||
it('Should make top-level plugin methods available via shorthand', () => {
|
||||
|
@ -103,7 +103,7 @@ describe('Store', () => {
|
|||
const Test = new Design({ plugins: [plugin], parts: [part] })
|
||||
const pattern = new Test()
|
||||
pattern.draft()
|
||||
expect(pattern.store.get('test.example_part.a')).to.equal('hello A')
|
||||
expect(pattern.store.get('test.example_part.b')).to.equal('hello B')
|
||||
expect(pattern.stores[0].get('test.example_part.a')).to.equal('hello A')
|
||||
expect(pattern.stores[0].get('test.example_part.b')).to.equal('hello B')
|
||||
})
|
||||
})
|
||||
|
|
|
@ -32,7 +32,7 @@ const { expect } = chai
|
|||
|
||||
describe('Utils', () => {
|
||||
it('Should return the correct macro name', () => {
|
||||
expect(macroName('test')).to.equal('_macro_test')
|
||||
expect(macroName('test')).to.equal('__macro_test')
|
||||
})
|
||||
|
||||
it('Should find the intersection of two endless line segments', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue