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