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.
|
|
|
|
*
|
2017-02-28 18:08:36 +01:00
|
|
|
* @author tlwr [toby@toby.codes]
|
2018-05-03 11:20:13 +02:00
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
2017-02-25 00:50:17 +01:00
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
2019-07-09 13:23:59 +02:00
|
|
|
import Chef from "../../src/core/Chef.mjs";
|
2017-02-28 18:08:36 +01:00
|
|
|
|
2018-05-03 11:20:13 +02:00
|
|
|
/**
|
|
|
|
* Object to store and run the list of tests.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
class TestRegister {
|
|
|
|
|
2017-02-25 00:50:17 +01:00
|
|
|
/**
|
2018-05-03 11:20:13 +02:00
|
|
|
* initialise with no tests
|
2017-02-25 00:50:17 +01:00
|
|
|
*/
|
2018-05-03 11:20:13 +02:00
|
|
|
constructor() {
|
2017-02-25 00:50:17 +01:00
|
|
|
this.tests = [];
|
2018-05-03 11:20:13 +02:00
|
|
|
this.apiTests = [];
|
2017-02-25 00:50:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a list of tests to the register.
|
|
|
|
*
|
|
|
|
* @param {Object[]} tests
|
|
|
|
*/
|
2018-05-03 11:20:13 +02:00
|
|
|
addTests(tests) {
|
2017-02-25 00:50:17 +01:00
|
|
|
this.tests = this.tests.concat(tests);
|
2018-05-03 11:20:13 +02:00
|
|
|
}
|
2017-02-25 00:50:17 +01:00
|
|
|
|
2018-05-03 11:20:13 +02:00
|
|
|
/**
|
|
|
|
* Add a list of api tests to the register
|
|
|
|
* @param {Object[]} tests
|
|
|
|
*/
|
|
|
|
addApiTests(tests) {
|
|
|
|
this.apiTests = this.apiTests.concat(tests);
|
|
|
|
}
|
2017-02-25 00:50:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs all the tests in the register.
|
|
|
|
*/
|
2018-05-03 11:20:13 +02:00
|
|
|
runTests () {
|
2019-08-20 18:13:05 +02:00
|
|
|
console.log("Running tests...");
|
2017-02-25 00:50:17 +01:00
|
|
|
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
|
|
|
|
2017-04-21 23:48:42 +02:00
|
|
|
return chef.bake(
|
2017-02-25 00:50:17 +01:00
|
|
|
test.input,
|
|
|
|
test.recipeConfig,
|
|
|
|
{},
|
|
|
|
0,
|
2017-02-28 18:08:36 +01:00
|
|
|
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";
|
2019-07-05 13:22:52 +02:00
|
|
|
} else if ("expectedMatch" in test && test.expectedMatch.test(result.result)) {
|
2018-08-09 20:17:24 +02:00
|
|
|
ret.status = "passing";
|
2017-02-25 00:50:17 +01:00
|
|
|
} else {
|
|
|
|
ret.status = "failing";
|
2018-08-09 20:17:24 +02:00
|
|
|
const expected = test.expectedOutput ? test.expectedOutput :
|
|
|
|
test.expectedMatch ? test.expectedMatch.toString() : "unknown";
|
2017-02-25 00:50:17 +01:00
|
|
|
ret.output = [
|
|
|
|
"Expected",
|
2018-08-09 20:17:24 +02:00
|
|
|
"\t" + expected.replace(/\n/g, "\n\t"),
|
2017-02-25 00:50:17 +01:00
|
|
|
"Received",
|
|
|
|
"\t" + result.result.replace(/\n/g, "\n\t"),
|
|
|
|
].join("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2018-05-03 11:20:13 +02:00
|
|
|
}
|
2017-03-23 18:52:20 +01:00
|
|
|
|
2018-05-03 11:20:13 +02:00
|
|
|
/**
|
|
|
|
* Run all api related tests and wrap results in report format
|
|
|
|
*/
|
|
|
|
runApiTests() {
|
2019-08-20 18:13:05 +02:00
|
|
|
console.log("Running tests...");
|
|
|
|
|
2018-05-03 11:20:13 +02:00
|
|
|
return Promise.all(this.apiTests.map(async function(test, i) {
|
|
|
|
const result = {
|
|
|
|
test: test,
|
|
|
|
status: null,
|
|
|
|
output: null,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
await test.run();
|
|
|
|
result.status = "passing";
|
|
|
|
} catch (e) {
|
|
|
|
result.status = "erroring";
|
|
|
|
result.output = e.message;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 18:52:20 +01:00
|
|
|
|
2018-05-03 11:20:13 +02:00
|
|
|
// Export an instance to make a singleton
|
|
|
|
export default new TestRegister();
|