Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-02-26 11:10:25 -0500
committerMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-02-26 11:10:25 -0500
commitc062cc5a5ceb4c44f3de90a779504b6a0800a3cf (patch)
tree413231e4ef77791b24bd014c53a46a00bbd74610 /progtest
parent602a77443f859c93cdac9c37c09bf936d0a3b841 (diff)
downloadtame-c062cc5a5ceb4c44f3de90a779504b6a0800a3cf.tar.gz
tame-c062cc5a5ceb4c44f3de90a779504b6a0800a3cf.tar.bz2
tame-c062cc5a5ceb4c44f3de90a779504b6a0800a3cf.zip
progtest: Check inputs against known params
This aims to prevent needlessly wasted time debugging a non-working test case, and to avoid writing incorrect test cases that happen to succeed even though their inputs aren't properly defined. For example, a common error is to use the name of a bucket field rather than the name of the param that it maps to. * progtest/src/TestRunner.js (_verifyKnownParams): New method. (_tryRun): Use it. * progtest/test/TestRunnerTest.js: New test case. Modify existing test cases to define used params. * progtest/test/_stub/program.js (exports.rater.params): Declare used param.
Diffstat (limited to 'progtest')
-rw-r--r--progtest/src/TestRunner.js33
-rw-r--r--progtest/test/TestRunnerTest.js29
-rw-r--r--progtest/test/_stub/program.js3
3 files changed, 63 insertions, 2 deletions
diff --git a/progtest/src/TestRunner.js b/progtest/src/TestRunner.js
index 5af3d4c..ee0f4dc 100644
--- a/progtest/src/TestRunner.js
+++ b/progtest/src/TestRunner.js
@@ -156,8 +156,8 @@ module.exports = Class( 'TestRunner',
/**
* Attempt test case, returning error on failure
*
- * If an error is thrown (e.g. terminating classification), it will be
- * returned in place of the results.
+ * If an error is thrown (e.g. terminating classification or unknown
+ * input), then it will be returned in place of the results.
*
* @param {Object} data input data
*
@@ -168,6 +168,8 @@ module.exports = Class( 'TestRunner',
// no input map---#rate uses params directly
try
{
+ this._verifyKnownParams( data );
+
return this._program.rater( data ).vars;
}
catch( e )
@@ -178,6 +180,33 @@ module.exports = Class( 'TestRunner',
/**
+ * 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( ", " ) );
+ }
+ },
+
+
+ /**
* Recursively compare values (scalar, array)
*
* @param {number|Array<number>} x first value
diff --git a/progtest/test/TestRunnerTest.js b/progtest/test/TestRunnerTest.js
index 6d88415..75e8ed9 100644
--- a/progtest/test/TestRunnerTest.js
+++ b/progtest/test/TestRunnerTest.js
@@ -41,6 +41,9 @@ describe( "TestRunner", () =>
}
};
+ // `a' is a known param
+ program.rater.params = { a: {} };
+
const test_cases = [
{
description: "first",
@@ -118,6 +121,32 @@ describe( "TestRunner", () =>
} );
+ it( "fails on unknown params", () =>
+ {
+ // no params at all are defined
+ const program = { rater: () => ( { vars: {} } ) };
+
+ const bad_test = {
+ description: 'bad param',
+ data: {
+ unknown_param_1: 0,
+ unknown_param_2: 0,
+ },
+ expect: {},
+ };
+
+ return Sut( NullTestReporter(), program )
+ .runTests( [ bad_test ] )
+ .then( ( [ result ] ) =>
+ {
+ expect( result.failures[ 0 ].result )
+ .to.contain( 'unknown_param_1' );
+ expect( result.failures[ 0 ].result )
+ .to.contain( 'unknown_param_2' );
+ } );
+ } );
+
+
it( "invokes reporter before, during, and after test cases", done =>
{
let pre = false;
diff --git a/progtest/test/_stub/program.js b/progtest/test/_stub/program.js
index 3f42a98..673ce57 100644
--- a/progtest/test/_stub/program.js
+++ b/progtest/test/_stub/program.js
@@ -23,3 +23,6 @@ exports.rater = data =>
{
return { vars: { out: data.in } };
};
+
+exports.rater.params = { in: {} };
+