index.js 3.6 KB

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