1
0
Fork 0

start refactoring tests for speed

This commit is contained in:
Enoch Riese 2022-11-15 14:50:14 -06:00
parent 6c68b33f3e
commit d921cc700d
5 changed files with 251 additions and 403 deletions

View file

@ -631,7 +631,7 @@ export function __asNumber(value, param, method, log) {
value = Number(value) value = Number(value)
return value return value
} catch { } catch {
this.log.error( log.error(
`Called \`${method}(${param})\` but \`${param}\` is not a number nor can it be cast to one` `Called \`${method}(${param})\` but \`${param}\` is not a number nor can it be cast to one`
) )
} }

View file

@ -63,7 +63,6 @@ describe('Part', () => {
const part = { const part = {
name: 'test', name: 'test',
draft: ({ getId, part }) => { draft: ({ getId, part }) => {
console.log(getId)
id = getId() id = getId()
id = getId() id = getId()
id = getId() id = getId()

View file

@ -1,289 +1,173 @@
import chai from 'chai' import chai from 'chai'
import { round, Path, Point, Design } from '../src/index.mjs' import { round, Path, Point, Design } from '../src/index.mjs'
import { pathsProxy } from '../src/path.mjs'
const expect = chai.expect const expect = chai.expect
describe('Path', () => { describe('Path', () => {
it('Should draw a smurve', () => { describe('smurve', () => {
const part = { it('Should draw a smurve', () => {
name: 'test', const points = {}
draft: ({ Point, points, Path, paths, part }) => { points.from = new Point(10, 20)
points.from = new Point(10, 20) points.cp1 = new Point(40, 10)
points.cp1 = new Point(40, 10) points.cp2 = new Point(60, 30)
points.cp2 = new Point(60, 30) points.to = new Point(90, 20)
points.to = new Point(90, 20) points.scp2 = new Point(140, 10)
points.scp2 = new Point(140, 10) points.sto = new Point(170, 20)
points.sto = new Point(170, 20)
paths.test = new Path() const test = new Path()
.move(points.from) .move(points.from)
.curve(points.cp1, points.cp2, points.to) .curve(points.cp1, points.cp2, points.to)
.smurve(points.scp2, points.sto) .smurve(points.scp2, points.sto)
return part expect(round(test.ops[2].cp1.x)).to.equal(120)
}, expect(round(test.ops[2].cp1.y)).to.equal(10)
} })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts[0].test.paths.test.ops[2].cp1.x)).to.equal(120) it('Should draw a smurve_', () => {
expect(round(pattern.parts[0].test.paths.test.ops[2].cp1.y)).to.equal(10) const points = {}
}) points.from = new Point(10, 20)
points.cp1 = new Point(40, 10)
points.cp2 = new Point(60, 30)
points.to = new Point(90, 20)
points.sto = new Point(170, 20)
it('Should draw a smurve_', () => { const test = new Path()
const part = { .move(points.from)
name: 'test', .curve(points.cp1, points.cp2, points.to)
draft: ({ Point, points, Path, paths, part }) => { .smurve_(points.sto)
points.from = new Point(10, 20)
points.cp1 = new Point(40, 10)
points.cp2 = new Point(60, 30)
points.to = new Point(90, 20)
points.sto = new Point(170, 20)
paths.test = new Path() expect(round(test.ops[2].cp1.x)).to.equal(120)
.move(points.from) expect(round(test.ops[2].cp1.y)).to.equal(10)
.curve(points.cp1, points.cp2, points.to) })
.smurve_(points.sto)
return part it('Should log a warning when passing a non-Point to smurve()', () => {
}, const points = {}
} points.from = new Point(10, 20)
const design = new Design({ parts: [part] }) points.cp1 = new Point(40, 10)
const pattern = new design() points.cp2 = new Point(60, 30)
pattern.draft().render() points.to = new Point(90, 20)
expect(round(pattern.parts[0].test.paths.test.ops[2].cp1.x)).to.equal(120) let invalid = false
expect(round(pattern.parts[0].test.paths.test.ops[2].cp1.y)).to.equal(10) const messages = []
}) const log = { warning: (msg) => messages.push(msg) }
const test = new Path()
.__withLog(log)
.move(points.from)
.curve(points.cp1, points.cp2, points.to)
.smurve('hi', 'there')
it('Should log a warning when passing a non-Point to smurve()', () => { expect(messages.length).to.equal(2)
const part = { expect(messages[0]).to.equal('Called `Path.smurve(cp2, to)` but `to` is not a `Point` object')
name: 'test', })
draft: ({ Point, points, Path, paths, part }) => {
points.from = new Point(10, 20)
points.cp1 = new Point(40, 10)
points.cp2 = new Point(60, 30)
points.to = new Point(90, 20)
paths.test = new Path() it('Should log a warning when passing a non-Point to smurve_()', () => {
.move(points.from) let invalid = false
.curve(points.cp1, points.cp2, points.to) const messages = []
.smurve('hi', 'there') const log = { warning: (msg) => messages.push(msg) }
try {
return part const test = new Path().__withLog(log).smurve_('hi')
}, } catch {
} } finally {
const design = new Design({ parts: [part] }) expect(messages.length).to.equal(1)
const pattern = new design() expect(messages[0]).to.equal('Called `Path.smurve_(to)` but `to` is not a `Point` object')
pattern.draft() }
expect(pattern.setStores[0].logs.warning.length).to.equal(2) })
expect(pattern.setStores[0].logs.warning[0]).to.equal(
'Called `Path.smurve(cp2, to)` but `to` is not a `Point` object'
)
})
it('Should log a warning when passing a non-Point to smurve_()', () => {
const part = {
name: 'test',
draft: ({ Path, paths, part }) => {
paths.test = new Path().smurve_('hi')
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.warning.length).to.equal(1)
expect(pattern.setStores[0].logs.warning[0]).to.equal(
'Called `Path.smurve_(to)` but `to` is not a `Point` object'
)
}) })
it('Should log a warning when passing a non-Path to the paths proxy', () => { it('Should log a warning when passing a non-Path to the paths proxy', () => {
const part = { let invalid = false
name: 'test', const messages = []
draft: ({ paths, part }) => { const log = { warning: (msg) => messages.push(msg) }
paths.test = 'Wriing code can get very lonely sometimes' const pathsObj = {}
const paths = pathsProxy(pathsObj, log)
paths.set(pathsObj, 'test', 'Writing code can get very lonely sometimes')
return part expect(messages.length).to.equal(2)
}, expect(messages[0]).to.equal('`paths.test` was set with a value that is not a `Path` object')
} expect(messages[1]).to.equal('Could not set `name` property on `paths.test`')
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.warning.length).to.equal(2)
expect(pattern.setStores[0].logs.warning[0]).to.equal(
'`paths.test` was set with a value that is not a `Path` object'
)
expect(pattern.setStores[0].logs.warning[1]).to.equal(
'Could not set `name` property on `paths.test`'
)
}) })
it('Should offset a line', () => { describe('offset', () => {
const part = { it('Should offset a line', () => {
name: 'test', const line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
draft: ({ paths, Path, Point, part }) => { const offLine = line.offset(10)
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)) const bbox = offLine.bbox()
paths.offset = paths.line.offset(10) expect(bbox.bottomRight.x).to.equal(-10)
return part expect(bbox.bottomRight.y).to.equal(40)
}, })
}
const design = new Design({ parts: [part] }) it('Should offset a curve', () => {
const pattern = new design() const curve = new Path()
pattern.draft().render() .move(new Point(0, 0))
expect(pattern.parts[0].test.paths.offset.bottomRight.x).to.equal(-10) .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
expect(pattern.parts[0].test.paths.offset.bottomRight.y).to.equal(40) const offset = curve.offset(10)
const bbox = offset.bbox()
expect(round(bbox.bottomRight.x)).to.equal(72.18)
expect(round(bbox.bottomRight.y)).to.equal(38.26)
})
it('Should offset a curve where cp1 = start', () => {
const curve = new Path().move(new Point(0, 0))._curve(new Point(123, 34), new Point(23, 4))
const offset = curve.offset(10)
const bbox = offset.bbox()
expect(round(bbox.bottomRight.x)).to.equal(72.63)
expect(round(bbox.bottomRight.y)).to.equal(26.47)
})
it('Should offset a curve where cp2 = end', () => {
const curve = new Path().move(new Point(0, 0)).curve_(new Point(40, 0), new Point(123, 34))
const offset = curve.offset(10)
const bbox = offset.bbox()
expect(round(bbox.bottomRight.x)).to.equal(119.86)
expect(round(bbox.bottomRight.y)).to.equal(43.49)
})
}) })
it('Should offset a curve', () => { describe('length', () => {
const part = { it('Should return the length of a line', () => {
name: 'test', const line = new Path().move(new Point(0, 0)).line(new Point(40, 0))
draft: ({ paths, Path, Point, part }) => { expect(line.length()).to.equal(40)
paths.curve = new Path() })
.move(new Point(0, 0))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.offset = paths.curve.offset(10)
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
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', () => { it('Should return the length of a curve', () => {
const part = { const curve = new Path()
name: 'test', .move(new Point(0, 0))
draft: ({ paths, Path, Point, part }) => { .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.curve = new Path().move(new Point(0, 0))._curve(new Point(123, 34), new Point(23, 4)) .close()
paths.offset = paths.curve.offset(10) expect(round(curve.length())).to.equal(145.11)
return part })
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
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.47)
})
it('Should offset a curve where cp2 = end', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path().move(new Point(0, 0)).curve_(new Point(40, 0), new Point(123, 34))
paths.offset = paths.curve.offset(10)
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts[0].test.paths.offset.bottomRight.x)).to.equal(119.86)
expect(round(pattern.parts[0].test.paths.offset.bottomRight.y)).to.equal(43.49)
})
it('Should return the length of a line', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path().move(new Point(0, 0)).line(new Point(40, 0))
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.parts[0].test.paths.line.length()).to.equal(40)
})
it('Should return the length of a curve', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close()
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts[0].test.paths.curve.length())).to.equal(145.11)
}) })
it('Should return the rough length of a curve', () => { it('Should return the rough length of a curve', () => {
const part = { const curve = new Path()
name: 'test', .move(new Point(0, 0))
draft: ({ paths, Path, Point, part }) => { .curve(new Point(0, 50), new Point(100, 50), new Point(100, 0))
paths.curve = new Path() .close()
.move(new Point(0, 0)) expect(round(curve.roughLength())).to.equal(300)
.curve(new Point(0, 50), new Point(100, 50), new Point(100, 0))
.close()
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts[0].test.paths.curve.roughLength())).to.equal(300)
}) })
it('Should return the rough length of a line', () => { it('Should return the rough length of a line', () => {
const part = { const line = new Path().move(new Point(0, 0)).line(new Point(0, 50))
name: 'test', expect(round(line.roughLength())).to.equal(50)
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 50))
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts[0].test.paths.line.roughLength())).to.equal(50)
}) })
it('Should return the path start point', () => { it('Should return the path start point', () => {
const part = { const curve = new Path()
name: 'test', .move(new Point(123, 456))
draft: ({ paths, Path, Point, part }) => { .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.curve = new Path() .close()
.move(new Point(123, 456)) expect(curve.start().x).to.equal(123)
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) expect(curve.start().y).to.equal(456)
.close()
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
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', () => { it('Should return the path end point', () => {
const part = { const curve = new Path()
name: 'test', .move(new Point(123, 456))
draft: ({ paths, Path, Point, part }) => { .curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.curve = new Path() .close()
.move(new Point(123, 456)) expect(curve.end().x).to.equal(123)
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4)) expect(curve.end().y).to.equal(456)
.close()
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
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', () => { it('Should calculate that path boundary', () => {
@ -810,24 +694,17 @@ describe('Path', () => {
}) })
it('Should overwrite a path attribute', () => { it('Should overwrite a path attribute', () => {
const part = { const line = new Path()
name: 'test', line.log = { debug: () => {} }
draft: ({ paths, Path, Point, part }) => { line
paths.line = new Path() .move(new Point(0, 0))
.move(new Point(0, 0)) .line(new Point(0, 40))
.line(new Point(0, 40)) .attr('class', 'foo')
.attr('class', 'foo') .attr('class', 'bar')
.attr('class', 'bar') .attr('class', 'overwritten', true)
.attr('class', 'overwritten', true)
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
// Paths from shorthand have the log method // Paths from shorthand have the log method
expect(pattern.parts[0].test.paths.line.attributes.get('class')).to.equal('overwritten') expect(line.attributes.get('class')).to.equal('overwritten')
}) })
it('Should move along a path even if it lands just on a joint', () => { it('Should move along a path even if it lands just on a joint', () => {
@ -1054,7 +931,6 @@ describe('Path', () => {
it('Should log a warning when setting an attribute without a name', () => { it('Should log a warning when setting an attribute without a name', () => {
let invalid = false let invalid = false
const log = { warning: () => (invalid = true) } const log = { warning: () => (invalid = true) }
expect(invalid).to.equal(false)
new Path().__withLog(log).attr() new Path().__withLog(log).attr()
expect(invalid).to.equal(true) expect(invalid).to.equal(true)
}) })
@ -1062,52 +938,41 @@ describe('Path', () => {
it('Should log a warning when setting an attribute without a value', () => { it('Should log a warning when setting an attribute without a value', () => {
let invalid = false let invalid = false
const log = { warning: () => (invalid = true) } const log = { warning: () => (invalid = true) }
expect(invalid).to.equal(false)
new Path().__withLog(log).attr('test') new Path().__withLog(log).attr('test')
expect(invalid).to.equal(true) expect(invalid).to.equal(true)
}) })
it('Should log a warning when calling offset without a distance', () => { it('Should log an error when calling offset without a distance', () => {
const part = { let invalid = true
name: 'test', const log = { warning: () => {}, error: () => (invalid = true) }
draft: ({ paths, Path, Point, points }) => { const pointLog = { error: () => {} }
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo') const pointA = new Point(0, 0).__withLog(pointLog)
paths.a = new Path().move(points.a).line(points.b) const pointB = new Point(0, 40).__withLog(pointLog)
paths.b = paths.a.offset() const a = new Path().__withLog(log).move(pointA).line(pointB)
return part const b = a.offset()
}, expect(invalid).to.equal(true)
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error.length).to.equal(2)
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Called `Path.offset(distance)` but `distance` is not a number'
)
}) })
it('Should log a warning when calling join without a path', () => { it('Should log an error when calling join without a path', () => {
const part = { let invalid = false
name: 'test', const log = { error: () => (invalid = true) }
draft: ({ paths, Path, Point, points }) => { const line = new Path()
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo') .move(new Point(0, 0))
paths.a = new Path().move(points.a).line(points.b).join() .line(new Point(0, 40))
return part .attr('class', 'foo')
}, .__withLog(log)
try {
line.join()
} catch {
} finally {
expect(invalid).to.equal(true)
} }
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error.length).to.equal(2)
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Called `Path.join(that)` but `that` is not a `Path` object'
)
}) })
it('Should log a warning when calling start on a path without drawing operations', () => { it('Should log a warning when calling start on a path without drawing operations', () => {
let invalid = false let invalid = false
const log = { error: () => (invalid = true) } const log = { error: () => (invalid = true) }
expect(invalid).to.equal(false)
try { try {
new Path().__withLog(log).start() new Path().__withLog(log).start()
} catch (err) { } catch (err) {
@ -1116,10 +981,9 @@ describe('Path', () => {
expect(invalid).to.equal(true) expect(invalid).to.equal(true)
}) })
it('Should log a warning when calling end on a path without drawing operations', () => { it('Should log an error when calling end on a path without drawing operations', () => {
let invalid = false let invalid = false
const log = { error: () => (invalid = true) } const log = { error: () => (invalid = true) }
expect(invalid).to.equal(false)
try { try {
new Path().__withLog(log).end() new Path().__withLog(log).end()
} catch (err) { } catch (err) {
@ -1128,7 +992,7 @@ describe('Path', () => {
expect(invalid).to.equal(true) expect(invalid).to.equal(true)
}) })
it('Should log a warning when calling shiftAlong but distance is not a number', () => { it('Should log an error when calling shiftAlong but distance is not a number', () => {
let invalid = false let invalid = false
const log = { error: () => (invalid = true) } const log = { error: () => (invalid = true) }
expect(invalid).to.equal(false) expect(invalid).to.equal(false)
@ -1136,53 +1000,32 @@ describe('Path', () => {
expect(invalid).to.equal(true) expect(invalid).to.equal(true)
}) })
it('Should log a warning when calling shiftFractionalong but fraction is not a number', () => { it('Should log an error when calling shiftFractionalong but fraction is not a number', () => {
const part = { let invalid = false
name: 'test', const log = { error: () => (invalid = true) }
draft: ({ Path, Point, points }) => { const a = new Path()
points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).shiftFractionAlong() .__withLog(log)
return part .move(new Point(0, 0))
}, .line(new Point(0, 40))
} .shiftFractionAlong()
const design = new Design({ parts: [part] }) expect(invalid).to.equal(true)
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number'
)
}) })
it('Should log a warning when splitting a path on a non-point', () => { it('Should log an error when splitting a path on a non-point', () => {
const part = { let invalid = false
name: 'test', const log = { error: () => (invalid = true) }
draft: ({ Path, Point, points, part }) => { try {
points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).split() const a = new Path().__withLog(log).move(new Point(0, 0)).line(new Point(0, 40)).split()
return part } catch {
}, } finally {
expect(invalid).to.equal(true)
} }
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.setStores[0].logs.error[0]).to.equal(
'Called `Path.split(point)` but `point` is not a `Point` object'
)
}) })
it('Should add a class', () => { it('Should add a class', () => {
const part = { const line = new Path().move(new Point(0, 0)).line(new Point(10, 10)).addClass('fabric banana')
name: 'test',
draft: ({ Path, paths, Point, part }) => { expect(line.attributes.get('class')).to.equal('fabric banana')
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(10, 10))
.addClass('fabric banana')
return part
},
}
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.parts[0].test.paths.line.attributes.get('class')).to.equal('fabric banana')
}) })
it('Should (un)hide a path with hide()/unhide()', () => { it('Should (un)hide a path with hide()/unhide()', () => {

View file

@ -132,26 +132,30 @@ describe('Point', () => {
expect(round(ss.shiftTowards(se, 200).y)).to.equal(-18.42) expect(round(ss.shiftTowards(se, 200).y)).to.equal(-18.42)
}) })
it('Should shift a point a fraction towards another', () => { describe('shiftFractionTowards', () => {
let origin = new Point(0, 0) it('Should shift a point a fraction towards another', () => {
let n = new Point(0, -10) let origin = new Point(0, 0)
let e = new Point(10, 0) let s = new Point(0, 10)
let s = new Point(0, 10) let ss = origin.shiftFractionTowards(s, 0.5)
let w = new Point(-10, 0) expect(round(ss.x)).to.equal(0)
let sn = origin.shiftFractionTowards(n, 1.5) expect(round(ss.y)).to.equal(5)
let se = origin.shiftFractionTowards(e, 1.5) })
let ss = origin.shiftFractionTowards(s, 0.5)
let sw = origin.shiftFractionTowards(w, 2.5) it('Should shift a point a fraction beyond another if the fraction is > 1', () => {
expect(round(sn.x)).to.equal(0) let origin = new Point(0, 0)
expect(round(sn.y)).to.equal(-15) let n = new Point(0, -10)
expect(round(se.x)).to.equal(15) let sn = origin.shiftFractionTowards(n, 1.5)
expect(round(se.y)).to.equal(0) expect(round(sn.x)).to.equal(0)
expect(round(ss.x)).to.equal(0) expect(round(sn.y)).to.equal(-15)
expect(round(ss.y)).to.equal(5) })
expect(round(sw.x)).to.equal(-25)
expect(round(sw.y)).to.equal(0) it('Should shift a point a fraction away from another if the fraction is < 0', () => {
expect(round(sw.shiftFractionTowards(sn, 100).x)).to.equal(2475) let origin = new Point(0, 0)
expect(round(ss.shiftFractionTowards(se, 200).y)).to.equal(-995) let n = new Point(0, -10)
let sn = origin.shiftFractionTowards(n, -0.5)
expect(round(sn.x)).to.equal(0)
expect(round(sn.y)).to.equal(5)
})
}) })
it('Should shift a point beyond another', () => { it('Should shift a point beyond another', () => {

View file

@ -154,48 +154,50 @@ describe('Utils', () => {
expect(hits.length).to.equal(3) expect(hits.length).to.equal(3)
}) })
it('Should find 9 intersections between two curves', () => { describe('curvesIntersect', function () {
this.timeout(15000) this.timeout(15000)
let A = new Point(10, 10) it('Should find 9 intersections between two curves', () => {
let Acp = new Point(310, 40) let A = new Point(10, 10)
let B = new Point(110, 70) let Acp = new Point(310, 40)
let Bcp = new Point(-210, 40) let B = new Point(110, 70)
let C = new Point(20, -5) let Bcp = new Point(-210, 40)
let Ccp = new Point(60, 300) let C = new Point(20, -5)
let D = new Point(100, 85) let Ccp = new Point(60, 300)
let Dcp = new Point(70, -220) let D = new Point(100, 85)
let Dcp = new Point(70, -220)
let hits = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D) let hits = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D)
expect(hits.length).to.equal(9) expect(hits.length).to.equal(9)
}) })
it('Should find 1 intersection between two curves', () => { it('Should find 1 intersection between two curves', () => {
let A = new Point(10, 10) let A = new Point(10, 10)
let Acp = new Point(310, 40) let Acp = new Point(310, 40)
let B = new Point(110, 70) let B = new Point(110, 70)
let Bcp = new Point(-210, 40) let Bcp = new Point(-210, 40)
let C = new Point(20, -5) let C = new Point(20, -5)
let Ccp = new Point(-60, 300) let Ccp = new Point(-60, 300)
let D = new Point(-200, 85) let D = new Point(-200, 85)
let Dcp = new Point(-270, -220) let Dcp = new Point(-270, -220)
let hit = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D) let hit = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D)
expect(round(hit.x)).to.equal(15.58) expect(round(hit.x)).to.equal(15.58)
expect(round(hit.y)).to.equal(10.56) expect(round(hit.y)).to.equal(10.56)
}) })
it('Should find no intersection between two curves', () => { it('Should find no intersection between two curves', () => {
let A = new Point(10, 10) let A = new Point(10, 10)
let Acp = new Point(310, 40) let Acp = new Point(310, 40)
let B = new Point(110, 70) let B = new Point(110, 70)
let Bcp = new Point(-210, 40) let Bcp = new Point(-210, 40)
let C = new Point(20, -5) let C = new Point(20, -5)
let Ccp = new Point(-60, -300) let Ccp = new Point(-60, -300)
let D = new Point(-200, 85) let D = new Point(-200, 85)
let Dcp = new Point(-270, -220) let Dcp = new Point(-270, -220)
let hit = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D) let hit = curvesIntersect(A, Acp, Bcp, B, C, Ccp, Dcp, D)
expect(hit).to.equal(false) expect(hit).to.equal(false)
})
}) })
it('Should correctly format units', () => { it('Should correctly format units', () => {