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