assert.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ---*/
  7. function assert(mustBeTrue, message) {
  8. if (mustBeTrue === true) {
  9. return;
  10. }
  11. if (message === undefined) {
  12. message = 'Expected true but got ' + String(mustBeTrue);
  13. }
  14. $ERROR(message);
  15. }
  16. assert._isSameValue = function (a, b) {
  17. if (a === b) {
  18. // Handle +/-0 vs. -/+0
  19. return a !== 0 || 1 / a === 1 / b;
  20. }
  21. // Handle NaN vs. NaN
  22. return a !== a && b !== b;
  23. };
  24. assert.sameValue = function (actual, expected, message) {
  25. if (assert._isSameValue(actual, expected)) {
  26. return;
  27. }
  28. if (message === undefined) {
  29. message = '';
  30. } else {
  31. message += ' ';
  32. }
  33. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  34. $ERROR(message);
  35. };
  36. assert.notSameValue = function (actual, unexpected, message) {
  37. if (!assert._isSameValue(actual, unexpected)) {
  38. return;
  39. }
  40. if (message === undefined) {
  41. message = '';
  42. } else {
  43. message += ' ';
  44. }
  45. message += 'Expected SameValue(«' + String(actual) + '», «' + String(unexpected) + '») to be false';
  46. $ERROR(message);
  47. };
  48. assert.throws = function (expectedErrorConstructor, func, message) {
  49. if (typeof func !== "function") {
  50. $ERROR('assert.throws requires two arguments: the error constructor ' +
  51. 'and a function to run');
  52. return;
  53. }
  54. if (message === undefined) {
  55. message = '';
  56. } else {
  57. message += ' ';
  58. }
  59. try {
  60. func();
  61. } catch (thrown) {
  62. if (typeof thrown !== 'object' || thrown === null) {
  63. message += 'Thrown value was not an object!';
  64. $ERROR(message);
  65. } else if (thrown.constructor !== expectedErrorConstructor) {
  66. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  67. $ERROR(message);
  68. }
  69. return;
  70. }
  71. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  72. $ERROR(message);
  73. };