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