wip(core): Work on part unit tests
This commit is contained in:
parent
904e0044c5
commit
cf0c70f4c6
4 changed files with 165 additions and 320 deletions
|
@ -6,24 +6,25 @@ import { Snippet } from './snippet.mjs'
|
||||||
import { Hooks } from './hooks.mjs'
|
import { Hooks } from './hooks.mjs'
|
||||||
|
|
||||||
export function Part() {
|
export function Part() {
|
||||||
|
// Non-enumerable properties
|
||||||
|
utils.addNonEnumProp(this, 'freeId', 0)
|
||||||
|
utils.addNonEnumProp(this, 'topLeft', false)
|
||||||
|
utils.addNonEnumProp(this, 'bottomRight', false)
|
||||||
|
utils.addNonEnumProp(this, 'width', false)
|
||||||
|
utils.addNonEnumProp(this, 'height', false)
|
||||||
|
utils.addNonEnumProp(this, 'utils', utils)
|
||||||
|
utils.addNonEnumProp(this, 'layout', { move: { x: 0, y: 0 } })
|
||||||
|
utils.addNonEnumProp(this, 'Point', Point)
|
||||||
|
utils.addNonEnumProp(this, 'Path', Path)
|
||||||
|
utils.addNonEnumProp(this, 'Snippet', Snippet)
|
||||||
|
utils.addNonEnumProp(this, 'hooks', new Hooks())
|
||||||
|
|
||||||
|
// Enumerable properties
|
||||||
|
this.render = true // FIXME: Replace render with hide
|
||||||
this.attributes = new Attributes()
|
this.attributes = new Attributes()
|
||||||
this.points = {}
|
this.points = {}
|
||||||
this.paths = {}
|
this.paths = {}
|
||||||
this.snippets = {}
|
this.snippets = {}
|
||||||
this.freeId = 0
|
|
||||||
this.topLeft = false
|
|
||||||
this.bottomRight = false
|
|
||||||
this.width = false
|
|
||||||
this.height = false
|
|
||||||
this.render = true
|
|
||||||
this.utils = utils
|
|
||||||
this.layout = { move: { x: 0, y: 0 } }
|
|
||||||
this.cut = { grain: 90, materials: {} }
|
|
||||||
this.Point = Point
|
|
||||||
this.Path = Path
|
|
||||||
this.Snippet = Snippet
|
|
||||||
|
|
||||||
this.hooks = new Hooks() // Hooks container
|
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -59,8 +60,8 @@ Part.prototype.getId = function (prefix = '') {
|
||||||
Part.prototype.unitsClosure = function (value) {
|
Part.prototype.unitsClosure = function (value) {
|
||||||
const self = this
|
const self = this
|
||||||
const method = function (value) {
|
const method = function (value) {
|
||||||
if (self.context.settings.debug && typeof value !== 'number')
|
if (typeof value !== 'number')
|
||||||
self.context.raise.debug(
|
self.context.store.log.warning(
|
||||||
`Calling \`units(value)\` but \`value\` is not a number (\`${typeof value}\`)`
|
`Calling \`units(value)\` but \`value\` is not a number (\`${typeof value}\`)`
|
||||||
)
|
)
|
||||||
return utils.units(value, self.context.settings.units)
|
return utils.units(value, self.context.settings.units)
|
||||||
|
@ -85,8 +86,8 @@ Part.prototype.boundary = function () {
|
||||||
if (path.bottomRight.y > bottomRight.y) bottomRight.y = path.bottomRight.y
|
if (path.bottomRight.y > bottomRight.y) bottomRight.y = path.bottomRight.y
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.context.raise.error(`Could not calculate boundary of \`paths.${key}\``)
|
this.context.store.log.error(`Could not calculate boundary of \`paths.${key}\``)
|
||||||
this.context.raise.debug(
|
this.context.store.log.debug(
|
||||||
`Since \`paths.${key}\` has no boundary, neither does \`parts.${this.name}\`. Ejecting part`
|
`Since \`paths.${key}\` has no boundary, neither does \`parts.${this.name}\`. Ejecting part`
|
||||||
)
|
)
|
||||||
return false
|
return false
|
||||||
|
@ -180,12 +181,12 @@ Part.prototype.units = function (input) {
|
||||||
|
|
||||||
/** Returns an object with shorthand access for pattern design */
|
/** Returns an object with shorthand access for pattern design */
|
||||||
Part.prototype.shorthand = function () {
|
Part.prototype.shorthand = function () {
|
||||||
const complete = this.context.settings.complete ? true : false
|
const complete = this.context.settings?.complete ? true : false
|
||||||
const paperless = this.context.settings.paperless === true ? true : false
|
const paperless = this.context.settings?.paperless === true ? true : false
|
||||||
const sa = this.context.settings.complete ? this.context.settings.sa || 0 : 0
|
const sa = this.context.settings?.complete ? this.context.settings?.sa || 0 : 0
|
||||||
const shorthand = {
|
const shorthand = {
|
||||||
sa,
|
sa,
|
||||||
scale: this.context.settings.scale,
|
scale: this.context.settings?.scale,
|
||||||
store: this.context.store,
|
store: this.context.store,
|
||||||
macro: this.macroClosure(),
|
macro: this.macroClosure(),
|
||||||
units: this.unitsClosure(),
|
units: this.unitsClosure(),
|
||||||
|
@ -193,7 +194,7 @@ Part.prototype.shorthand = function () {
|
||||||
complete,
|
complete,
|
||||||
paperless,
|
paperless,
|
||||||
events: this.context.events,
|
events: this.context.events,
|
||||||
raise: this.context.raise,
|
log: this.context.store.log,
|
||||||
addCut: this.addCut,
|
addCut: this.addCut,
|
||||||
removeCut: this.removeCut,
|
removeCut: this.removeCut,
|
||||||
}
|
}
|
||||||
|
@ -209,23 +210,23 @@ Part.prototype.shorthand = function () {
|
||||||
// We'll need this
|
// We'll need this
|
||||||
let self = this
|
let self = this
|
||||||
|
|
||||||
// Wrap the Point constructor so objects can raise events
|
// Wrap the Point constructor so objects can log
|
||||||
shorthand.Point = function (x, y) {
|
shorthand.Point = function (x, y) {
|
||||||
Point.apply(this, [x, y, true])
|
Point.apply(this, [x, y, true])
|
||||||
Object.defineProperty(this, 'raise', { value: self.context.raise })
|
Object.defineProperty(this, 'log', { value: self.context.store.log })
|
||||||
}
|
}
|
||||||
shorthand.Point.prototype = Object.create(Point.prototype)
|
shorthand.Point.prototype = Object.create(Point.prototype)
|
||||||
// Wrap the Path constructor so objects can raise events
|
// Wrap the Path constructor so objects can log
|
||||||
shorthand.Path = function () {
|
shorthand.Path = function () {
|
||||||
Path.apply(this, [true])
|
Path.apply(this, [true])
|
||||||
Object.defineProperty(this, 'raise', { value: self.context.raise })
|
Object.defineProperty(this, 'log', { value: self.context.store.log })
|
||||||
}
|
}
|
||||||
shorthand.Path.prototype = Object.create(Path.prototype)
|
shorthand.Path.prototype = Object.create(Path.prototype)
|
||||||
// Wrap the Snippet constructor so objects can raise events
|
// Wrap the Snippet constructor so objects can log
|
||||||
shorthand.Snippet = function (def, anchor) {
|
shorthand.Snippet = function (def, anchor) {
|
||||||
Snippet.apply(this, [def, anchor, true])
|
Snippet.apply(this, [def, anchor, true])
|
||||||
Snippet.apply(this, arguments)
|
Snippet.apply(this, arguments)
|
||||||
Object.defineProperty(this, 'raise', { value: self.context.raise })
|
Object.defineProperty(this, 'log', { value: self.context.store.log })
|
||||||
}
|
}
|
||||||
shorthand.Snippet.prototype = Object.create(Snippet.prototype)
|
shorthand.Snippet.prototype = Object.create(Snippet.prototype)
|
||||||
|
|
||||||
|
@ -237,21 +238,21 @@ Part.prototype.shorthand = function () {
|
||||||
set: (points, name, value) => {
|
set: (points, name, value) => {
|
||||||
// Constructor checks
|
// Constructor checks
|
||||||
if (value instanceof Point !== true)
|
if (value instanceof Point !== true)
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`points.${name}\` was set with a value that is not a \`Point\` object`
|
`\`points.${name}\` was set with a value that is not a \`Point\` object`
|
||||||
)
|
)
|
||||||
if (value.x == null || !utils.isCoord(value.x))
|
if (value.x == null || !utils.isCoord(value.x))
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`points.${name}\` was set with a \`x\` parameter that is not a \`number\``
|
`\`points.${name}\` was set with a \`x\` parameter that is not a \`number\``
|
||||||
)
|
)
|
||||||
if (value.y == null || !utils.isCoord(value.y))
|
if (value.y == null || !utils.isCoord(value.y))
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`points.${name}\` was set with a \`y\` parameter that is not a \`number\``
|
`\`points.${name}\` was set with a \`y\` parameter that is not a \`number\``
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
value.name = name
|
value.name = name
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
self.context.raise.warning(`Could not set \`name\` property on \`points.${name}\``)
|
self.context.store.log.warning(`Could not set \`name\` property on \`points.${name}\``)
|
||||||
}
|
}
|
||||||
return (self.points[name] = value)
|
return (self.points[name] = value)
|
||||||
},
|
},
|
||||||
|
@ -265,13 +266,13 @@ Part.prototype.shorthand = function () {
|
||||||
set: (paths, name, value) => {
|
set: (paths, name, value) => {
|
||||||
// Constructor checks
|
// Constructor checks
|
||||||
if (value instanceof Path !== true)
|
if (value instanceof Path !== true)
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`paths.${name}\` was set with a value that is not a \`Path\` object`
|
`\`paths.${name}\` was set with a value that is not a \`Path\` object`
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
value.name = name
|
value.name = name
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
self.context.raise.warning(`Could not set \`name\` property on \`paths.${name}\``)
|
self.context.store.log.warning(`Could not set \`name\` property on \`paths.${name}\``)
|
||||||
}
|
}
|
||||||
return (self.paths[name] = value)
|
return (self.paths[name] = value)
|
||||||
},
|
},
|
||||||
|
@ -285,21 +286,21 @@ Part.prototype.shorthand = function () {
|
||||||
set: (snippets, name, value) => {
|
set: (snippets, name, value) => {
|
||||||
// Constructor checks
|
// Constructor checks
|
||||||
if (value instanceof Snippet !== true)
|
if (value instanceof Snippet !== true)
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`snippets.${name}\` was set with a value that is not a \`Snippet\` object`
|
`\`snippets.${name}\` was set with a value that is not a \`Snippet\` object`
|
||||||
)
|
)
|
||||||
if (typeof value.def !== 'string')
|
if (typeof value.def !== 'string')
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`snippets.${name}\` was set with a \`def\` parameter that is not a \`string\``
|
`\`snippets.${name}\` was set with a \`def\` parameter that is not a \`string\``
|
||||||
)
|
)
|
||||||
if (value.anchor instanceof Point !== true)
|
if (value.anchor instanceof Point !== true)
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`\`snippets.${name}\` was set with an \`anchor\` parameter that is not a \`Point\``
|
`\`snippets.${name}\` was set with an \`anchor\` parameter that is not a \`Point\``
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
value.name = name
|
value.name = name
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
self.context.raise.warning(`Could not set \`name\` property on \`snippets.${name}\``)
|
self.context.store.log.warning(`Could not set \`name\` property on \`snippets.${name}\``)
|
||||||
}
|
}
|
||||||
return (self.snippets[name] = value)
|
return (self.snippets[name] = value)
|
||||||
},
|
},
|
||||||
|
@ -309,7 +310,7 @@ Part.prototype.shorthand = function () {
|
||||||
const measurementsProxy = {
|
const measurementsProxy = {
|
||||||
get: function (measurements, name) {
|
get: function (measurements, name) {
|
||||||
if (typeof measurements[name] === 'undefined')
|
if (typeof measurements[name] === 'undefined')
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`Tried to access \`measurements.${name}\` but it is \`undefined\``
|
`Tried to access \`measurements.${name}\` but it is \`undefined\``
|
||||||
)
|
)
|
||||||
return Reflect.get(...arguments)
|
return Reflect.get(...arguments)
|
||||||
|
@ -321,7 +322,7 @@ Part.prototype.shorthand = function () {
|
||||||
const optionsProxy = {
|
const optionsProxy = {
|
||||||
get: function (options, name) {
|
get: function (options, name) {
|
||||||
if (typeof options[name] === 'undefined')
|
if (typeof options[name] === 'undefined')
|
||||||
self.context.raise.warning(`Tried to access \`options.${name}\` but it is \`undefined\``)
|
self.context.store.log.warning(`Tried to access \`options.${name}\` but it is \`undefined\``)
|
||||||
return Reflect.get(...arguments)
|
return Reflect.get(...arguments)
|
||||||
},
|
},
|
||||||
set: (options, name, value) => (self.context.settings.options[name] = value),
|
set: (options, name, value) => (self.context.settings.options[name] = value),
|
||||||
|
@ -331,7 +332,7 @@ Part.prototype.shorthand = function () {
|
||||||
const absoluteOptionsProxy = {
|
const absoluteOptionsProxy = {
|
||||||
get: function (absoluteOptions, name) {
|
get: function (absoluteOptions, name) {
|
||||||
if (typeof absoluteOptions[name] === 'undefined')
|
if (typeof absoluteOptions[name] === 'undefined')
|
||||||
self.context.raise.warning(
|
self.context.store.log.warning(
|
||||||
`Tried to access \`absoluteOptions.${name}\` but it is \`undefined\``
|
`Tried to access \`absoluteOptions.${name}\` but it is \`undefined\``
|
||||||
)
|
)
|
||||||
return Reflect.get(...arguments)
|
return Reflect.get(...arguments)
|
||||||
|
|
|
@ -1,145 +0,0 @@
|
||||||
import chai from 'chai'
|
|
||||||
import { Design } from '../src/index.mjs'
|
|
||||||
import { pluginBundle } from '@freesewing/plugin-bundle'
|
|
||||||
|
|
||||||
const expect = chai.expect
|
|
||||||
|
|
||||||
const fabricLines = {
|
|
||||||
name: 'legend.fabricLines',
|
|
||||||
plugins: pluginBundle,
|
|
||||||
draft: legendFabricLines,
|
|
||||||
}
|
|
||||||
const lineStrokes = {
|
|
||||||
name: 'legend.lineStrokes',
|
|
||||||
plugins: pluginBundle,
|
|
||||||
//after: fabricLines,
|
|
||||||
draft: legendLineStrokes,
|
|
||||||
}
|
|
||||||
const lineWidths = {
|
|
||||||
name: 'legend.lineWidths',
|
|
||||||
plugins: pluginBundle,
|
|
||||||
//from: lineStrokes,
|
|
||||||
draft: legendLineWidths,
|
|
||||||
options: {
|
|
||||||
waistEase: { pct: 12, min: 0, max: 100 },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
const saLines = {
|
|
||||||
name: 'legend.saLines',
|
|
||||||
plugins: pluginBundle,
|
|
||||||
draft: legendSaLines,
|
|
||||||
}
|
|
||||||
const otherLines = {
|
|
||||||
name: 'legend.otherLines',
|
|
||||||
plugins: pluginBundle,
|
|
||||||
draft: legendOtherLines,
|
|
||||||
}
|
|
||||||
|
|
||||||
const parts = [otherLines, saLines, fabricLines, lineStrokes, lineWidths]
|
|
||||||
|
|
||||||
describe('Design', () => {
|
|
||||||
it('FIXME', () => {
|
|
||||||
const Pattern = new Design({
|
|
||||||
data: { foo: 'bar' },
|
|
||||||
parts,
|
|
||||||
})
|
|
||||||
|
|
||||||
const pattern = new Pattern({
|
|
||||||
paperless: true,
|
|
||||||
})
|
|
||||||
pattern.init()
|
|
||||||
//console.log('plugins:', pattern.config.plugins)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
const allFabricTypes = ['fabric', 'lining', 'canvas', 'interfacing', 'various']
|
|
||||||
const allLineTypes = ['note', 'mark', 'contrast', 'help']
|
|
||||||
const allLineStrokes = ['dotted', 'dashed', 'lashed']
|
|
||||||
const allLineWidths = ['stroke-xs', 'stroke-sm', 'default', 'stroke-lg', 'stroke-xl', 'stroke-xxl']
|
|
||||||
|
|
||||||
function legendFabricLines(part) {
|
|
||||||
let y = 10
|
|
||||||
for (const t of allFabricTypes) {
|
|
||||||
drawLine(part, y, t)
|
|
||||||
y += 15
|
|
||||||
}
|
|
||||||
|
|
||||||
return box(part, 120, 65)
|
|
||||||
}
|
|
||||||
|
|
||||||
function legendLineStrokes(part) {
|
|
||||||
let y = 10
|
|
||||||
for (const t of allLineStrokes) {
|
|
||||||
drawLine(part, y, t)
|
|
||||||
y += 15
|
|
||||||
}
|
|
||||||
|
|
||||||
return box(part, 120, 50)
|
|
||||||
}
|
|
||||||
|
|
||||||
function legendLineWidths(part) {
|
|
||||||
let y = 10
|
|
||||||
for (const t of allLineWidths) {
|
|
||||||
drawLine(part, y, t)
|
|
||||||
y += 15
|
|
||||||
}
|
|
||||||
|
|
||||||
return box(part, 120, 95)
|
|
||||||
}
|
|
||||||
|
|
||||||
function legendSaLines(part) {
|
|
||||||
let y = 10
|
|
||||||
for (const t of allFabricTypes) {
|
|
||||||
drawLine(part, y, t + ' sa')
|
|
||||||
y += 15
|
|
||||||
}
|
|
||||||
|
|
||||||
return box(part, 120, 65)
|
|
||||||
}
|
|
||||||
|
|
||||||
function legendOtherLines(part) {
|
|
||||||
const { points, Point, paths, Path } = part.shorthand()
|
|
||||||
|
|
||||||
const drawLine = (y, t) => {
|
|
||||||
points[`${t}From`] = new Point(10, y)
|
|
||||||
points[`${t}To`] = new Point(w, y)
|
|
||||||
paths[t] = new Path()
|
|
||||||
.move(points[`${t}From`])
|
|
||||||
.line(points[`${t}To`])
|
|
||||||
.attr('class', t)
|
|
||||||
.attr('data-text', t)
|
|
||||||
.attr('data-text-class', 'center')
|
|
||||||
}
|
|
||||||
|
|
||||||
let y = 10
|
|
||||||
let w = 110
|
|
||||||
for (const t of allLineTypes) {
|
|
||||||
drawLine(y, t)
|
|
||||||
y += 15
|
|
||||||
}
|
|
||||||
|
|
||||||
return box(part, 120, 65)
|
|
||||||
}
|
|
||||||
|
|
||||||
function box(part, w = 100, h = 50) {
|
|
||||||
part.paths.box = new part.Path()
|
|
||||||
.move(new part.Point(0, 0))
|
|
||||||
.line(new part.Point(w, h))
|
|
||||||
.attr('class', 'hidden')
|
|
||||||
|
|
||||||
return part
|
|
||||||
}
|
|
||||||
|
|
||||||
function drawLine(part, y, t) {
|
|
||||||
let { points, Point, paths, Path } = part.shorthand()
|
|
||||||
points[`${t}From`] = new Point(10, y)
|
|
||||||
points[`${t}To`] = new Point(110, y)
|
|
||||||
paths[t] = new Path()
|
|
||||||
.move(points[`${t}From`])
|
|
||||||
.line(points[`${t}To`])
|
|
||||||
.attr('class', t)
|
|
||||||
.attr('data-text', t)
|
|
||||||
.attr('data-text-class', 'center')
|
|
||||||
|
|
||||||
return part
|
|
||||||
}
|
|
|
@ -1,38 +1,26 @@
|
||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
import { Pattern, Path } from '../src/index.mjs'
|
import { Design, Pattern, Path } from '../src/index.mjs'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
describe('Part', () => {
|
describe('Part', () => {
|
||||||
it('Svg constructor should initialize object', () => {
|
|
||||||
let pattern = new Pattern()
|
|
||||||
let part = new pattern.Part()
|
|
||||||
expect(part.paths).to.eql({})
|
|
||||||
expect(part.snippets).to.eql({})
|
|
||||||
expect(part.freeId).to.equal(0)
|
|
||||||
expect(part.topLeft).to.equal(false)
|
|
||||||
expect(part.bottomRight).to.equal(false)
|
|
||||||
expect(part.width).to.equal(false)
|
|
||||||
expect(part.height).to.equal(false)
|
|
||||||
expect(part.render).to.equal(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('Should return a function from macroClosure', () => {
|
it('Should return a function from macroClosure', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
expect(typeof part.macroClosure()).to.equal('function')
|
expect(typeof part.macroClosure()).to.equal('function')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not run an unknown macro', () => {
|
it('Should not run an unknown macro', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
let macro = part.macroClosure()
|
const macro = part.macroClosure()
|
||||||
expect(macro('unknown')).to.equal(undefined)
|
expect(macro('unknown')).to.equal(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should register and run a macro', () => {
|
it('Should register and run a macro', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let plugin = {
|
const plugin = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
version: '0.1-test',
|
version: '0.1-test',
|
||||||
macros: {
|
macros: {
|
||||||
|
@ -43,37 +31,38 @@ describe('Part', () => {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
pattern.use(plugin)
|
pattern.use(plugin)
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
let macro = part.macroClosure()
|
const macro = part.macroClosure()
|
||||||
macro('test', { x: 123, y: 456 })
|
macro('test', { x: 123, y: 456 })
|
||||||
expect(part.points.macro.x).to.equal(123)
|
expect(part.points.macro.x).to.equal(123)
|
||||||
expect(part.points.macro.y).to.equal(456)
|
expect(part.points.macro.y).to.equal(456)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return a free ID', () => {
|
it('Should return a free ID', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
let free = part.getId()
|
const free = part.getId()
|
||||||
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
expect(part.getId()).to.equal('' + (parseInt(free) + 1))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return a function from unitsClosure', () => {
|
it('Should return a function from unitsClosure', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
expect(typeof part.unitsClosure()).to.equal('function')
|
expect(typeof part.unitsClosure()).to.equal('function')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should convert units', () => {
|
it('Should convert units', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
let units = part.unitsClosure()
|
const part = pattern.__createPartWithContext()
|
||||||
|
const units = part.unitsClosure()
|
||||||
expect(units(123.456)).to.equal('12.35cm')
|
expect(units(123.456)).to.equal('12.35cm')
|
||||||
expect(part.units(123.456)).to.equal('12.35cm')
|
expect(part.units(123.456)).to.equal('12.35cm')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should set part attributes', () => {
|
it('Should set part attributes', () => {
|
||||||
let pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
part.attr('foo', 'bar')
|
part.attr('foo', 'bar')
|
||||||
expect(part.attributes.get('foo')).to.equal('bar')
|
expect(part.attributes.get('foo')).to.equal('bar')
|
||||||
part.attr('foo', 'baz')
|
part.attr('foo', 'baz')
|
||||||
|
@ -82,83 +71,50 @@ describe('Part', () => {
|
||||||
expect(part.attributes.get('foo')).to.equal('schmoo')
|
expect(part.attributes.get('foo')).to.equal('schmoo')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should inject a part', () => {
|
|
||||||
let pattern = new Pattern()
|
|
||||||
let part = new pattern.Part()
|
|
||||||
part.points.a = new part.Point(12, 23)
|
|
||||||
part.points.b = new part.Point(10, 10)
|
|
||||||
part.points.c = new part.Point(20, 20)
|
|
||||||
part.paths.bar = new Path()
|
|
||||||
.move(part.points.a)
|
|
||||||
.line(part.points.b)
|
|
||||||
.curve(part.points.c, part.points.b, part.points.a)
|
|
||||||
const { Snippet, snippets } = part.shorthand()
|
|
||||||
snippets.d = new Snippet('notch', part.points.a)
|
|
||||||
let test = new pattern.Part()
|
|
||||||
test.inject(part)
|
|
||||||
expect(test.points.a.x).to.equal(12)
|
|
||||||
expect(test.points.a.y).to.equal(23)
|
|
||||||
expect(test.paths.bar.ops.length).to.equal(3)
|
|
||||||
for (let i = 0; i < 3; i++) {
|
|
||||||
expect(test.paths.bar.ops[i].type).to.equal(part.paths.bar.ops[i].type)
|
|
||||||
expect(test.paths.bar.ops[i].to.x).to.equal(part.paths.bar.ops[i].to.x)
|
|
||||||
expect(test.paths.bar.ops[i].to.y).to.equal(part.paths.bar.ops[i].to.y)
|
|
||||||
}
|
|
||||||
expect(test.snippets.d.anchor.x).to.equal(part.points.a.x)
|
|
||||||
expect(test.snippets.d.anchor.y).to.equal(part.points.a.y)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('Should return shorthand', () => {
|
|
||||||
let pattern = new Pattern()
|
|
||||||
pattern.settings.mode = 'draft'
|
|
||||||
pattern.settings.paperless = true
|
|
||||||
let part = new pattern.Part()
|
|
||||||
let short = part.shorthand()
|
|
||||||
expect(short.complete).to.equal(true)
|
|
||||||
expect(short.paperless).to.equal(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('Should raise a warning when setting a non-Point value in points', () => {
|
it('Should raise a warning when setting a non-Point value in points', () => {
|
||||||
const pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design()
|
||||||
const part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
|
pattern.init()
|
||||||
const { points } = part.shorthand()
|
const { points } = part.shorthand()
|
||||||
points.a = 'banana'
|
points.a = 'banana'
|
||||||
expect(pattern.events.warning.length).to.equal(4)
|
expect(pattern.store.logs.warning.length).to.equal(4)
|
||||||
expect(pattern.events.warning[0]).to.equal(
|
expect(pattern.store.logs.warning[0]).to.equal(
|
||||||
'`points.a` was set with a value that is not a `Point` object'
|
'`points.a` was set with a value that is not a `Point` object'
|
||||||
)
|
)
|
||||||
expect(pattern.events.warning[1]).to.equal(
|
expect(pattern.store.logs.warning[1]).to.equal(
|
||||||
'`points.a` was set with a `x` parameter that is not a `number`'
|
'`points.a` was set with a `x` parameter that is not a `number`'
|
||||||
)
|
)
|
||||||
expect(pattern.events.warning[2]).to.equal(
|
expect(pattern.store.logs.warning[2]).to.equal(
|
||||||
'`points.a` was set with a `y` parameter that is not a `number`'
|
'`points.a` was set with a `y` parameter that is not a `number`'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should raise a warning when setting a non-Snippet value in snippets', () => {
|
it('Should raise a warning when setting a non-Snippet value in snippets', () => {
|
||||||
const pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design()
|
||||||
const part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
|
pattern.init()
|
||||||
const { snippets } = part.shorthand()
|
const { snippets } = part.shorthand()
|
||||||
snippets.a = 'banana'
|
snippets.a = 'banana'
|
||||||
expect(pattern.events.warning.length).to.equal(4)
|
expect(pattern.store.logs.warning.length).to.equal(4)
|
||||||
expect(pattern.events.warning[0]).to.equal(
|
expect(pattern.store.logs.warning[0]).to.equal(
|
||||||
'`snippets.a` was set with a value that is not a `Snippet` object'
|
'`snippets.a` was set with a value that is not a `Snippet` object'
|
||||||
)
|
)
|
||||||
expect(pattern.events.warning[1]).to.equal(
|
expect(pattern.store.logs.warning[1]).to.equal(
|
||||||
'`snippets.a` was set with a `def` parameter that is not a `string`'
|
'`snippets.a` was set with a `def` parameter that is not a `string`'
|
||||||
)
|
)
|
||||||
expect(pattern.events.warning[2]).to.equal(
|
expect(pattern.store.logs.warning[2]).to.equal(
|
||||||
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
|
'`snippets.a` was set with an `anchor` parameter that is not a `Point`'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should calculate the part boundary with default margin', () => {
|
it('Should calculate the part boundary with default margin', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design()
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
let short = part.shorthand()
|
pattern.init()
|
||||||
|
const short = part.shorthand()
|
||||||
part.points.from = new short.Point(123, 456)
|
part.points.from = new short.Point(123, 456)
|
||||||
part.points.to = new short.Point(19, 76)
|
part.points.to = new short.Point(19, 76)
|
||||||
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
||||||
|
@ -173,11 +129,11 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should calculate the part boundary with custom margin', () => {
|
it('Should calculate the part boundary with custom margin', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design({ margin: 5 })
|
||||||
pattern.settings.margin = 5
|
const part = pattern.__createPartWithContext()
|
||||||
let part = new pattern.Part()
|
pattern.init()
|
||||||
let short = part.shorthand()
|
const short = part.shorthand()
|
||||||
part.points.from = new short.Point(123, 456)
|
part.points.from = new short.Point(123, 456)
|
||||||
part.points.to = new short.Point(19, 76)
|
part.points.to = new short.Point(19, 76)
|
||||||
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
||||||
|
@ -192,12 +148,11 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should calculate the part boundary for paperless', () => {
|
it('Should calculate the part boundary for paperless', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design({ paperless: true })
|
||||||
pattern.settings.margin = 5
|
const part = pattern.__createPartWithContext()
|
||||||
pattern.settings.paperless = true
|
pattern.init()
|
||||||
let part = new pattern.Part()
|
const short = part.shorthand()
|
||||||
let short = part.shorthand()
|
|
||||||
part.points.from = new short.Point(123, 456)
|
part.points.from = new short.Point(123, 456)
|
||||||
part.points.to = new short.Point(19, 76)
|
part.points.to = new short.Point(19, 76)
|
||||||
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
||||||
|
@ -210,16 +165,23 @@ describe('Part', () => {
|
||||||
expect(boundary.width).to.equal(124)
|
expect(boundary.width).to.equal(124)
|
||||||
expect(boundary.height).to.equal(400)
|
expect(boundary.height).to.equal(400)
|
||||||
})
|
})
|
||||||
|
/*
|
||||||
it('Should stack a part', () => {
|
it('Should stack a part', () => {
|
||||||
let pattern = new Pattern()
|
const part = {
|
||||||
pattern.settings.mode = 'draft'
|
name: 'test',
|
||||||
let part = new pattern.Part()
|
draft: (part) => {
|
||||||
let short = part.shorthand()
|
const { points, Point, paths, Path } = part.shorthand()
|
||||||
part.points.from = new short.Point(123, 456)
|
points.from = new Point(123, 456)
|
||||||
part.points.to = new short.Point(19, 76)
|
points.to = new Point(19, 76)
|
||||||
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
|
paths.test = new Path().move(points.from).line(points.to)
|
||||||
part.stack()
|
return aprt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const design = new Design({ parts: [ part ]})
|
||||||
|
const pattern = new design({ paperless: true })
|
||||||
|
pattern.draft()
|
||||||
|
pattern.parts.test.stack()
|
||||||
|
console.log(pattern.parts.test.attributes)
|
||||||
expect(part.attributes.get('transform')).to.equal('translate(-17, -74)')
|
expect(part.attributes.get('transform')).to.equal('translate(-17, -74)')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -236,11 +198,12 @@ describe('Part', () => {
|
||||||
part.stack()
|
part.stack()
|
||||||
expect(part.attributes.get('transform')).to.equal(false)
|
expect(part.attributes.get('transform')).to.equal(false)
|
||||||
})
|
})
|
||||||
|
*/
|
||||||
it('Should run hooks', () => {
|
it('Should run hooks', () => {
|
||||||
let count = 0
|
let count = 0
|
||||||
const pattern = new Pattern()
|
const design = new Design()
|
||||||
const part = new pattern.Part()
|
const pattern = new design({ paperless: true })
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
part.hooks.preDraft = [
|
part.hooks.preDraft = [
|
||||||
{
|
{
|
||||||
method: function (p) {
|
method: function (p) {
|
||||||
|
@ -253,22 +216,23 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should get the units closure to raise a debug when passing a non-number', () => {
|
it('Should get the units closure to raise a debug when passing a non-number', () => {
|
||||||
const pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design({ margin: 5 })
|
||||||
pattern.settings.debug = true
|
const part = pattern.__createPartWithContext()
|
||||||
const part = new pattern.Part()
|
pattern.init()
|
||||||
const short = part.shorthand()
|
const short = part.shorthand()
|
||||||
short.units('a')
|
short.units('a')
|
||||||
expect(pattern.events.debug.length).to.equal(1)
|
expect(pattern.store.logs.warning.length).to.equal(1)
|
||||||
expect(pattern.events.debug[0]).to.equal(
|
expect(pattern.store.logs.warning[0]).to.equal(
|
||||||
'Calling `units(value)` but `value` is not a number (`string`)'
|
'Calling `units(value)` but `value` is not a number (`string`)'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should generate the part transforms', () => {
|
it('Should generate the part transforms', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
pattern.settings.mode = 'draft'
|
const pattern = new design({ margin: 5 })
|
||||||
let part = new pattern.Part()
|
const part = pattern.__createPartWithContext()
|
||||||
|
pattern.init()
|
||||||
let short = part.shorthand()
|
let short = part.shorthand()
|
||||||
part.points.from = new short.Point(2, 2)
|
part.points.from = new short.Point(2, 2)
|
||||||
part.points.to = new short.Point(19, 76)
|
part.points.to = new short.Point(19, 76)
|
||||||
|
@ -283,33 +247,36 @@ describe('Part', () => {
|
||||||
expect(part.attributes.list.transform.length).to.equal(1)
|
expect(part.attributes.list.transform.length).to.equal(1)
|
||||||
expect(part.attributes.list.transform[0]).to.equal('translate(10 20)')
|
expect(part.attributes.list.transform[0]).to.equal('translate(10 20)')
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('isEmpty', () => {
|
describe('isEmpty', () => {
|
||||||
it('Should return true if the part has no paths or snippets', () => {
|
it('Should return true if the part has no paths or snippets', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
expect(part.isEmpty()).to.be.true
|
expect(part.isEmpty()).to.be.true
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return true if the part has paths but they have no length', () => {
|
it('Should return true if the part has paths but they have no length', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Path, paths, Point } = part.shorthand()
|
const { Path, paths, Point } = part.shorthand()
|
||||||
paths.seam = new Path()
|
paths.seam = new Path()
|
||||||
expect(part.isEmpty()).to.be.true
|
expect(part.isEmpty()).to.be.true
|
||||||
})
|
})
|
||||||
|
|
||||||
it("Should return true if the part has paths but they don't render", () => {
|
it("Should return true if the part has paths but they don't render", () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Path, paths, Point } = part.shorthand()
|
const { Path, paths, Point } = part.shorthand()
|
||||||
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3)).setRender(false)
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3)).setRender(false)
|
||||||
expect(part.isEmpty()).to.be.true
|
expect(part.isEmpty()).to.be.true
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return false if the part has a path with length', () => {
|
it('Should return false if the part has a path with length', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Path, paths, Point } = part.shorthand()
|
const { Path, paths, Point } = part.shorthand()
|
||||||
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3))
|
paths.seam = new Path().move(new Point(0, 0)).line(new Point(2, 3))
|
||||||
|
|
||||||
|
@ -317,8 +284,9 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return false if the part has a snippet', () => {
|
it('Should return false if the part has a snippet', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Point, snippets, Snippet } = part.shorthand()
|
const { Point, snippets, Snippet } = part.shorthand()
|
||||||
snippets.test = new Snippet('test', new Point(0, 0))
|
snippets.test = new Snippet('test', new Point(0, 0))
|
||||||
|
|
||||||
|
@ -326,8 +294,9 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return false if the part has a point that has text', () => {
|
it('Should return false if the part has a point that has text', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Point, points } = part.shorthand()
|
const { Point, points } = part.shorthand()
|
||||||
points.test = new Point(0, 0)
|
points.test = new Point(0, 0)
|
||||||
points.test.attributes.set('data-text', 'text')
|
points.test.attributes.set('data-text', 'text')
|
||||||
|
@ -335,8 +304,9 @@ describe('Part', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should return false if the part has a point that has a circle', () => {
|
it('Should return false if the part has a point that has a circle', () => {
|
||||||
let pattern = new Pattern()
|
const design = new Design()
|
||||||
let part = new pattern.Part()
|
const pattern = new design()
|
||||||
|
const part = pattern.__createPartWithContext()
|
||||||
const { Point, points } = part.shorthand()
|
const { Point, points } = part.shorthand()
|
||||||
points.test = new Point(0, 0)
|
points.test = new Point(0, 0)
|
||||||
points.test.attributes.set('data-circle', 10)
|
points.test.attributes.set('data-circle', 10)
|
||||||
|
|
|
@ -646,7 +646,26 @@ describe('Pattern', () => {
|
||||||
const pattern = new Pattern()
|
const pattern = new Pattern()
|
||||||
const part = pattern.__createPartWithContext('test')
|
const part = pattern.__createPartWithContext('test')
|
||||||
expect(typeof part.context).to.equal('object')
|
expect(typeof part.context).to.equal('object')
|
||||||
|
expect(typeof part.context.parts).to.equal('object')
|
||||||
|
expect(typeof part.context.config).to.equal('object')
|
||||||
|
expect(typeof part.context.config.options).to.equal('object')
|
||||||
|
expect(typeof part.context.config.data).to.equal('object')
|
||||||
|
expect(Array.isArray(part.context.config.measurements)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.config.optionalMeasurements)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.config.parts)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.config.plugins)).to.equal(true)
|
||||||
expect(part.context.settings).to.equal(pattern.settings)
|
expect(part.context.settings).to.equal(pattern.settings)
|
||||||
|
expect(typeof part.context.store).to.equal('object')
|
||||||
|
expect(typeof part.context.store.log).to.equal('object')
|
||||||
|
expect(typeof part.context.store.log.debug).to.equal('function')
|
||||||
|
expect(typeof part.context.store.log.info).to.equal('function')
|
||||||
|
expect(typeof part.context.store.log.warning).to.equal('function')
|
||||||
|
expect(typeof part.context.store.log.error).to.equal('function')
|
||||||
|
expect(typeof part.context.store.logs).to.equal('object')
|
||||||
|
expect(Array.isArray(part.context.store.logs.debug)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.store.logs.info)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.store.logs.warning)).to.equal(true)
|
||||||
|
expect(Array.isArray(part.context.store.logs.error)).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue