2018-02-16 12:19:37 -05:00
|
|
|
/**
|
|
|
|
* Test case runner
|
|
|
|
*
|
2021-07-22 15:00:15 -04:00
|
|
|
* Copyright (C) 2014-2021 Ryan Specialty Group, LLC.
|
2018-02-16 12:19:37 -05:00
|
|
|
*
|
|
|
|
* This file is part of TAME.
|
|
|
|
*
|
|
|
|
* TAME is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { Class } = require( 'easejs' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run test cases and report results
|
|
|
|
*/
|
|
|
|
module.exports = Class( 'TestRunner',
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* SUT
|
|
|
|
*
|
|
|
|
* @type {Program}
|
|
|
|
*/
|
|
|
|
'private _program': null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reporter
|
|
|
|
*
|
|
|
|
* @type {TestReporter}
|
|
|
|
*/
|
|
|
|
'private _reporter': null,
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize runner for program PROGRAM
|
|
|
|
*
|
|
|
|
* @param {TestReporter} reporter test reporter
|
|
|
|
* @param {Program} program SUT
|
|
|
|
*/
|
|
|
|
constructor( reporter, program )
|
|
|
|
{
|
|
|
|
// primitive check to guess whether this might be a program
|
|
|
|
if ( typeof program.rater !== 'function' )
|
|
|
|
{
|
|
|
|
throw TypeError( "program#rater is not a function" );
|
|
|
|
}
|
|
|
|
|
|
|
|
this._reporter = reporter;
|
|
|
|
this._program = program;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run set of test cases
|
|
|
|
*
|
|
|
|
* @param {Array<TestCase>} dfns array of TestCases
|
|
|
|
*
|
2018-02-16 14:41:39 -05:00
|
|
|
* @return {Promise} promise to complete test cases, yielding results
|
2018-02-16 12:19:37 -05:00
|
|
|
*/
|
|
|
|
'public runTests'( dfns )
|
|
|
|
{
|
|
|
|
const total = dfns.length;
|
|
|
|
|
|
|
|
this._reporter.preRun( total );
|
|
|
|
|
2018-03-05 16:22:13 -05:00
|
|
|
return this.runAllTests( dfns ).then(
|
2018-02-23 14:15:09 -05:00
|
|
|
results => ( this._reporter.done( results ), results )
|
2018-02-16 12:19:37 -05:00
|
|
|
);
|
2018-02-16 14:41:39 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-05 16:22:13 -05:00
|
|
|
* Run all tests
|
2018-02-16 14:41:39 -05:00
|
|
|
*
|
2018-03-05 16:22:13 -05:00
|
|
|
* This may be overridden by subtypes to change how the tests are run
|
|
|
|
* (for example, to run each asynchronously).
|
2018-02-16 14:41:39 -05:00
|
|
|
*
|
|
|
|
* @param {Array<TestCase>} dfns test case definitions
|
|
|
|
*
|
|
|
|
* @return {Promise} promise to complete test cases, yielding results
|
|
|
|
*/
|
2018-03-05 16:22:13 -05:00
|
|
|
'virtual protected runAllTests'( dfns )
|
2018-02-16 14:41:39 -05:00
|
|
|
{
|
|
|
|
const total = dfns.length;
|
|
|
|
|
2018-03-05 16:22:13 -05:00
|
|
|
return Promise.resolve(
|
|
|
|
dfns.map( ( dfn, i ) => this.runTest( dfn, ( i + 1 ), total ) )
|
|
|
|
);
|
2018-02-16 12:19:37 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run individual test case
|
|
|
|
*
|
|
|
|
* @param {Object<TestCase>} _ source test case
|
|
|
|
* @param {number} test_i test index
|
|
|
|
* @param {number} total total number of tests
|
|
|
|
*
|
|
|
|
* @return {Object<desc,i,total,failures>} test results
|
|
|
|
*/
|
2020-03-09 12:21:32 -04:00
|
|
|
'protected runTest'(
|
|
|
|
{
|
|
|
|
description: desc,
|
|
|
|
allow_failures,
|
|
|
|
data,
|
|
|
|
expect,
|
|
|
|
},
|
|
|
|
test_i,
|
|
|
|
total
|
|
|
|
) {
|
|
|
|
const can_term = ( typeof allow_failures === 'string' )
|
|
|
|
? !( allow_failures.toLowerCase() === 'true')
|
|
|
|
: !allow_failures;
|
|
|
|
|
2018-02-16 12:19:37 -05:00
|
|
|
// no input map---#rate uses params directly
|
2020-03-09 12:21:32 -04:00
|
|
|
const result = this._tryRun( data, can_term );
|
2018-02-16 12:19:37 -05:00
|
|
|
|
|
|
|
const cmp = Object.keys( expect ).map(
|
|
|
|
field => [
|
|
|
|
field,
|
|
|
|
this._deepCompare( expect[ field ], result[ field ] )
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-02-23 15:45:54 -05:00
|
|
|
const failures = ( result instanceof Error )
|
|
|
|
? [ {
|
|
|
|
field: "error",
|
|
|
|
expect: "",
|
|
|
|
result: result.message,
|
|
|
|
} ]
|
|
|
|
: cmp.filter( ( [ , ok ] ) => !ok )
|
|
|
|
.map( ( [ field ] ) => ( {
|
|
|
|
field: field,
|
|
|
|
expect: expect[ field ],
|
|
|
|
result: result[ field ],
|
|
|
|
} ) );
|
2018-02-16 12:19:37 -05:00
|
|
|
|
|
|
|
const succeeded = cmp.length - failures.length;
|
|
|
|
|
|
|
|
const result_data = {
|
|
|
|
desc: desc,
|
|
|
|
i: test_i,
|
|
|
|
total: cmp.length,
|
|
|
|
failures: failures,
|
2018-02-23 14:15:09 -05:00
|
|
|
given: data,
|
|
|
|
expect: expect,
|
2018-02-16 12:19:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
this._reporter.testCaseResult( result_data, total );
|
|
|
|
|
|
|
|
return result_data;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-02-23 15:45:54 -05:00
|
|
|
/**
|
|
|
|
* Attempt test case, returning error on failure
|
|
|
|
*
|
2019-02-26 11:10:25 -05:00
|
|
|
* If an error is thrown (e.g. terminating classification or unknown
|
|
|
|
* input), then it will be returned in place of the results.
|
2018-02-23 15:45:54 -05:00
|
|
|
*
|
|
|
|
* @param {Object} data input data
|
|
|
|
*
|
|
|
|
* @return {Object|Error} result or error
|
|
|
|
*/
|
2020-03-09 12:21:32 -04:00
|
|
|
'private _tryRun'( data, can_term )
|
2018-02-23 15:45:54 -05:00
|
|
|
{
|
|
|
|
// no input map---#rate uses params directly
|
|
|
|
try
|
|
|
|
{
|
2019-02-26 11:10:25 -05:00
|
|
|
this._verifyKnownParams( data );
|
|
|
|
|
2020-03-09 12:21:32 -04:00
|
|
|
return this._program.rater( data, can_term ).vars;
|
2018-02-23 15:45:54 -05:00
|
|
|
}
|
|
|
|
catch( e )
|
|
|
|
{
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2019-02-26 11:10:25 -05:00
|
|
|
/**
|
|
|
|
* Verify that all provided inputs match known params
|
|
|
|
*
|
|
|
|
* If a given input is not known for the rater for the current program,
|
|
|
|
* an Error will be thrown with a comma-delimited list of all unknown
|
|
|
|
* params.
|
|
|
|
*
|
|
|
|
* @param {Object} data input data
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*
|
|
|
|
* @throws Error when unknown input is found
|
|
|
|
*/
|
|
|
|
'private _verifyKnownParams'( data )
|
|
|
|
{
|
|
|
|
const params = this._program.rater.params || {};
|
|
|
|
|
|
|
|
const unknown = Object.keys( data )
|
|
|
|
.filter( param => params[ param ] === undefined );
|
|
|
|
|
|
|
|
if ( unknown.length > 0 )
|
|
|
|
{
|
|
|
|
throw Error( "Unknown params: " + unknown.join( ", " ) );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-02-16 12:19:37 -05:00
|
|
|
/**
|
|
|
|
* Recursively compare values (scalar, array)
|
|
|
|
*
|
|
|
|
* @param {number|Array<number>} x first value
|
|
|
|
* @param {number|Array<number>} y second value
|
|
|
|
*
|
|
|
|
* @return {boolean} whether X deeply equals Y
|
|
|
|
*/
|
|
|
|
'private _deepCompare'( x, y )
|
|
|
|
{
|
|
|
|
// vector/matrix/etc
|
|
|
|
if ( Array.isArray( x ) )
|
|
|
|
{
|
|
|
|
return Array.isArray( y )
|
|
|
|
&& ( x.length === y.length )
|
2018-10-22 10:59:03 -04:00
|
|
|
&& x.every( ( xval, i ) => this._deepCompare( xval, y[ i ] ) );
|
2018-02-16 12:19:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// scalar
|
|
|
|
return x === y;
|
|
|
|
},
|
|
|
|
} );
|