1
0
Fork 0
freesewing/tests/designs/config.mjs

190 lines
7.7 KiB
JavaScript
Raw Normal View History

2022-08-25 11:52:34 +02:00
import { measurements } from '@freesewing/models'
import designs from '../../config/software/designs.json' assert { type: 'json' }
2022-08-25 11:52:34 +02:00
import chai from 'chai'
const expect = chai.expect
export const getShortName = (name) => name.split('/').pop()
export const isUtilityDesign = (name) => typeof designs[name].tags === 'undefined'
2022-09-07 10:11:19 +02:00
// These are ok to use mm options
const mmAllowed = ['rendertest']
2020-03-14 15:04:45 +01:00
/*
* This runs unit tests for the pattern configuration
* It expects the following:
*
* @param string Pattern: The Pattern constructor
2020-03-14 15:04:45 +01:00
*/
export const testPatternConfig = (Pattern) => {
2022-09-25 10:46:50 +02:00
//const pattern = new Pattern()
const designConfig = Pattern.designConfig
const patternConfig = Pattern.patternConfig
it('Pattern data:', () => true)
it(` - 'name' should be set and be a non-empty string`, () => {
2022-09-25 10:46:50 +02:00
expect(typeof designConfig.data.name).to.equal('string')
expect(designConfig.data.name.length > 1).to.be.true
2020-03-14 15:04:45 +01:00
})
//
2020-03-14 15:04:45 +01:00
it(` - 'version' should be set and be a non-empty string`, () => {
2022-09-25 10:46:50 +02:00
expect(typeof designConfig.data.version).to.equal('string')
expect(designConfig.data.version.length > 1).to.be.true
2020-03-14 15:04:45 +01:00
})
it(` - 'version' should be a proper semantic version`, () => {
2022-09-25 10:46:50 +02:00
const chunks = designConfig.data.version.split('.')
if (chunks.length > 3) {
2022-09-25 10:46:50 +02:00
expect(designConfig.data.version.split('.').length).to.equal(4)
expect(chunks[2]).to.contain.oneOf(['-alpha', '-beta', '-rc'])
} else expect(designConfig.version.split('.').length).to.equal(3)
})
it('Monorepo data:', () => true)
// Store these for re-use
2022-09-25 10:46:50 +02:00
const name = getShortName(designConfig.data.name)
it(` - 'name' should be resolvable to a short name`, () => {
expect(typeof name).to.equal('string')
expect(name.length > 1).to.be.true
})
const meta = designs[name]
it(` - 'description' should be set and be a string of reasonable length`, () => {
expect(typeof meta.description).to.equal('string')
expect(meta.description.length > 15).to.be.true
expect(meta.description.length < 280).to.be.true
})
// Config tests for non-utility patterns only
if (typeof designs[name].tags !== 'undefined') {
it(` - 'design' should be set and be a string of reasonable length`, () => {
const people = Array.isArray(meta.design) ? meta.design : [meta.design]
2022-09-04 19:03:18 +02:00
for (const person of people) {
expect(typeof person).to.equal('string')
expect(person.length > 2).to.be.true
expect(person.length < 80).to.be.true
2022-08-29 08:31:23 +02:00
}
})
it(` - 'code' should be set and be a string of reasonable length`, () => {
const people = Array.isArray(meta.code) ? meta.code : [meta.code]
2022-09-04 19:03:18 +02:00
for (const person of people) {
expect(typeof person).to.equal('string')
expect(person.length > 2).to.be.true
expect(person.length < 80).to.be.true
}
})
it(` - 'dfficulty' should be set and be a [1-5] number`, () => {
expect(typeof meta.difficulty).to.equal('number')
expect([1, 2, 3, 4, 5].indexOf(meta.difficulty) === -1).to.be.false
})
}
2020-03-14 15:04:45 +01:00
if (!isUtilityDesign(name)) {
// Ensure required measurements are known measurements
it('Required measurements:', () => true)
2022-09-25 10:46:50 +02:00
for (const measurement of patternConfig.measurements || []) {
2020-03-15 10:43:11 +01:00
it(` - '${measurement}' should be a known measurement`, () => {
expect(measurements.indexOf(measurement)).to.not.equal(-1)
2020-03-15 10:43:11 +01:00
})
}
it('Optional measurements:', () => true)
2022-09-25 10:46:50 +02:00
for (let measurement of patternConfig.optionalMeasurements || []) {
it(` - '${measurement}' should be a known measurement`, () => {
expect(measurements.indexOf(measurement)).to.not.equal(-1)
})
}
2020-03-14 15:04:45 +01:00
}
// Test validity of the pattern's options
it('Pattern options:', () => true)
2022-09-25 10:46:50 +02:00
for (const name in patternConfig.options) {
const option = patternConfig.options[name]
const type = typeof option
2020-03-14 15:04:45 +01:00
if (type === 'object' && typeof option.pct !== 'undefined') {
2022-09-07 10:11:19 +02:00
it(` - If it has a 'menu' property, it should be a string or method`, () => {
if (option.menu)
expect(['string', 'function'].indexOf(typeof option.menu) === -1).to.equal(false)
})
2020-03-14 15:04:45 +01:00
// Percentage option
it(` - '${name}' is a percentage option`, () => true)
// Snapped options can just be hidden instead
if (option.hidden) {
if (option.snap) it(` - '${name}' is a hidden snap option`, () => true)
}
2020-03-14 15:04:45 +01:00
it(` - Should have a default value`, () => {
expect(typeof option.pct).to.equal('number')
})
it(` - Should have a minimum <= the default value`, () => {
expect(option.min <= option.pct).to.be.true
})
it(` - Should have a maximum >= the default value`, () => {
expect(option.max >= option.pct).to.be.true
})
} else if (type === 'object' && typeof option.deg !== 'undefined') {
2020-03-14 15:04:45 +01:00
// Degree option
it(` - '${name}' is a degree option`, () => true)
it(` - Should have a default value`, () => {
expect(typeof option.deg).to.equal('number')
})
it(` - Should have a minimum <= the default value`, () => {
expect(option.min <= option.deg).to.be.true
})
it(` - Should have a maximum >= the default value`, () => {
expect(option.max >= option.deg).to.be.true
})
} else if (type === 'object' && typeof option.mm !== 'undefined') {
2020-03-14 15:04:45 +01:00
// Millimeter option
it(` - '${name}' is a distance (mm) option`, () => true)
it(` - Should have a default value`, () => {
expect(typeof option.mm).to.equal('number')
})
it(` - Should have a minimum <= the default value`, () => {
expect(option.min <= option.mm).to.be.true
})
it(` - Should have a maximum >= the default value`, () => {
expect(option.max >= option.mm).to.be.true
})
2022-09-25 10:46:50 +02:00
if (mmAllowed.indexOf(getShortName(designConfig.data.name)) === -1) {
2022-09-07 10:11:19 +02:00
it(` - Patterns should not use mm options`, () => {
expect('Does not use mm').to.be.true
2022-09-07 10:11:19 +02:00
})
}
} else if (type === 'object' && typeof option.bool !== 'undefined') {
2020-03-14 15:04:45 +01:00
// Boolean option
it(` - '${name}' is a boolean option`, () => true)
it(` - Should have a default value`, () => {
expect(typeof option.bool).to.equal('boolean')
})
it(` - Default value should be one of TRUE or FALSE`, () => {
expect([true, false].indexOf(option.bool)).to.not.equal(-1)
})
} else if (type === 'object' && typeof option.count !== 'undefined') {
2020-03-14 15:04:45 +01:00
// Count option
it(` - '${name}' is a count option`, () => true)
it(` - Should have a default value`, () => {
expect(typeof option.count).to.equal('number')
})
it(` - Should have a minimum <= the default value`, () => {
expect(option.min <= option.count).to.be.true
})
it(` - Should have a maximum >= the default value`, () => {
expect(option.max >= option.count).to.be.true
})
} else if (type === 'object' && typeof option.list !== 'undefined') {
2020-03-14 15:04:45 +01:00
// List option
it(` - '${name}' is a list option`, () => true)
it(` - Should have a default value`, () => {
expect(typeof option.dflt).to.not.equal('undefined')
})
it(` - Its default value should be in the list of options`, () => {
expect(option.list.indexOf(option.dflt)).to.not.equal(-1)
})
it(` - Its options should be an array of numbers or strings`, () => {
for (let o of option.list) expect(['number', 'string'].indexOf(typeof o)).to.not.equal(-1)
})
} else if (type === 'number') {
2020-03-14 15:04:45 +01:00
// Static number
it(` - '${name}' is a static number`, () => true)
} else if (type === 'string') {
2020-03-14 15:04:45 +01:00
// Static string
it(` - '${name}' is a static string`, () => true)
2020-03-14 15:04:45 +01:00
}
}
}