From dd5aeb529b8b261d6b64d3aac25f0fa55fe30df8 Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Fri, 17 Sep 2021 23:08:27 +0300 Subject: [PATCH] fix: guard against 0 revs in propslip (#93) Adds also minimal test for this. --- calcs/propslip.js | 52 ++++++++++++++++++++++++++++++++++++++++------- test/test.js | 21 ++++++++++++------- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/calcs/propslip.js b/calcs/propslip.js index 3d88572..f174a59 100644 --- a/calcs/propslip.js +++ b/calcs/propslip.js @@ -1,5 +1,24 @@ const _ = require('lodash') +const selfData = { + propulsion: { + port: { + transmission: { + gearRatio: { + value: 1 + } + }, + drive: { + propeller: { + pitch: { + value: 1 + } + } + } + } + } +} + module.exports = function (app, plugin) { return plugin.engines.map(instance => { return { @@ -26,13 +45,32 @@ module.exports = function (app, plugin) { var pitch = app.getSelfPath( 'propulsion.' + instance + '.drive.propeller.pitch.value' ) - return [ - { - path: 'propulsion.' + instance + '.drive.propeller.slip', - value: 1 - stw * gearRatio / (revolutions * pitch) - } - ] - } + if (revolutions > 0) { + return [ + { + path: 'propulsion.' + instance + '.drive.propeller.slip', + value: 1 - stw * gearRatio / (revolutions * pitch) + } + ] + } + }, + tests: [ + { + input: [0, 1], + selfData, + expected: undefined + }, + { + input: [2, 1], + selfData, + expected: [ + { + path: 'propulsion.port.drive.propeller.slip', + value: 0.5 + } + ] + } + ] } }) } diff --git a/test/test.js b/test/test.js index 10b8697..90aa0ef 100644 --- a/test/test.js +++ b/test/test.js @@ -5,19 +5,21 @@ const chai = require('chai') chai.Should() chai.use(require('chai-json-equal')); +let selfData = {} + const app = { - debug: () => {}, - getSelfPath: (path) => {} + debug: () => { }, + getSelfPath: (path) => _.get(selfData, path) } const plugin = { - batteries: [ '0', '1' ], - engines: [ 'port' ], + batteries: ['0', '1'], + engines: ['port'], tanks: ['fuel'], air: ['outside'] } -function load_calcs () { +function load_calcs() { fpath = path.join(__dirname, '../calcs') files = fs.readdirSync(fpath) return files @@ -37,11 +39,16 @@ describe('derived data converts', function () { calcs.forEach(calci => { (_.isArray(calci) ? calci : [calci]).forEach(calc => { - if ( calc.tests ) { + if (calc.tests) { calc.tests.forEach((test, idx) => { it(`${calc.title}[${idx}] works`, (done) => { + selfData = test.selfData || {} let res = calc.calculator.apply(null, test.input) - res.should.jsonEqual(test.expected) + if (test.expected) { + res.should.jsonEqual(test.expected) + } else { + (typeof res).should.equal('undefined') + } done() }) })