Skip to content

Commit

Permalink
test: make so tests can have mulitple sets of options
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Sep 12, 2024
1 parent 5bf02ec commit b42b0e3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
27 changes: 19 additions & 8 deletions conversions/temperature.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

let tempMessage = (pgn, temp, inst, src) => {
return [{
return {
pgn,
prio: 2,
dst: 255,
Expand All @@ -9,7 +9,7 @@ let tempMessage = (pgn, temp, inst, src) => {
"Source": src,
[pgn == 130316 ? "Temperature" : "Actual Temperature"]: temp
}
}]
}
}

function makeTemperature(pgn, prefix, info)
Expand All @@ -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
Expand All @@ -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)
}
]
}
]
}]
Expand Down
85 changes: 46 additions & 39 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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)
})
})
}
}
})
})
})
})
Expand Down

0 comments on commit b42b0e3

Please sign in to comment.