chore(core): Unit tests for v3 code
This commit is contained in:
parent
d5eb2946d3
commit
bb89b6ca65
11 changed files with 526 additions and 111 deletions
|
@ -1,42 +1,191 @@
|
|||
import chai from 'chai'
|
||||
//import { round, Pattern, Design, pctBasedOn } from '../src/index.mjs'
|
||||
import { round, Pattern, Design, pctBasedOn } from '../src/index.mjs'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
describe('Pattern', () => {
|
||||
describe('Pattern.sample()', () => {
|
||||
it('FIXME: Write some tests here', () => {
|
||||
expect(true).to.equal(true)
|
||||
|
||||
it('Should sample an option', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { pct: 50, min: 20, max: 80 },
|
||||
},
|
||||
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.stores.length).to.equal(10)
|
||||
expect(pattern.settings.length).to.equal(10)
|
||||
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(320)
|
||||
})
|
||||
|
||||
it('Should sample a static option', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: 0.05,
|
||||
},
|
||||
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.stores.length).to.equal(10)
|
||||
expect(pattern.settings.length).to.equal(10)
|
||||
expect(round(pattern.parts[9].test.paths.test.ops[1].to.y)).to.equal(22)
|
||||
})
|
||||
|
||||
it('Should sample a list option', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { dflt: 5, list: [1, 2, 3, 4, 5 ,6 ,7, 8, 9, 10] },
|
||||
},
|
||||
draft: ({ Point, paths, Path, measurements, options, part }) => {
|
||||
paths.test = new Path()
|
||||
.move(new Point(0,0))
|
||||
.line(new Point(0, measurements.head * options.size/10))
|
||||
|
||||
return part
|
||||
},
|
||||
}
|
||||
const Pattern = new Design({ parts: [part] })
|
||||
const pattern = new Pattern({
|
||||
measurements: { head: 400 },
|
||||
sample: {
|
||||
type: 'option',
|
||||
option: 'size'
|
||||
}
|
||||
})
|
||||
pattern.sample()
|
||||
expect(pattern.stores.length).to.equal(10)
|
||||
expect(pattern.settings.length).to.equal(10)
|
||||
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(400)
|
||||
})
|
||||
|
||||
it('Should sample a measurement', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { pct: 50, min: 20, max: 80 },
|
||||
},
|
||||
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: 'measurement',
|
||||
measurement: 'head'
|
||||
}
|
||||
})
|
||||
pattern.sample()
|
||||
expect(pattern.stores.length).to.equal(10)
|
||||
expect(pattern.settings.length).to.equal(10)
|
||||
expect(pattern.parts[9].test.paths.test.ops[1].to.y).to.equal(216)
|
||||
})
|
||||
|
||||
it('Should log an error when sampling an undefined measurement', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { pct: 50, min: 20, max: 80 },
|
||||
},
|
||||
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: { },
|
||||
sample: {
|
||||
type: 'measurement',
|
||||
measurement: 'head'
|
||||
}
|
||||
})
|
||||
pattern.sample()
|
||||
expect(pattern.stores[0].logs.error.length).to.equal(1)
|
||||
expect(pattern.stores[0].logs.error[0]).to.equal("Cannot sample measurement `head` because it's `undefined`")
|
||||
})
|
||||
|
||||
it('Should sample models', () => {
|
||||
const part = {
|
||||
name: 'test',
|
||||
measurements: ['head'],
|
||||
options: {
|
||||
size: { pct: 50, min: 20, max: 80 },
|
||||
},
|
||||
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: 'models',
|
||||
models: {
|
||||
a: { head: 100 },
|
||||
b: { head: 200 },
|
||||
c: { head: 300 },
|
||||
d: { head: 400 },
|
||||
},
|
||||
focus: 'c'
|
||||
}
|
||||
})
|
||||
pattern.sample()
|
||||
expect(pattern.stores.length).to.equal(4)
|
||||
expect(pattern.settings.length).to.equal(4)
|
||||
expect(pattern.parts[3].test.paths.test.ops[1].to.y).to.equal(200)
|
||||
})
|
||||
|
||||
/*
|
||||
it('Should sample an option', () => {
|
||||
let pattern = new Pattern({
|
||||
options: {
|
||||
len: { pct: 30, min: 10 },
|
||||
bonus: 10,
|
||||
},
|
||||
})
|
||||
pattern.draft = function () {
|
||||
pattern.parts.a = new pattern.Part()
|
||||
pattern.parts.b = new pattern.Part()
|
||||
let a = pattern.parts.a
|
||||
a.points.from = new a.Point(0, 0)
|
||||
a.points.to = new a.Point(
|
||||
100 * a.context.settings.options.len,
|
||||
a.context.settings.options.bonus
|
||||
)
|
||||
a.paths.test = new a.Path().move(a.points.from).line(a.points.to)
|
||||
pattern.parts.b.inject(a)
|
||||
}
|
||||
pattern.settings.sample = {
|
||||
type: 'option',
|
||||
option: 'len',
|
||||
}
|
||||
pattern.sample()
|
||||
expect(pattern.parts.a.paths.test_1.render).to.equal(true)
|
||||
expect(pattern.parts.b.paths.test_10.ops[1].to.y).to.equal(10)
|
||||
})
|
||||
it("Should sample a list option", () => {
|
||||
const front = {
|
||||
name: 'front',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue