1
0
Fork 0

chore(core): Unit tests for v3 code

This commit is contained in:
Joost De Cock 2022-09-19 23:35:52 +02:00
parent d5eb2946d3
commit bb89b6ca65
11 changed files with 526 additions and 111 deletions

View file

@ -130,15 +130,8 @@ Pattern.prototype.draft = function () {
this.stores[set].log.error([`Unable to draft part \`${partName}\` (set ${set})`, err])
}
} else this.stores[set].log.error(`Unable to draft pattern. Part.draft() is not callable`)
try {
this.parts[set][partName].hidden =
this.parts[set][partName].hidden === true ? true : !this.__wants(partName, set)
} catch (err) {
this.stores[set].log.error([
`Unable to set \`hidden\` property on part \`${partName}\``,
err,
])
}
this.parts[set][partName].hidden =
this.parts[set][partName].hidden === true ? true : !this.__wants(partName, set)
} else {
this.stores[set].log.debug(
`Part \`${partName}\` is not needed. Skipping draft and setting hidden to \`true\``
@ -253,11 +246,12 @@ Pattern.prototype.init = function () {
* @return {object} this - The Pattern instance
*/
Pattern.prototype.sample = function () {
this.init()
if (this.settings[0].sample.type === 'option') {
return this.sampleOption(this.settings[0].sample.option)
} else if (this.settings[0].sample.type === 'measurement') {
return this.sampleMeasurement(this.settings[0].sample.measurement)
} else if (this.settings.sample.type === 'models') {
} else if (this.settings[0].sample.type === 'models') {
return this.sampleModels(this.settings[0].sample.models, this.settings[0].sample.focus || false)
}
}
@ -268,7 +262,7 @@ Pattern.prototype.sample = function () {
* @return {object} this - The Pattern instance
*/
Pattern.prototype.sampleMeasurement = function (measurementName) {
this.store.log.debug(`Sampling measurement \`${measurementName}\``)
this.stores[0].log.debug(`Sampling measurement \`${measurementName}\``)
this.__runHooks('preSample')
this.__applySettings(this.__measurementSets(measurementName))
this.init()
@ -283,7 +277,7 @@ Pattern.prototype.sampleMeasurement = function (measurementName) {
* @return {object} this - The Pattern instance
*/
Pattern.prototype.sampleModels = function (models, focus = false) {
this.store.log.debug(`Sampling models \`${Object.keys(models).join(', ')}\``)
this.stores[0].log.debug(`Sampling models \`${Object.keys(models).join(', ')}\``)
this.__runHooks('preSample')
this.__applySettings(this.__modelSets(models, focus))
this.init()
@ -345,15 +339,7 @@ Pattern.prototype.render = function () {
* @return {object} this - The Pattern instance
*/
Pattern.prototype.use = function (plugin, data) {
if (this.plugins?.[plugin.name]?.condition && !plugin.condition) {
// Plugin was first loaded conditionally, and is now loaded explicitly
this.stores[0].log.info(
`Plugin \`${plugin.plugin.name} was loaded conditionally earlier, but is now loaded explicitly.`
)
return this.__loadPlugin(plugin, data)
}
// New plugin
else if (!this.plugins?.[plugin.name])
if (!this.plugins?.[plugin.name])
return plugin.plugin && plugin.condition
? this.__useIf(plugin, data) // Conditional plugin
: this.__loadPlugin(plugin, data) // Regular plugin
@ -482,8 +468,8 @@ Pattern.prototype.__filterOptionalMeasurements = function () {
* @return {bool} hidden - true if the part is hidden, or false if not
*/
Pattern.prototype.__isPartHidden = function (partName) {
if (Array.isArray(this.settings.only)) {
if (this.settings.only.includes(partName)) return false
if (Array.isArray(this.settings[this.activeSet || 0].only)) {
if (this.settings[this.activeSet || 0].only.includes(partName)) return false
}
if (this.__parts?.[partName]?.hide) return true
if (this.__parts?.[partName]?.hideAll) return true
@ -501,9 +487,9 @@ Pattern.prototype.__isPartHidden = function (partName) {
Pattern.prototype.__isStackHidden = function (stackName) {
if (!this.stacks[stackName]) return true
const parts = this.stacks[stackName].getPartNames()
if (Array.isArray(this.settings.only)) {
if (Array.isArray(this.settings[this.activeStack || 0].only)) {
for (const partName of parts) {
if (this.settings.only.includes(partName)) return false
if (this.settings[this.activeStack || 0].only.includes(partName)) return false
}
}
for (const partName of parts) {
@ -694,7 +680,7 @@ Pattern.prototype.__loadPlugins = function () {
*/
Pattern.prototype.__loadPluginStoreMethods = function (plugin) {
if (Array.isArray(plugin.store)) {
for (const store of this.stores) store.extend(...plugin.store)
for (const store of this.stores) store.extend(plugin.store)
} else this.stores[0].log.warning(`Plugin store methods should be an Array`)
}
@ -722,7 +708,7 @@ Pattern.prototype.__macro = function (key, method) {
Pattern.prototype.__measurementSets = function (measurementName) {
let val = this.settings[0].measurements[measurementName]
if (val === undefined)
this.stores.log.error(
this.stores[0].log.error(
`Cannot sample measurement \`${measurementName}\` because it's \`undefined\``
)
let step = val / 50
@ -826,7 +812,7 @@ Pattern.prototype.__needs = function (partName, set = 0) {
*/
Pattern.prototype.__optionSets = function (optionName) {
let option = this.config.options[optionName]
if (typeof option.list === 'object') return this.__listOptionSets(optionName)
if (typeof option?.list === 'object') return this.__listOptionSets(optionName)
const sets = []
let factor = 1
let step, val
@ -917,7 +903,7 @@ Pattern.prototype.__pack = function () {
for (let stackId of Object.keys(this.settings[0].layout.stacks)) {
// Some parts are added by late-stage plugins
if (this.stacks[stackId]) {
let transforms = this.settings.layout.stacks[stackId]
let transforms = this.settings[this.activeStack || 0].layout.stacks[stackId]
this.stacks[stackId].generateTransform(transforms)
}
}

View file

@ -117,31 +117,11 @@ Stack.prototype.attr = function (name, value, overwrite = false) {
/** Generates the transform for a stack */
Stack.prototype.generateTransform = function (transforms) {
const { move, rotate, flipX, flipY } = transforms
const generated = utils.generateStackTransform(move.x, move.y, rotate, flipX, flipY, this)
const generated = utils.generateStackTransform( move?.x, move?.y, rotate, flipX, flipY, this)
for (var t in generated) {
this.attr(t, generated[t], true)
}
}
/** Homes the stack so that its top left corner is in (0,0) */
//Stack.prototype.home = function () {
// const parts = this.getPartList()
// if (parts.length < 1) return this
// for (const part of this.getPartList()) {
// part.home()
// }
//
// if (parts.length === 1) {
// this.topLeft = part.topLeft
// this.bottomRigth = part.bottomRight
// this.width = part.width
// this.height = part.height
//
// return this
// }
//
// return this.boundary()
//}
export default Stack

View file

@ -43,10 +43,10 @@ export function Store(methods = []) {
}
this.logs = logs
for (const method of methods) {
if (avoid.indexOf(method[0]) !== -1) {
this.logs.warning(`You cannot squat ${method[0]} in the store`)
} else set(this, ...method)
for (const [path, method] of methods) {
if (avoid.indexOf(path) !== -1) {
this.log.warning(`You cannot overwrite store.${path}()`)
} else set(this, path, method)
}
return this
@ -62,10 +62,10 @@ export function Store(methods = []) {
* @param {function} method - Method to add to the store (variadic)
* @return {Store} this - The Store instance
*/
Store.prototype.extend = function (...methods) {
Store.prototype.extend = function (methods) {
for (const [path, method] of methods) {
if (avoid.indexOf(method[0]) !== -1) {
this.log.warning(`You can't squat ${method[0]}in the store`)
if (avoid.indexOf(path) !== -1) {
this.log.warning(`You cannot overwrite store.${path}()`)
} else {
this.log.info(`Extending store with ${path}`)
set(this, path, (...args) => method(this, ...args))

View file

@ -60,7 +60,7 @@ Svg.prototype.render = function () {
this.activeStackIndex = 0
for (let stackId in this.pattern.stacks) {
this.activeStack = stackId
this.idPrefix = this.pattern.settings[this.activeStackIndex].idPrefix
this.idPrefix = this.pattern.settings[this.activeStackIndex]?.idPrefix || 'fs-'
const stack = this.pattern.stacks[stackId]
if (!stack.hidden) {
const stackSvg = this.__renderStack(stack)

View file

@ -288,7 +288,7 @@ export function deg2rad(degrees) {
* @param {Stack} stack - The Stack instance
* @return {string} transform - The SVG transform value
*/
export const generateStackTransform = (x, y, rotate, flipX, flipY, stack) => {
export const generateStackTransform = (x=0, y=0, rotate=0, flipX=false, flipY=false, stack) => {
const transforms = []
let xTotal = x || 0
let yTotal = y || 0