1
0
Fork 0

wip(core): Unit tests

This commit is contained in:
joostdecock 2022-09-10 18:45:57 +02:00
parent 4121ef44d2
commit b7071bb487
8 changed files with 130 additions and 64 deletions

View file

@ -1,22 +1,19 @@
import { Attributes } from './attributes.mjs'
import { round } from './utils.mjs'
import { addNonEnumProp, round } from './utils.mjs'
import { version } from '../data.mjs'
export function Svg(pattern) {
this.openGroups = []
this.layout = {}
this.freeId = 0
this.body = ''
/*
* This breaks SVG style (see #1606)
* Can we not set variables in SVG style?
* this.style = `svg.freesewing.pattern { --pattern-scale: ${pattern.settings.scale} }`
*/
this.style = ''
this.script = ''
this.defs = ''
// Non-enumerable properties
addNonEnumProp(this, 'openGroups', [])
addNonEnumProp(this, 'layout', {})
addNonEnumProp(this, 'freeId', 0)
addNonEnumProp(this, 'body', '')
addNonEnumProp(this, 'style', '')
addNonEnumProp(this, 'defs', '')
addNonEnumProp(this, 'prefix', '<?xml version="1.0" encoding="UTF-8" standalone="no"?>')
// Enumerable properties
this.pattern = pattern // Needed to expose pattern to hooks
this.prefix = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
this.attributes = new Attributes()
this.attributes.add('xmlns', 'http://www.w3.org/2000/svg')
this.attributes.add('xmlns:svg', 'http://www.w3.org/2000/svg')

View file

@ -1,5 +1,5 @@
import chai from 'chai'
import freesewing from '../dist/index.js'
import { Design } from '../src/index.mjs'
const expect = chai.expect
@ -8,19 +8,22 @@ const toAbs = (val, { measurements }) => measurements.head * val
describe('Snapped options', () => {
it('Should snap a percentage options to equal steps', () => {
const design = new freesewing.Design({
const part = {
name: 'test',
options: {
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs },
},
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
}
}
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(60)
expect(patternB.settings.absoluteOptions.test).to.equal(108)
})
it('Should snap a percentage options to the Fibonacci sequence', () => {
const design = new freesewing.Design({
const part = {
name: 'test',
options: {
test: {
pct: 30,
@ -29,18 +32,20 @@ describe('Snapped options', () => {
toAbs,
snap: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144],
},
},
})
const patternA = new design({ options: { test: 0.13 }, measurements })
const patternB = new design({ options: { test: 0.27 }, measurements })
const patternC = new design({ options: { test: 0.97 }, measurements })
}
}
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(55)
expect(patternB.settings.absoluteOptions.test).to.equal(89)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
})
it('Should snap a percentage options to imperial snaps', () => {
const design = new freesewing.Design({
const part = {
name: 'test',
options: {
test: {
pct: 30,
@ -52,12 +57,13 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6],
},
},
},
})
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'metric' })
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' })
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' })
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'metric' })
}
}
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'metric' }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' }).draft()
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'metric' }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(50)
expect(patternB.settings.absoluteOptions.test).to.equal(100)
expect(patternC.settings.absoluteOptions.test).to.equal(388)
@ -65,7 +71,8 @@ describe('Snapped options', () => {
})
it('Should snap a percentage options to metrics snaps', () => {
const design = new freesewing.Design({
const part = {
name: 'test',
options: {
test: {
pct: 30,
@ -77,12 +84,13 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6],
},
},
},
})
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'imperial' })
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'imperial' })
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'imperial' })
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'imperial' })
}
}
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'imperial' }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'imperial' }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'imperial' }).draft()
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'imperial' }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(50.8)
expect(patternB.settings.absoluteOptions.test).to.equal(101.6)
expect(patternC.settings.absoluteOptions.test).to.equal(388)

View file

@ -1,6 +1,6 @@
import chai from 'chai'
import chaiString from 'chai-string'
import { Pattern } from '../src/index.mjs'
import { Design, Pattern } from '../src/index.mjs'
import pkg from '../package.json' assert { type: 'json' }
import render from './fixtures/render.mjs'
@ -9,8 +9,21 @@ const expect = chai.expect
const { version } = pkg
describe('Svg', () => {
const part = {
name: 'test',
draft: part => {
const { paths, Path, Point, points } = part.shorthand()
points.a = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
.shiftFractionAlong()
return part
}
}
const design = new Design({ parts: [ part ] })
const pattern = new design()
it('Svg constructor should initialize object', () => {
let pattern = new Pattern()
pattern.render()
let svg = pattern.svg
expect(svg.openGroups).to.eql([])

View file

@ -30,6 +30,7 @@ import {
Bezier,
generatePartTransform,
macroName,
Design,
} from '../src/index.mjs'
const { expect } = chai
@ -513,17 +514,22 @@ describe('Utils', () => {
expect(result.toAbs(0.0123, { measurements })).to.equal(12.3)
expect(result.fromAbs(12.3, { measurements })).to.equal(0.0123)
})
/*
it('Should generate a part transform', () => {
let pattern = new Pattern()
pattern.settings.mode = 'draft'
let part = new pattern.Part()
let short = part.shorthand()
part.points.from = new short.Point(2, 2)
part.points.to = new short.Point(19, 76)
part.paths.test = new short.Path().move(part.points.from).line(part.points.to)
part.stack()
const transform = generatePartTransform(30, 60, 90, true, true, part)
const part = {
name: 'test',
draft: part => {
const { points, Point, paths, Path } = part.shorthand()
points.from = new Point(2, 2)
points.to = new Point(19, 76)
paths.test = new Path().move(points.from).line(points.to)
return part
}
}
const design = new Design({ parts: [ part ]})
const pattern = new design()
pattern.draft().render()
const transform = generatePartTransform(30, 60, 90, true, true, pattern.__parts.test)
expect(transform.transform).to.equal(
`translate(${30 + part.topLeft.x + part.bottomRight.x} ${
60 + part.topLeft.y + part.bottomRight.y
@ -532,4 +538,5 @@ describe('Utils', () => {
})`
)
})
*/
})