assert.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (C) 2017 Ecma International. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: |
  5. Collection of assertion functions used throughout test262
  6. defines: [assert]
  7. ---*/
  8. function assert(mustBeTrue, message) {
  9. if (mustBeTrue === true) {
  10. return;
  11. }
  12. if (message === undefined) {
  13. message = 'Expected true but got ' + assert._toString(mustBeTrue);
  14. }
  15. $ERROR(message);
  16. }
  17. assert._isSameValue = function (a, b) {
  18. if (a === b) {
  19. // Handle +/-0 vs. -/+0
  20. return a !== 0 || 1 / a === 1 / b;
  21. }
  22. // Handle NaN vs. NaN
  23. return a !== a && b !== b;
  24. };
  25. assert.sameValue = function (actual, expected, message) {
  26. try {
  27. if (assert._isSameValue(actual, expected)) {
  28. return;
  29. }
  30. } catch (error) {
  31. $ERROR(message + ' (_isSameValue operation threw) ' + error);
  32. return;
  33. }
  34. if (message === undefined) {
  35. message = '';
  36. } else {
  37. message += ' ';
  38. }
  39. message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(expected) + '») to be true';
  40. $ERROR(message);
  41. };
  42. assert.notSameValue = function (actual, unexpected, message) {
  43. if (!assert._isSameValue(actual, unexpected)) {
  44. return;
  45. }
  46. if (message === undefined) {
  47. message = '';
  48. } else {
  49. message += ' ';
  50. }
  51. message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(unexpected) + '») to be false';
  52. $ERROR(message);
  53. };
  54. assert.throws = function (expectedErrorConstructor, func, message) {
  55. if (typeof func !== "function") {
  56. $ERROR('assert.throws requires two arguments: the error constructor ' +
  57. 'and a function to run');
  58. return;
  59. }
  60. if (message === undefined) {
  61. message = '';
  62. } else {
  63. message += ' ';
  64. }
  65. try {
  66. func();
  67. } catch (thrown) {
  68. if (typeof thrown !== 'object' || thrown === null) {
  69. message += 'Thrown value was not an object!';
  70. $ERROR(message);
  71. } else if (thrown.constructor !== expectedErrorConstructor) {
  72. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  73. $ERROR(message);
  74. }
  75. return;
  76. }
  77. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  78. $ERROR(message);
  79. };
  80. assert._toString = function (value) {
  81. try {
  82. if (value === 0 && 1 / value === -Infinity) {
  83. return '-0';
  84. }
  85. return String(value);
  86. } catch (err) {
  87. if (err.name === 'TypeError') {
  88. return Object.prototype.toString.call(value);
  89. }
  90. throw err;
  91. }
  92. };