[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var callBind = require('../');
|
---|
| 4 | var hasStrictMode = require('has-strict-mode')();
|
---|
| 5 | var forEach = require('for-each');
|
---|
| 6 | var inspect = require('object-inspect');
|
---|
| 7 | var v = require('es-value-fixtures');
|
---|
| 8 |
|
---|
| 9 | var test = require('tape');
|
---|
| 10 |
|
---|
| 11 | /*
|
---|
| 12 | * older engines have length nonconfigurable
|
---|
| 13 | * in io.js v3, it is configurable except on bound functions, hence the .bind()
|
---|
| 14 | */
|
---|
[79a0317] | 15 | var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths;
|
---|
[d565449] | 16 |
|
---|
| 17 | test('callBind', function (t) {
|
---|
| 18 | forEach(v.nonFunctions, function (nonFunction) {
|
---|
| 19 | t['throws'](
|
---|
| 20 | function () { callBind(nonFunction); },
|
---|
| 21 | TypeError,
|
---|
| 22 | inspect(nonFunction) + ' is not a function'
|
---|
| 23 | );
|
---|
| 24 | });
|
---|
| 25 |
|
---|
| 26 | var sentinel = { sentinel: true };
|
---|
| 27 | var func = function (a, b) {
|
---|
| 28 | // eslint-disable-next-line no-invalid-this
|
---|
| 29 | return [!hasStrictMode && this === global ? undefined : this, a, b];
|
---|
| 30 | };
|
---|
| 31 | t.equal(func.length, 2, 'original function length is 2');
|
---|
| 32 | t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
|
---|
| 33 | t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
|
---|
| 34 | t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
|
---|
| 35 |
|
---|
| 36 | var bound = callBind(func);
|
---|
[79a0317] | 37 | t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
---|
[d565449] | 38 | t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
|
---|
| 39 | t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args');
|
---|
| 40 | t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
|
---|
| 41 |
|
---|
| 42 | var boundR = callBind(func, sentinel);
|
---|
[79a0317] | 43 | t.equal(boundR.length, func.length, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
---|
[d565449] | 44 | t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
|
---|
| 45 | t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
|
---|
| 46 | t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
|
---|
| 47 |
|
---|
| 48 | var boundArg = callBind(func, sentinel, 1);
|
---|
[79a0317] | 49 | t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
---|
[d565449] | 50 | t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
|
---|
| 51 | t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
|
---|
| 52 | t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
|
---|
| 53 |
|
---|
| 54 | t.test('callBind.apply', function (st) {
|
---|
| 55 | var aBound = callBind.apply(func);
|
---|
| 56 | st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
|
---|
| 57 | st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
|
---|
| 58 | st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
|
---|
| 59 |
|
---|
| 60 | var aBoundArg = callBind.apply(func);
|
---|
| 61 | st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
|
---|
| 62 | st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
|
---|
| 63 | st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
|
---|
| 64 |
|
---|
| 65 | var aBoundR = callBind.apply(func, sentinel);
|
---|
| 66 | st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
|
---|
| 67 | st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
|
---|
| 68 | st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
|
---|
| 69 |
|
---|
| 70 | st.end();
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 | t.end();
|
---|
| 74 | });
|
---|