index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var callBind = require('../');
  3. var test = require('tape');
  4. test('callBind', function (t) {
  5. var sentinel = { sentinel: true };
  6. var func = function (a, b) {
  7. // eslint-disable-next-line no-invalid-this
  8. return [this, a, b];
  9. };
  10. t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
  11. t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
  12. t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
  13. var bound = callBind(func);
  14. t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
  15. t.deepEqual(bound(1, 2), [1, 2, undefined], 'bound func with right args');
  16. t.deepEqual(bound(1, 2, 3), [1, 2, 3], 'bound func with too many args');
  17. var boundR = callBind(func, sentinel);
  18. t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
  19. t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
  20. t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
  21. var boundArg = callBind(func, sentinel, 1);
  22. t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
  23. t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
  24. t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
  25. t.test('callBind.apply', function (st) {
  26. var aBound = callBind.apply(func);
  27. st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
  28. st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
  29. st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
  30. var aBoundArg = callBind.apply(func);
  31. st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
  32. st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
  33. st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
  34. var aBoundR = callBind.apply(func, sentinel);
  35. st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
  36. st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
  37. st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
  38. st.end();
  39. });
  40. t.end();
  41. });