index.js 2.6 KB

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