test-common.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Custom error for failed assertions.
  3. * @constructor
  4. * @param {string} message Error message
  5. * @returns Error
  6. */
  7. function AssertionError(message) {
  8. var instance = new Error(message);
  9. instance.name = 'AssertionError';
  10. Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
  11. return instance;
  12. }
  13. /**
  14. * Throws an `AssertionError` if `value` is not truthy.
  15. * @param {*} value Value to be tested
  16. */
  17. function assert(value) {
  18. if (!value)
  19. throw new AssertionError("The assertion failed!");
  20. }
  21. /**
  22. * Throws an `AssertionError` when called.
  23. * @throws {AssertionError}
  24. */
  25. function assertNotReached() {
  26. throw new AssertionError("assertNotReached() was reached!");
  27. }
  28. /**
  29. * Ensures the provided functions throws a specific error.
  30. * @param {Function} testFunction Function executing the throwing code
  31. * @param {object} [options]
  32. * @param {Error} [options.error] Expected error type
  33. * @param {string} [options.name] Expected error name
  34. * @param {string} [options.message] Expected error message
  35. */
  36. function assertThrowsError(testFunction, options) {
  37. try {
  38. testFunction();
  39. assertNotReached();
  40. } catch (e) {
  41. if (options.error !== undefined)
  42. assert(e instanceof options.error);
  43. if (options.name !== undefined)
  44. assert(e.name === options.name);
  45. if (options.message !== undefined)
  46. assert(e.message === options.message);
  47. }
  48. }
  49. /**
  50. * Ensures the provided JavaScript source code results in a SyntaxError
  51. * @param {string} source The JavaScript source code to compile
  52. */
  53. function assertIsSyntaxError(source) {
  54. assertThrowsError(() => {
  55. new Function(source)();
  56. }, {
  57. error: SyntaxError,
  58. });
  59. }
  60. /**
  61. * Ensures the provided arrays contain exactly the same items.
  62. * @param {Array} a First array
  63. * @param {Array} b Second array
  64. */
  65. function assertArrayEquals(a, b) {
  66. if (a.length != b.length)
  67. throw new AssertionError("Array lengths do not match");
  68. for (var i = 0; i < a.length; i++) {
  69. if (a[i] !== b[i])
  70. throw new AssertionError("Elements do not match");
  71. }
  72. }
  73. const assertVisitsAll = (testFunction, expectedOutput) => {
  74. const visited = [];
  75. testFunction(value => visited.push(value));
  76. assert(visited.length === expectedOutput.length);
  77. expectedOutput.forEach((value, i) => assert(visited[i] === value));
  78. };
  79. /**
  80. * Check whether the difference between two numbers is less than 0.000001.
  81. * @param {Number} a First number
  82. * @param {Number} b Second number
  83. */
  84. function isClose(a, b) {
  85. return Math.abs(a - b) < 0.000001;
  86. }
  87. /**
  88. * Quick and dirty deep equals method.
  89. * @param {*} a First value
  90. * @param {*} b Second value
  91. */
  92. function assertDeepEquals(a, b) {
  93. assert(deepEquals(a, b));
  94. }
  95. function deepEquals(a, b) {
  96. if (Array.isArray(a))
  97. return Array.isArray(b) && deepArrayEquals(a, b);
  98. if (typeof a === "object")
  99. return typeof b === "object" && deepObjectEquals(a, b);
  100. return a === b;
  101. }
  102. function deepArrayEquals(a, b) {
  103. if (a.length !== b.length)
  104. return false;
  105. for (let i = 0; i < a.length; ++i) {
  106. if (!deepEquals(a[i], b[i]))
  107. return false;
  108. }
  109. return true;
  110. }
  111. function deepObjectEquals(a, b) {
  112. if (a === null)
  113. return b === null;
  114. for (let key of Reflect.ownKeys(a)) {
  115. if (!deepEquals(a[key], b[key]))
  116. return false;
  117. }
  118. return true;
  119. }