From 452d7c155b412f6a383952e1def639c12f654794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1n=20D=C3=A9nes?= Date: Sun, 11 Jul 2021 13:45:53 +0200 Subject: [PATCH] Units separated to their own block, and properly tested --- src/ergogen.js | 17 ++++--- src/points.js | 15 +----- src/units.js | 21 ++++++++ test/points/000_units.yaml | 11 +++++ test/points/000_units___units.json | 7 +++ ..._data.json => 001_basic_2x2___points.json} | 0 test/unit/units.js | 48 +++++++++++++++++++ 7 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 src/units.js create mode 100644 test/points/000_units.yaml create mode 100644 test/points/000_units___units.json rename test/points/{001_basic_2x2___points_data.json => 001_basic_2x2___points.json} (100%) create mode 100644 test/unit/units.js diff --git a/src/ergogen.js b/src/ergogen.js index b14fa87..5a35f1a 100644 --- a/src/ergogen.js +++ b/src/ergogen.js @@ -1,4 +1,5 @@ const prepare = require('./prepare') +const units_lib = require('./units') const points_lib = require('./points') const outlines_lib = require('./outlines') const cases_lib = require('./cases') @@ -15,13 +16,17 @@ module.exports = { config = prepare.inherit(config) const results = {} - logger('Parsing points...') - const [points, units] = points_lib.parse(config.points) + // parsing units + logger('Calculating variables...') + const units = units_lib.parse(config) if (debug) { - results.points = { - data: points, - units: units - } + results.units = units + } + + logger('Parsing points...') + const points = points_lib.parse(config.points, units) + if (debug) { + results.points = points } logger('Generating outlines...') diff --git a/src/points.js b/src/points.js index 7d9fb3a..fcc5168 100644 --- a/src/points.js +++ b/src/points.js @@ -215,18 +215,7 @@ const perform_mirror = exports._perform_mirror = (point, axis) => { return ['', null] } -exports.parse = (config = {}) => { - - // parsing units - const raw_units = prep.extend({ - u: 19, - cx: 18, - cy: 17 - }, a.sane(config.units || {}, 'points.units', 'object')()) - const units = {} - for (const [key, val] of Object.entries(raw_units)) { - units[key] = a.mathnum(val)(units) - } +exports.parse = (config, units) => { // config sanitization a.unexpected(config, 'points', ['units', 'zones', 'key', 'rotate', 'mirror']) @@ -317,7 +306,7 @@ exports.parse = (config = {}) => { } // done - return [filtered, units] + return filtered } exports.visualize = (points) => { diff --git a/src/units.js b/src/units.js new file mode 100644 index 0000000..3bedd2a --- /dev/null +++ b/src/units.js @@ -0,0 +1,21 @@ +const a = require('./assert') +const prep = require('./prepare') + +const default_units = { + u: 19, + cx: 18, + cy: 17 +} + +exports.parse = (config = {}) => { + const raw_units = prep.extend( + default_units, + a.sane(config.units || {}, 'units', 'object')(), + a.sane(config.variables || {}, 'variables', 'object')() + ) + const units = {} + for (const [key, val] of Object.entries(raw_units)) { + units[key] = a.mathnum(val)(units) + } + return units +} \ No newline at end of file diff --git a/test/points/000_units.yaml b/test/points/000_units.yaml new file mode 100644 index 0000000..73e89dd --- /dev/null +++ b/test/points/000_units.yaml @@ -0,0 +1,11 @@ +units: + a: cy - 7 +variables: + b: a * 1.5 +points: + zones: + matrix: + columns: + col: + rows: + row: \ No newline at end of file diff --git a/test/points/000_units___units.json b/test/points/000_units___units.json new file mode 100644 index 0000000..9f2a007 --- /dev/null +++ b/test/points/000_units___units.json @@ -0,0 +1,7 @@ +{ + "u": 19, + "cx": 18, + "cy": 17, + "a": 10, + "b": 15 +} diff --git a/test/points/001_basic_2x2___points_data.json b/test/points/001_basic_2x2___points.json similarity index 100% rename from test/points/001_basic_2x2___points_data.json rename to test/points/001_basic_2x2___points.json diff --git a/test/unit/units.js b/test/unit/units.js new file mode 100644 index 0000000..19bf781 --- /dev/null +++ b/test/unit/units.js @@ -0,0 +1,48 @@ +const u = require('../../src/units') + +describe('Units', function() { + + it('defaults', function() { + // check that an empty config has the default units (and nothing more) + const def = u.parse({}) + Object.keys(def).length.should.equal(3) + def.u.should.equal(19) + def.cx.should.equal(18) + def.cy.should.equal(17) + }) + + it('units', function() { + // check that units can contain formulas, and reference each other + const res = u.parse({ + units: { + a: 'cx / 2', + b: 'a + 1' + } + }) + Object.keys(res).length.should.equal(5) + res.a.should.equal(9) + res.b.should.equal(10) + // also check that order matters, which it should + u.parse.bind(this, { + units: { + a: 'b + 1', + b: 'cx / 2' + } + }).should.throw() + }) + + it('variables', function() { + // check that variables work, and can override units + const res = u.parse({ + units: { + a: 'cx / 2', + }, + variables: { + a: 'u + 1' + } + }) + Object.keys(res).length.should.equal(4) + res.a.should.equal(20) + }) + +}) \ No newline at end of file