source: imaps-frontend/node_modules/call-bind/test/index.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.7 KB
Line 
1'use strict';
2
3var callBind = require('../');
4var bind = require('function-bind');
5var gOPD = require('gopd');
6var hasStrictMode = require('has-strict-mode')();
7var forEach = require('for-each');
8var inspect = require('object-inspect');
9var v = require('es-value-fixtures');
10
11var test = require('tape');
12
13/*
14 * older engines have length nonconfigurable
15 * in io.js v3, it is configurable except on bound functions, hence the .bind()
16 */
17var functionsHaveConfigurableLengths = !!(
18 gOPD
19 && Object.getOwnPropertyDescriptor
20 && Object.getOwnPropertyDescriptor(bind.call(function () {}), 'length').configurable
21);
22
23test('callBind', function (t) {
24 forEach(v.nonFunctions, function (nonFunction) {
25 t['throws'](
26 function () { callBind(nonFunction); },
27 TypeError,
28 inspect(nonFunction) + ' is not a function'
29 );
30 });
31
32 var sentinel = { sentinel: true };
33 var func = function (a, b) {
34 // eslint-disable-next-line no-invalid-this
35 return [!hasStrictMode && this === global ? undefined : this, a, b];
36 };
37 t.equal(func.length, 2, 'original function length is 2');
38 t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
39 t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
40 t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
41
42 var bound = callBind(func);
43 t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
44 t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
45 t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args');
46 t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
47
48 var boundR = callBind(func, sentinel);
49 t.equal(boundR.length, func.length, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
50 t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
51 t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
52 t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
53
54 var boundArg = callBind(func, sentinel, 1);
55 t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !functionsHaveConfigurableLengths });
56 t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
57 t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
58 t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
59
60 t.test('callBind.apply', function (st) {
61 var aBound = callBind.apply(func);
62 st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
63 st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
64 st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
65
66 var aBoundArg = callBind.apply(func);
67 st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
68 st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
69 st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
70
71 var aBoundR = callBind.apply(func, sentinel);
72 st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
73 st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
74 st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
75
76 st.end();
77 });
78
79 t.end();
80});
Note: See TracBrowser for help on using the repository browser.