index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * TestRunner.js
  3. *
  4. * For running the tests in the test register.
  5. *
  6. * @author tlwr [toby@toby.codes]
  7. * @author n1474335 [n1474335@gmail.com]
  8. * @copyright Crown Copyright 2017
  9. * @license Apache-2.0
  10. */
  11. import "babel-polyfill";
  12. import TestRegister from "./TestRegister.js";
  13. import "./tests/operations/Base58.js";
  14. import "./tests/operations/BCD.js";
  15. import "./tests/operations/ByteRepr.js";
  16. import "./tests/operations/CharEnc.js";
  17. import "./tests/operations/Cipher.js";
  18. import "./tests/operations/Code.js";
  19. import "./tests/operations/Compress.js";
  20. import "./tests/operations/DateTime.js";
  21. import "./tests/operations/FlowControl.js";
  22. import "./tests/operations/Image.js";
  23. import "./tests/operations/MorseCode.js";
  24. import "./tests/operations/StrUtils.js";
  25. import "./tests/operations/SeqUtils.js";
  26. import "./tests/operations/VBE.js";
  27. let allTestsPassing = true;
  28. const testStatusCounts = {
  29. total: 0,
  30. };
  31. /**
  32. * Helper function to convert a status to an icon.
  33. *
  34. * @param {string} status
  35. * @returns {string}
  36. */
  37. function statusToIcon(status) {
  38. const icons = {
  39. erroring: "🔥",
  40. failing: "❌",
  41. passing: "✔️️",
  42. };
  43. return icons[status] || "?";
  44. }
  45. /**
  46. * Displays a given test result in the console.
  47. *
  48. * @param {Object} testResult
  49. */
  50. function handleTestResult(testResult) {
  51. allTestsPassing = allTestsPassing && testResult.status === "passing";
  52. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  53. testStatusCounts[testResult.status] = newCount;
  54. testStatusCounts.total += 1;
  55. console.log([
  56. statusToIcon(testResult.status),
  57. testResult.test.name
  58. ].join(" "));
  59. if (testResult.output) {
  60. console.log(
  61. testResult.output
  62. .trim()
  63. .replace(/^/, "\t")
  64. .replace(/\n/g, "\n\t")
  65. );
  66. }
  67. }
  68. /**
  69. * Fail if the process takes longer than 10 seconds.
  70. */
  71. setTimeout(function() {
  72. console.log("Tests took longer than 10 seconds to run, returning.");
  73. process.exit(1);
  74. }, 10 * 1000);
  75. TestRegister.runTests()
  76. .then(function(results) {
  77. results.forEach(handleTestResult);
  78. console.log("\n");
  79. for (const testStatus in testStatusCounts) {
  80. const count = testStatusCounts[testStatus];
  81. if (count > 0) {
  82. console.log(testStatus.toUpperCase(), count);
  83. }
  84. }
  85. if (!allTestsPassing) {
  86. console.log("\nNot all tests are passing");
  87. }
  88. process.exit(allTestsPassing ? 0 : 1);
  89. });