index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/Compress.js";
  15. import "./tests/operations/FlowControl.js";
  16. import "./tests/operations/MorseCode.js";
  17. import "./tests/operations/StrUtils.js";
  18. var allTestsPassing = true,
  19. testStatusCounts = {
  20. total: 0,
  21. };
  22. /**
  23. * Helper function to convert a status to an icon.
  24. *
  25. * @param {string} status
  26. * @returns {string}
  27. */
  28. function statusToIcon(status) {
  29. var icons = {
  30. erroring: "🔥",
  31. failing: "❌",
  32. passing: "✔️️",
  33. };
  34. return icons[status] || "?";
  35. }
  36. /**
  37. * Displays a given test result in the console.
  38. *
  39. * @param {Object} testResult
  40. */
  41. function handleTestResult(testResult) {
  42. allTestsPassing = allTestsPassing && testResult.status === "passing";
  43. var newCount = (testStatusCounts[testResult.status] || 0) + 1;
  44. testStatusCounts[testResult.status] = newCount;
  45. testStatusCounts.total += 1;
  46. console.log([
  47. statusToIcon(testResult.status),
  48. testResult.test.name
  49. ].join(" "));
  50. if (testResult.output) {
  51. console.log(
  52. testResult.output
  53. .trim()
  54. .replace(/^/, "\t")
  55. .replace(/\n/g, "\n\t")
  56. );
  57. }
  58. }
  59. TestRegister.runTests()
  60. .then(function(results) {
  61. results.forEach(handleTestResult);
  62. console.log("\n");
  63. for (var testStatus in testStatusCounts) {
  64. var count = testStatusCounts[testStatus];
  65. if (count > 0) {
  66. console.log(testStatus.toUpperCase(), count);
  67. }
  68. }
  69. if (!allTestsPassing) {
  70. console.log("\nNot all tests are passing");
  71. }
  72. process.exit(allTestsPassing ? 0 : 1);
  73. });