PHP and Javascript implementations of a simple markdown parser
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

basetest.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. class BaseTest {
  2. numberDifferenceRatio = 0.000_001;
  3. setUp() {}
  4. tearDown() {}
  5. /** @var {TestCaseRunner|null} */
  6. currentRunner = null;
  7. fail(failMessage=null) {
  8. throw new FailureError(failMessage || 'failed');
  9. }
  10. assertTrue(test, failMessage=null) {
  11. if (!test) this.fail(failMessage || `expected true, got ${test}`);
  12. }
  13. assertFalse(test, failMessage=null) {
  14. if (test) this.fail(failMessage || `expected false, got ${test}`);
  15. }
  16. assertEqual(a, b, failMessage=null) {
  17. if (BaseTest.#equal(a, b, this.numberDifferenceRatio)) return;
  18. const aVal = (typeof a == 'string') ? `"${a}"` : `${a}`;
  19. const bVal = (typeof b == 'string') ? `"${b}"` : `${b}`;
  20. if (aVal.length > 20 || bVal.length > 20) {
  21. this.fail(failMessage || `equality failed:\n${aVal}\n!=\n${bVal}`);
  22. } else {
  23. this.fail(failMessage || `equality failed: ${aVal} != ${bVal}`);
  24. }
  25. }
  26. expectError(e=true) {
  27. if (this.currentRunner) this.currentRunner.expectedError = e;
  28. }
  29. /**
  30. * @param {number} maxTime maximum time in seconds
  31. * @param {function} timedCode code to run and time
  32. */
  33. profile(maxTime, timedCode) {
  34. const startTime = performance.now();
  35. const result = timedCode();
  36. const endTime = performance.now();
  37. const seconds = (endTime - startTime) / 1000.0;
  38. if (seconds > maxTime) {
  39. this.fail(`Expected <= ${maxTime}s execution time, actual ${seconds}s.`);
  40. }
  41. return result;
  42. }
  43. /**
  44. * @param {Array} a
  45. * @param {Array} b
  46. * @returns {boolean}
  47. */
  48. static #equalArrays(a, b) {
  49. if (a === b) return true;
  50. if (!(a instanceof Array) || !(b instanceof Array)) return false;
  51. if (a == null || b == null) return false;
  52. if (a.length != b.length) return false;
  53. for (var i = 0; i < a.length; i++) {
  54. if (!this.#equal(a[i], b[i])) return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * @param {object} a
  60. * @param {object} b
  61. * @returns {boolean}
  62. */
  63. static #equalObjects(a, b) {
  64. if (a === b) return true;
  65. if (!(a instanceof Object) || !(b instanceof Object)) return false;
  66. if (a == null || b == null) return false;
  67. if (a.equals !== undefined) {
  68. return a.equals(b);
  69. }
  70. for (const key of Object.keys(a)) {
  71. if (!this.#equal(a[key], b[key])) return false;
  72. }
  73. for (const key of Object.keys(b)) {
  74. if (!this.#equal(a[key], b[key])) return false;
  75. }
  76. return true;
  77. }
  78. /**
  79. * @param {number} a
  80. * @param {number} b
  81. * @returns {boolean}
  82. */
  83. static #equalNumbers(a, b, differenceRatio=0.000001) {
  84. if (a === b) return true;
  85. const largestAllowableDelta = Math.max(Math.abs(a), Math.abs(b)) * differenceRatio;
  86. const delta = Math.abs(a - b);
  87. return delta <= largestAllowableDelta;
  88. }
  89. /**
  90. * Tests for equality on lots of different kinds of values including objects
  91. * and arrays. Will use `.equals` on objects that implement it.
  92. *
  93. * @param {any} a
  94. * @param {any} b
  95. * @param {number} numberDifferenceRatio
  96. * @returns {boolean}
  97. */
  98. static #equal(a, b, numberDifferenceRatio=0.000_001) {
  99. if (a instanceof Array && b instanceof Array) {
  100. return this.#equalArrays(a, b);
  101. }
  102. if (a instanceof Object && b instanceof Object) {
  103. return this.#equalObjects(a, b);
  104. }
  105. if (typeof a === 'number' && typeof b === 'number') {
  106. return this.#equalNumbers(a, b, numberDifferenceRatio);
  107. }
  108. return a === b;
  109. }
  110. }