Skip to content

Commit

Permalink
fix: guard against 0 revs in propslip (#93)
Browse files Browse the repository at this point in the history
Adds also minimal test for this.
  • Loading branch information
tkurki authored Sep 17, 2021
1 parent 9b71650 commit dd5aeb5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
52 changes: 45 additions & 7 deletions calcs/propslip.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
]
}
]
}
})
}
21 changes: 14 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
})
})
Expand Down

0 comments on commit dd5aeb5

Please sign in to comment.