test-common.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }