TestRegister.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * TestRegister.js
  3. *
  4. * This is so individual files can register their tests in one place, and
  5. * ensure that they will get run by the frontend.
  6. *
  7. * @author tlwr [toby@toby.codes
  8. *
  9. * @copyright Crown Copyright 2017
  10. * @license Apache-2.0
  11. *
  12. */
  13. (function() {
  14. /**
  15. * Add a list of tests to the register.
  16. *
  17. * @class
  18. */
  19. function TestRegister() {
  20. this.tests = [];
  21. }
  22. /**
  23. * Add a list of tests to the register.
  24. *
  25. * @param {Object[]} tests
  26. */
  27. TestRegister.prototype.addTests = function(tests) {
  28. this.tests = this.tests.concat(tests);
  29. };
  30. /**
  31. * Returns the list of tests.
  32. *
  33. * @returns {Object[]} tests
  34. */
  35. TestRegister.prototype.getTests = function() {
  36. return this.tests;
  37. };
  38. /**
  39. * Runs all the tests in the register.
  40. *
  41. */
  42. TestRegister.prototype.runTests = function() {
  43. return Promise.all(
  44. this.tests.map(function(test, i) {
  45. var chef = new Chef();
  46. return Promise.resolve(chef.bake(
  47. test.input,
  48. test.recipeConfig,
  49. {},
  50. 0,
  51. 0
  52. ))
  53. .then(function(result) {
  54. var ret = {
  55. test: test,
  56. status: null,
  57. output: null,
  58. };
  59. if (result.error) {
  60. if (test.expectedError) {
  61. ret.status = "passing";
  62. } else {
  63. ret.status = "erroring";
  64. ret.output = result.error.displayStr;
  65. }
  66. } else {
  67. if (test.expectedError) {
  68. ret.status = "failing";
  69. ret.output = "Expected an error but did not receive one.";
  70. } else if (result.result === test.expectedOutput) {
  71. ret.status = "passing";
  72. } else {
  73. ret.status = "failing";
  74. ret.output = [
  75. "Expected",
  76. "\t" + test.expectedOutput.replace(/\n/g, "\n\t"),
  77. "Received",
  78. "\t" + result.result.replace(/\n/g, "\n\t"),
  79. ].join("\n");
  80. }
  81. }
  82. return ret;
  83. });
  84. })
  85. );
  86. };
  87. // Singleton TestRegister, keeping things simple and obvious.
  88. window.TestRegister = new TestRegister();
  89. })();