| 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 | */
|
|---|
| 15 | var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths;
|
|---|
| 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 | return [!hasStrictMode && this === global ? undefined : this, a, b];
|
|---|
| 29 | };
|
|---|
| 30 | t.equal(func.length, 2, 'original function length is 2');
|
|---|
| 31 | t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
|
|---|
| 32 | t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
|
|---|
| 33 | t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
|
|---|
| 34 |
|
|---|
| 35 | var bound = callBind(func);
|
|---|
| 36 | t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
|---|
| 37 | t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
|
|---|
| 38 | t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args');
|
|---|
| 39 | t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
|
|---|
| 40 |
|
|---|
| 41 | var boundR = callBind(func, sentinel);
|
|---|
| 42 | t.equal(boundR.length, func.length, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
|---|
| 43 | t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
|
|---|
| 44 | t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
|
|---|
| 45 | t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
|
|---|
| 46 |
|
|---|
| 47 | var zeroLen = function () {
|
|---|
| 48 | return [!hasStrictMode && this === global ? undefined : this];
|
|---|
| 49 | };
|
|---|
| 50 | t.equal(zeroLen.length, 0, 'zero-length function has length 0');
|
|---|
| 51 |
|
|---|
| 52 | var boundZero = callBind(zeroLen, sentinel);
|
|---|
| 53 | t.equal(boundZero.length, 0, 'zero-length function with receiver has length 0', { skip: !boundFnsHaveConfigurableLengths });
|
|---|
| 54 | t.deepEqual(boundZero(), [sentinel], 'zero-length bound func with receiver works');
|
|---|
| 55 |
|
|---|
| 56 | var boundArg = callBind(func, sentinel, 1);
|
|---|
| 57 | t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
|
|---|
| 58 | t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
|
|---|
| 59 | t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
|
|---|
| 60 | t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
|
|---|
| 61 |
|
|---|
| 62 | t.test('callBind.apply', function (st) {
|
|---|
| 63 | var aBound = callBind.apply(func);
|
|---|
| 64 | st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
|
|---|
| 65 | st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
|
|---|
| 66 | st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
|
|---|
| 67 |
|
|---|
| 68 | var aBoundArg = callBind.apply(func);
|
|---|
| 69 | st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
|
|---|
| 70 | st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
|
|---|
| 71 | st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
|
|---|
| 72 |
|
|---|
| 73 | var aBoundR = callBind.apply(func, sentinel);
|
|---|
| 74 | st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
|
|---|
| 75 | st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
|
|---|
| 76 | st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
|
|---|
| 77 |
|
|---|
| 78 | st.end();
|
|---|
| 79 | });
|
|---|
| 80 |
|
|---|
| 81 | t.end();
|
|---|
| 82 | });
|
|---|