index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var test = require('tape');
  3. var callBound = require('../');
  4. /** @template {true} T @template U @typedef {T extends U ? T : never} AssertType */
  5. test('callBound', function (t) {
  6. // static primitive
  7. t.equal(callBound('Array.length'), Array.length, 'Array.length yields itself');
  8. t.equal(callBound('%Array.length%'), Array.length, '%Array.length% yields itself');
  9. // static non-function object
  10. t.equal(callBound('Array.prototype'), Array.prototype, 'Array.prototype yields itself');
  11. t.equal(callBound('%Array.prototype%'), Array.prototype, '%Array.prototype% yields itself');
  12. t.equal(callBound('Array.constructor'), Array.constructor, 'Array.constructor yields itself');
  13. t.equal(callBound('%Array.constructor%'), Array.constructor, '%Array.constructor% yields itself');
  14. // static function
  15. t.equal(callBound('Date.parse'), Date.parse, 'Date.parse yields itself');
  16. t.equal(callBound('%Date.parse%'), Date.parse, '%Date.parse% yields itself');
  17. // prototype primitive
  18. t.equal(callBound('Error.prototype.message'), Error.prototype.message, 'Error.prototype.message yields itself');
  19. t.equal(callBound('%Error.prototype.message%'), Error.prototype.message, '%Error.prototype.message% yields itself');
  20. var x = callBound('Object.prototype.toString');
  21. var y = callBound('%Object.prototype.toString%');
  22. // prototype function
  23. t.notEqual(x, Object.prototype.toString, 'Object.prototype.toString does not yield itself');
  24. t.notEqual(y, Object.prototype.toString, '%Object.prototype.toString% does not yield itself');
  25. t.equal(x(true), Object.prototype.toString.call(true), 'call-bound Object.prototype.toString calls into the original');
  26. t.equal(y(true), Object.prototype.toString.call(true), 'call-bound %Object.prototype.toString% calls into the original');
  27. t['throws'](
  28. // @ts-expect-error
  29. function () { callBound('does not exist'); },
  30. SyntaxError,
  31. 'nonexistent intrinsic throws'
  32. );
  33. t['throws'](
  34. // @ts-expect-error
  35. function () { callBound('does not exist', true); },
  36. SyntaxError,
  37. 'allowMissing arg still throws for unknown intrinsic'
  38. );
  39. t.test('real but absent intrinsic', { skip: typeof WeakRef !== 'undefined' }, function (st) {
  40. st['throws'](
  41. function () { callBound('WeakRef'); },
  42. TypeError,
  43. 'real but absent intrinsic throws'
  44. );
  45. st.equal(callBound('WeakRef', true), undefined, 'allowMissing arg avoids exception');
  46. st.end();
  47. });
  48. t.end();
  49. });