1
0
Fork 0

fix (core) fix rounding in count sampling. add tests

This commit is contained in:
Enoch Riese 2023-04-19 11:30:32 -04:00
parent 8ccab0df21
commit c6d4fc3aea
4 changed files with 132 additions and 185 deletions

View file

@ -193,6 +193,7 @@ PatternSampler.prototype.__optionSets = function (optionName) {
}
step = (option.max / factor - val) / stepFactor
const base = this.__setBase()
const roundVal = typeof option.count !== 'undefined' || typeof option.mm !== 'undefined'
for (let run = 1; run <= numberRuns; run++) {
const settings = {
...base,
@ -202,11 +203,9 @@ PatternSampler.prototype.__optionSets = function (optionName) {
idPrefix: `sample-${run}`,
partClasses: `sample-${run}`,
}
settings.options[optionName] = val
settings.options[optionName] = roundVal ? Math.ceil(val) : val
sets.push(settings)
val += step
if (typeof option.count !== 'undefined' || typeof option.mm !== 'undefined')
val = Math.round(val)
}
return sets

View file

@ -30,12 +30,6 @@ describe('Pattern', () => {
expect(typeof pattern.Path).to.equal('function')
expect(typeof pattern.Snippet).to.equal('function')
expect(typeof pattern.Attributes).to.equal('function')
// expect(typeof pattern.__designParts).to.equal('object')
// expect(typeof pattern.config.inject).to.equal('object')
// expect(typeof pattern.config.directDependencies).to.equal('object')
// expect(typeof pattern.__resolvedDependencies).to.equal('object')
// expect(typeof pattern.__hide).to.equal('object')
// expect(Array.isArray(pattern.__draftOrder)).to.equal(true)
expect(pattern.width).to.equal(0)
expect(pattern.height).to.equal(0)
expect(pattern.is).to.equal('')
@ -631,7 +625,7 @@ describe('Pattern', () => {
expect(pattern.plugins.hooks.preRender).to.have.lengthOf(1)
})
it('Pattern.__init() should not load conditional plugin if condition is not mett', () => {
it('Pattern.__init() should not load conditional plugin if condition is not met', () => {
const plugin = {
name: 'example',
version: 1,
@ -812,6 +806,32 @@ describe('Pattern', () => {
expect(count).to.equal(2)
})
it('Pattern.__init() should not register the same method twice on one hook', () => {
function hookMethod() {
count++
}
const plugin = {
name: 'test',
version: '0.1-test',
hooks: {
preDraft: [
hookMethod,
hookMethod,
function () {
count++
},
],
},
}
const Pattern = new Design()
const pattern = new Pattern()
let count = 0
pattern._draft = () => {}
pattern.use(plugin)
pattern.draft()
expect(count).to.equal(2)
})
it('Should check whether created parts get the pattern context', () => {
let partContext
const plugin = {

View file

@ -31,6 +31,7 @@ describe('Pattern', () => {
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
expect(pattern.settings.length).to.equal(10)
expect(pattern.parts[0].test.paths.test.ops[1].to.y).to.equal(80)
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(320)
})
@ -60,9 +61,70 @@ describe('Pattern', () => {
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
expect(pattern.settings.length).to.equal(10)
expect(round(pattern.parts[0].test.paths.test.ops[1].to.y)).to.equal(round(0.05 * 0.9 * 400))
expect(round(pattern.parts[9].test.paths.test.ops[1].to.y)).to.equal(22)
})
it('Should sample a count option', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: { count: 2, min: 0, max: 6 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
.move(new Point(0, 0))
.line(new Point(0, measurements.head * options.size))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
option: 'size',
},
})
pattern.sample()
expect(pattern.setStores.length).to.equal(7)
expect(pattern.settings.length).to.equal(7)
expect(round(pattern.parts[0].test.paths.test.ops[1].to.y)).to.equal(0)
expect(round(pattern.parts[6].test.paths.test.ops[1].to.y)).to.equal(2400)
})
it('Should not sample a count option more than 10 times', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
size: { count: 2, min: 0, max: 20 },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
paths.test = new Path()
.move(new Point(0, 0))
.line(new Point(0, measurements.head * options.size))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
option: 'size',
},
})
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
expect(pattern.settings.length).to.equal(10)
expect(round(pattern.parts[0].test.paths.test.ops[1].to.y)).to.equal(0)
expect(round(pattern.parts[9].test.paths.test.ops[1].to.y)).to.equal(8000)
})
it('Should sample a list option', () => {
const part = {
name: 'test',
@ -89,9 +151,39 @@ describe('Pattern', () => {
pattern.sample()
expect(pattern.setStores.length).to.equal(10)
expect(pattern.settings.length).to.equal(10)
expect(pattern.parts[0].test.paths.test.ops[1].to.y).to.equal(40)
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(400)
})
it('Should sample a boolean option', () => {
const part = {
name: 'test',
measurements: ['head'],
options: {
reverse: { bool: true },
},
draft: ({ Point, paths, Path, measurements, options, part }) => {
const yFac = options.reverse ? -1 : 1
paths.test = new Path().move(new Point(0, 0)).line(new Point(0, measurements.head * yFac))
return part
},
}
const Pattern = new Design({ parts: [part] })
const pattern = new Pattern({
measurements: { head: 400 },
sample: {
type: 'option',
option: 'reverse',
},
})
pattern.sample()
expect(pattern.setStores.length).to.equal(2)
expect(pattern.settings.length).to.equal(2)
expect(pattern.parts[0].test.paths.test.ops[1].to.y).to.equal(400)
expect(pattern.parts[1].test.paths.test.ops[1].to.y).to.equal(-400)
})
it('Should sample a measurement', () => {
const part = {
name: 'test',