index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/BitwiseOp.js";
  16. import "./tests/operations/ByteRepr.js";
  17. import "./tests/operations/CharEnc.js";
  18. import "./tests/operations/Cipher.js";
  19. import "./tests/operations/Code.js";
  20. import "./tests/operations/Compress.js";
  21. import "./tests/operations/DateTime.js";
  22. import "./tests/operations/FlowControl.js";
  23. import "./tests/operations/Hash.js";
  24. import "./tests/operations/Image.js";
  25. import "./tests/operations/MorseCode.js";
  26. import "./tests/operations/MS.js";
  27. import "./tests/operations/PHP.js";
  28. import "./tests/operations/StrUtils.js";
  29. import "./tests/operations/SeqUtils.js";
  30. let allTestsPassing = true;
  31. const testStatusCounts = {
  32. total: 0,
  33. };
  34. /**
  35. * Helper function to convert a status to an icon.
  36. *
  37. * @param {string} status
  38. * @returns {string}
  39. */
  40. function statusToIcon(status) {
  41. const icons = {
  42. erroring: "🔥",
  43. failing: "❌",
  44. passing: "✔️️",
  45. };
  46. return icons[status] || "?";
  47. }
  48. /**
  49. * Displays a given test result in the console.
  50. *
  51. * @param {Object} testResult
  52. */
  53. function handleTestResult(testResult) {
  54. allTestsPassing = allTestsPassing && testResult.status === "passing";
  55. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  56. testStatusCounts[testResult.status] = newCount;
  57. testStatusCounts.total += 1;
  58. console.log([
  59. statusToIcon(testResult.status),
  60. testResult.test.name
  61. ].join(" "));
  62. if (testResult.output) {
  63. console.log(
  64. testResult.output
  65. .trim()
  66. .replace(/^/, "\t")
  67. .replace(/\n/g, "\n\t")
  68. );
  69. }
  70. }
  71. /**
  72. * Fail if the process takes longer than 10 seconds.
  73. */
  74. setTimeout(function() {
  75. console.log("Tests took longer than 10 seconds to run, returning.");
  76. process.exit(1);
  77. }, 10 * 1000);
  78. TestRegister.runTests()
  79. .then(function(results) {
  80. results.forEach(handleTestResult);
  81. console.log("\n");
  82. for (const testStatus in testStatusCounts) {
  83. const count = testStatusCounts[testStatus];
  84. if (count > 0) {
  85. console.log(testStatus.toUpperCase(), count);
  86. }
  87. }
  88. if (!allTestsPassing) {
  89. console.log("\nNot all tests are passing");
  90. }
  91. process.exit(allTestsPassing ? 0 : 1);
  92. });