From b42b0e3b1fc9c50f8015a7845a80f9f5b15ff13a Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Thu, 12 Sep 2024 12:27:44 -0400 Subject: [PATCH] test: make so tests can have mulitple sets of options --- conversions/temperature.js | 27 ++++++++---- test/test.js | 85 +++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/conversions/temperature.js b/conversions/temperature.js index 5469660..fc87024 100644 --- a/conversions/temperature.js +++ b/conversions/temperature.js @@ -1,6 +1,6 @@ let tempMessage = (pgn, temp, inst, src) => { - return [{ + return { pgn, prio: 2, dst: 255, @@ -9,7 +9,7 @@ let tempMessage = (pgn, temp, inst, src) => { "Source": src, [pgn == 130316 ? "Temperature" : "Actual Temperature"]: temp } - }] + } } function makeTemperature(pgn, prefix, info) @@ -28,11 +28,17 @@ function makeTemperature(pgn, prefix, info) }, }, - testOptions: { - [optionKey]: { - instance: 0 + testOptions: [ + { + [optionKey]: { + instance: 0 + } + }, + { + [optionKey]: { + } } - }, + ], conversions: (options) => { let instance = options[optionKey].instance @@ -41,12 +47,17 @@ function makeTemperature(pgn, prefix, info) return [{ keys: [ info.source ], callback: (temperature) => { - return tempMessage(pgn, temperature, instance, info.n2kSource) + return [ tempMessage(pgn, temperature, instance, info.n2kSource) ] }, tests: [ { input: [ 281.2 ], - expected: tempMessage(pgn, 281.2, 0, info.n2kSource) + expected: [ + (testOptions) => { + let expectedInstance = testOptions[optionKey].instance !== undefined ? testOptions[optionKey].instance : info.instance + return tempMessage(pgn, 281.2, expectedInstance, info.n2kSource) + } + ] } ] }] diff --git a/test/test.js b/test/test.js index 8c8a1a2..69346ec 100644 --- a/test/test.js +++ b/test/test.js @@ -46,7 +46,7 @@ describe('every conversion has a test', () => { if ( typeof subConversions === 'undefined' ) { subConversions = [ conversion ] } else if ( typeof subConversions === 'function' ) { - subConversions = subConversions(conversion.testOptions || {}) + subConversions = subConversions(Array.isArray(conversion.testOptions) ? conversion.testOptions[0] : conversion.testOptions) } assert(subConversions != undefined) subConversions.forEach(subConv => { @@ -65,48 +65,55 @@ describe('conversions work', () => { } conversion.forEach(conversion => { - var subConversions = conversion.conversions - if ( typeof subConversions === 'undefined' ) { - subConversions = [ conversion ] - } else if ( typeof subConversions === 'function' ) { - subConversions = subConversions(conversion.testOptions || {}) - } - subConversions.forEach(subConv => { - //subConv.should.have.property('tests') - if ( subConv.tests ) { - subConv.tests.forEach((test, idx) => { - it(`${conversion.title} test # ${idx} works`, function (done) { - skData = test.skData || {} - skSelfData = test.skSelfData || {} - let results = subConv.callback.call(null, ...test.input) - assert.equal(results.length, test.expected.length, 'number of results returned does not match the number of expected results') - let error - results.forEach((res, idx) => { - try - { - let encoded = pgnToActisenseSerialFormat(res) - let pgn = parser.parseString(encoded) - delete pgn.description - delete pgn.src - delete pgn.timestamp - delete pgn.input + let optionsList = Array.isArray(conversion.testOptions) ? conversion.testOptions : [ conversion.testOptions ] - let expected = test.expected[idx] - let preprocess = expected["__preprocess__"] - if ( preprocess ) { - preprocess(pgn) - delete expected["__preprocess__"] + optionsList.forEach((options, oidx) => { + var subConversions = conversion.conversions + if ( typeof subConversions === 'undefined' ) { + subConversions = [ conversion ] + } else if ( typeof subConversions === 'function' ) { + subConversions = subConversions(options || {}) + } + subConversions.forEach(subConv => { + //subConv.should.have.property('tests') + if ( subConv.tests ) { + subConv.tests.forEach((test, idx) => { + it(`${conversion.title} test # ${oidx}/${idx} works`, function (done) { + skData = test.skData || {} + skSelfData = test.skSelfData || {} + let results = subConv.callback.call(null, ...test.input) + assert.equal(results.length, test.expected.length, 'number of results returned does not match the number of expected results') + let error + results.forEach((res, idx) => { + try + { + let encoded = pgnToActisenseSerialFormat(res) + let pgn = parser.parseString(encoded) + delete pgn.description + delete pgn.src + delete pgn.timestamp + delete pgn.input + + let expected = test.expected[idx] + if ( typeof expected === 'function' ) { + expected = expected(options) + } + let preprocess = expected["__preprocess__"] + if ( preprocess ) { + preprocess(pgn) + delete expected["__preprocess__"] + } + //console.log('parsed: ' + JSON.stringify(pgn, null, 2)) + pgn.should.jsonEqual(expected) + } catch ( e ) { + error = e } - //console.log('parsed: ' + JSON.stringify(pgn, null, 2)) - pgn.should.jsonEqual(expected) - } catch ( e ) { - error = e - } + }) + done(error) }) - done(error) }) - }) - } + } + }) }) }) })