/** * Test case runner * * Copyright (C) 2018 R-T Specialty, LLC. * * 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 . */ "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} dfns array of TestCases * * @return {Array>} results */ 'public runTests'( dfns ) { const total = dfns.length; this._reporter.preRun( total ); const results = dfns.map( ( test, i ) => this._runTest( test, i, total ) ); this._reporter.done( results ); return results; }, /** * Run individual test case * * @param {Object} _ source test case * @param {number} test_i test index * @param {number} total total number of tests * * @return {Object} test results */ 'private _runTest'( { description: desc, data, expect }, test_i, total ) { // no input map---#rate uses params directly const result = this._program.rater( data ).vars; const cmp = Object.keys( expect ).map( field => [ field, this._deepCompare( expect[ field ], result[ field ] ) ] ); const failures = cmp.filter( ( [ , ok ] ) => !ok ) .map( ( [ field ] ) => ( { field: field, expect: expect[ field ], result: result[ field ], } ) ); const succeeded = cmp.length - failures.length; const result_data = { desc: desc, i: test_i, total: cmp.length, failures: failures, }; this._reporter.testCaseResult( result_data, total ); return result_data; }, /** * Recursively compare values (scalar, array) * * @param {number|Array} x first value * @param {number|Array} 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 ) && x.every( ( xval, i ) => xval === y[ i ] ); } // scalar return x === y; }, } );