From c7b86c755626f7ceba2fad66378586191dc21ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Sat, 22 May 2021 17:58:26 +0200 Subject: [PATCH] Add unit test for anchors --- src/anchor.js | 19 ++++--- test/helpers/point.js | 6 +++ test/unit/anchor.js | 112 ++++++++++++++++++++++++++++++++++++++++++ test/unit/point.js | 8 +-- 4 files changed, 128 insertions(+), 17 deletions(-) create mode 100644 test/helpers/point.js create mode 100644 test/unit/anchor.js diff --git a/src/anchor.js b/src/anchor.js index 7a75a95..3dd0a9c 100644 --- a/src/anchor.js +++ b/src/anchor.js @@ -5,9 +5,9 @@ const Point = require('./point') const anchor = module.exports = (raw, name, points={}, check_unexpected=true, default_point=new Point()) => units => { if (a.type(raw)() == 'array') { // recursive call with incremental default_point mods, according to `affect`s - let current = () => default_point.clone() + let current = default_point.clone() for (const step of raw) { - current = anchor(step, name, points, check_unexpected, current(units)) + current = anchor(step, name, points, check_unexpected, current)(units) } return current } @@ -32,29 +32,28 @@ const anchor = module.exports = (raw, name, points={}, check_unexpected=true, de } } if (raw.orient !== undefined) { - point.r += a.sane(raw.orient || 0, `${name}.orient`, 'number')(units) + point.r += a.sane(raw.orient, `${name}.orient`, 'number')(units) } if (raw.shift !== undefined) { - let xyval = a.wh(raw.shift || [0, 0], `${name}.shift`)(units) + let xyval = a.wh(raw.shift, `${name}.shift`)(units) if (point.meta.mirrored) { xyval[0] = -xyval[0] } point.shift(xyval, true) } if (raw.rotate !== undefined) { - point.r += a.sane(raw.rotate || 0, `${name}.rotate`, 'number')(units) + point.r += a.sane(raw.rotate, `${name}.rotate`, 'number')(units) } if (raw.affect !== undefined) { const candidate = point point = default_point.clone() - const valid_affects = ['x', 'y', 'r'] - let affect = raw.affect || valid_affects + let affect = raw.affect if (a.type(affect)() == 'string') affect = affect.split('') affect = a.strarr(affect, `${name}.affect`) let i = 0 - for (const a of affect) { - a._in(a, `${name}.affect[${++i}]`, valid_affects) - point[a] = candidate[a] + for (const aff of affect) { + a.in(aff, `${name}.affect[${++i}]`, ['x', 'y', 'r']) + point[aff] = candidate[aff] } } return point diff --git a/test/helpers/point.js b/test/helpers/point.js new file mode 100644 index 0000000..1770a6d --- /dev/null +++ b/test/helpers/point.js @@ -0,0 +1,6 @@ +exports.check = (point, expected=[]) => { + point.x.should.equal(expected[0] || 0) + point.y.should.equal(expected[1] || 0) + point.r.should.equal(expected[2] || 0) + point.meta.should.deep.equal(expected[3] || {}) +} \ No newline at end of file diff --git a/test/unit/anchor.js b/test/unit/anchor.js new file mode 100644 index 0000000..a5ed60b --- /dev/null +++ b/test/unit/anchor.js @@ -0,0 +1,112 @@ +const anchor = require('../../src/anchor') +const Point = require('../../src/point') +const {check} = require('../helpers/point') + +describe('Anchor', function() { + + const points = { + o: new Point(0, 0, 0, {label: 'o'}), + ten: new Point(10, 10, 10, {label: 'ten'}), + mirror: new Point(20, 0, 0, {mirrored: true}) + } + + it('params', function() { + // an empty anchor definition leads to the default point + check( + anchor({}, 'name')(), + [0, 0, 0, {}] + ) + // unexpected check can be disabled + check( + anchor({unexpected_key: true}, 'name', {}, false)(), + [0, 0, 0, {}] + ) + // default point can be overridden + check( + anchor({}, 'name', {}, true, new Point(1, 1))(), + [1, 1, 0, {}] + ) + }) + + it('ref', function() { + // single reference + check( + anchor({ref: 'o'}, 'name', points)(), + [0, 0, 0, {label: 'o'}] + ) + // average of multiple references (metadata gets ignored) + check( + anchor({ref: ['o', 'ten']}, 'name', points)(), + [5, 5, 5, {}] + ) + }) + + it('shift', function() { + // normal shift + check( + anchor({shift: [1, 1]}, 'name')(), + [1, 1, 0, {}] + ) + // shift should respect mirrored points (and invert along the x axis) + check( + anchor({ref: 'mirror', shift: [1, 1]}, 'name', points)(), + [19, 1, 0, {mirrored: true}] + ) + }) + + it('orient', function() { + // an orient by itself is equal to rotation + check( + anchor({orient: 10}, 'name')(), + [0, 0, 10, {}] + ) + // orient acts before shifting + // so when we orient to the right, an upward shift goes to the right + check( + anchor({orient: -90, shift: [0, 1]}, 'name')(), + [1, 0, -90, {}] + ) + }) + + it('rotate', function() { + // basic rotation + check( + anchor({rotate: 10}, 'name')(), + [0, 0, 10, {}] + ) + // rotate acts *after* shifting + // so even tho we rotate to the right, an upward shift does go upward + check( + anchor({shift: [0, 1], rotate: -90}, 'name')(), + [0, 1, -90, {}] + ) + }) + + it('affect', function() { + // affect can restrict which point fields (x, y, r) are affected by the transformations + check( + anchor({orient: -90, shift: [0, 1], rotate: 10, affect: 'r'}, 'name')(), + [0, 0, -80, {}] + ) + check( + anchor({orient: -90, shift: [0, 1], rotate: 10, affect: 'xy'}, 'name')(), + [1, 0, 0, {}] + ) + // affects can also be arrays (example same as above) + check( + anchor({orient: -90, shift: [0, 1], rotate: 10, affect: ['x', 'y']}, 'name')(), + [1, 0, 0, {}] + ) + }) + + it('array', function() { + // basic multi-anchor + check( + anchor([ + {shift: [1, 1]}, + {rotate: 10} + ], 'name')(), + [1, 1, 10, {}] + ) + }) +}) \ No newline at end of file diff --git a/test/unit/point.js b/test/unit/point.js index 4f99041..5f4b03f 100644 --- a/test/unit/point.js +++ b/test/unit/point.js @@ -1,12 +1,6 @@ const m = require('makerjs') const Point = require('../../src/point') - -const check = (point, expected) => { - point.x.should.equal(expected[0]) - point.y.should.equal(expected[1]) - point.r.should.equal(expected[2]) - point.meta.should.deep.equal(expected[3]) -} +const {check} = require('../helpers/point') describe('Point', function() {