CyberChef/test/TestRegister.mjs

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-02-25 00:50:17 +01:00
/**
* TestRegister.js
*
* This is so individual files can register their tests in one place, and
* ensure that they will get run by the frontend.
*
* @author tlwr [toby@toby.codes]
2017-02-25 00:50:17 +01:00
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
2018-03-27 00:14:23 +02:00
import Chef from "../src/core/Chef";
2017-02-25 00:50:17 +01:00
(function() {
/**
* Object to store and run the list of tests.
2017-02-25 00:50:17 +01:00
*
* @class
* @constructor
2017-02-25 00:50:17 +01:00
*/
function TestRegister() {
this.tests = [];
}
2017-02-25 00:50:17 +01:00
/**
* Add a list of tests to the register.
*
* @param {Object[]} tests
*/
TestRegister.prototype.addTests = function(tests) {
this.tests = this.tests.concat(tests);
};
/**
* Runs all the tests in the register.
*/
TestRegister.prototype.runTests = function() {
return Promise.all(
this.tests.map(function(test, i) {
2017-05-03 00:06:28 +02:00
const chef = new Chef();
2017-02-25 00:50:17 +01:00
return chef.bake(
2017-02-25 00:50:17 +01:00
test.input,
test.recipeConfig,
{},
0,
false
2017-07-24 15:49:16 +02:00
).then(function(result) {
2017-05-03 00:06:28 +02:00
const ret = {
2017-02-25 00:50:17 +01:00
test: test,
status: null,
output: null,
};
if (result.error) {
if (test.expectedError) {
ret.status = "passing";
} else {
ret.status = "erroring";
ret.output = result.error.displayStr;
}
} else {
if (test.expectedError) {
ret.status = "failing";
ret.output = "Expected an error but did not receive one.";
} else if (result.result === test.expectedOutput) {
ret.status = "passing";
} else {
ret.status = "failing";
ret.output = [
"Expected",
"\t" + test.expectedOutput.replace(/\n/g, "\n\t"),
"Received",
"\t" + result.result.replace(/\n/g, "\n\t"),
].join("\n");
}
}
return ret;
});
})
);
};
2017-02-25 00:50:17 +01:00
// Singleton TestRegister, keeping things simple and obvious.
global.TestRegister = global.TestRegister || new TestRegister();
2017-02-25 00:50:17 +01:00
})();
export default global.TestRegister;