index.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* eslint no-console: 0 */
  2. /**
  3. * TestRunner.js
  4. *
  5. * For running the tests in the test register.
  6. *
  7. * @author tlwr [toby@toby.codes]
  8. * @author n1474335 [n1474335@gmail.com]
  9. * @copyright Crown Copyright 2017
  10. * @license Apache-2.0
  11. */
  12. import "babel-polyfill";
  13. // Define global environment functions
  14. global.ENVIRONMENT_IS_WORKER = function() {
  15. return typeof importScripts === "function";
  16. };
  17. global.ENVIRONMENT_IS_NODE = function() {
  18. return typeof process === "object" && typeof require === "function";
  19. };
  20. global.ENVIRONMENT_IS_WEB = function() {
  21. return typeof window === "object";
  22. };
  23. import TestRegister from "./TestRegister";
  24. // import "./tests/operations/Base58.js";
  25. import "./tests/operations/Base64";
  26. // import "./tests/operations/BCD.js";
  27. // import "./tests/operations/BitwiseOp.js";
  28. // import "./tests/operations/BSON.js";
  29. // import "./tests/operations/ByteRepr.js";
  30. // import "./tests/operations/CharEnc.js";
  31. // import "./tests/operations/Cipher.js";
  32. // import "./tests/operations/Code.js";
  33. // import "./tests/operations/Compress.js";
  34. // import "./tests/operations/DateTime.js";
  35. // import "./tests/operations/FlowControl.js";
  36. // import "./tests/operations/Hash.js";
  37. // import "./tests/operations/Hexdump.js";
  38. // import "./tests/operations/Image.js";
  39. // import "./tests/operations/MorseCode.js";
  40. // import "./tests/operations/MS.js";
  41. // import "./tests/operations/PHP.js";
  42. // import "./tests/operations/NetBIOS.js";
  43. // import "./tests/operations/OTP.js";
  44. // import "./tests/operations/Regex.js";
  45. import "./tests/operations/Rotate.mjs";
  46. // import "./tests/operations/StrUtils.js";
  47. // import "./tests/operations/SeqUtils.js";
  48. import "./tests/operations/SetUnion";
  49. import "./tests/operations/SetIntersection";
  50. import "./tests/operations/SetDifference";
  51. import "./tests/operations/SymmetricDifference";
  52. import "./tests/operations/CartesianProduct";
  53. import "./tests/operations/PowerSet";
  54. import "./tests/nodeApi/nodeApi";
  55. let allTestsPassing = true;
  56. const testStatusCounts = {
  57. total: 0,
  58. };
  59. /**
  60. * Helper function to convert a status to an icon.
  61. *
  62. * @param {string} status
  63. * @returns {string}
  64. */
  65. function statusToIcon(status) {
  66. const icons = {
  67. erroring: "🔥",
  68. failing: "❌",
  69. passing: "✔️️",
  70. };
  71. return icons[status] || "?";
  72. }
  73. /**
  74. * Displays a given test result in the console.
  75. *
  76. * @param {Object} testResult
  77. */
  78. function handleTestResult(testResult) {
  79. allTestsPassing = allTestsPassing && testResult.status === "passing";
  80. const newCount = (testStatusCounts[testResult.status] || 0) + 1;
  81. testStatusCounts[testResult.status] = newCount;
  82. testStatusCounts.total += 1;
  83. console.log([
  84. statusToIcon(testResult.status),
  85. testResult.test.name
  86. ].join(" "));
  87. if (testResult.output) {
  88. console.log(
  89. testResult.output
  90. .trim()
  91. .replace(/^/, "\t")
  92. .replace(/\n/g, "\n\t")
  93. );
  94. }
  95. }
  96. /**
  97. * Fail if the process takes longer than 10 seconds.
  98. */
  99. setTimeout(function() {
  100. console.log("Tests took longer than 10 seconds to run, returning.");
  101. process.exit(1);
  102. }, 10 * 1000);
  103. Promise.all([
  104. TestRegister.runTests(),
  105. TestRegister.runApiTests()
  106. ])
  107. .then(function(resultsPair) {
  108. const results = resultsPair[0].concat(resultsPair[1]);
  109. results.forEach(handleTestResult);
  110. console.log("\n");
  111. for (const testStatus in testStatusCounts) {
  112. const count = testStatusCounts[testStatus];
  113. if (count > 0) {
  114. console.log(testStatus.toUpperCase(), count);
  115. }
  116. }
  117. if (!allTestsPassing) {
  118. console.log("\nNot all tests are passing");
  119. }
  120. process.exit(allTestsPassing ? 0 : 1);
  121. });