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

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.6 KB
Line 
1'use strict';
2
3var callBind = require('../');
4var hasStrictMode = require('has-strict-mode')();
5var forEach = require('for-each');
6var inspect = require('object-inspect');
7var v = require('es-value-fixtures');
8
9var 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 */
15var boundFnsHaveConfigurableLengths = require('set-function-length/env').boundFnsHaveConfigurableLengths;
16
17test('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 // eslint-disable-next-line no-invalid-this
29 return [!hasStrictMode && this === global ? undefined : this, a, b];
30 };
31 t.equal(func.length, 2, 'original function length is 2');
32 t.deepEqual(func(), [undefined, undefined, undefined], 'unbound func with too few args');
33 t.deepEqual(func(1, 2), [undefined, 1, 2], 'unbound func with right args');
34 t.deepEqual(func(1, 2, 3), [undefined, 1, 2], 'unbound func with too many args');
35
36 var bound = callBind(func);
37 t.equal(bound.length, func.length + 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
38 t.deepEqual(bound(), [undefined, undefined, undefined], 'bound func with too few args');
39 t.deepEqual(bound(1, 2), [hasStrictMode ? 1 : Object(1), 2, undefined], 'bound func with right args');
40 t.deepEqual(bound(1, 2, 3), [hasStrictMode ? 1 : Object(1), 2, 3], 'bound func with too many args');
41
42 var boundR = callBind(func, sentinel);
43 t.equal(boundR.length, func.length, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
44 t.deepEqual(boundR(), [sentinel, undefined, undefined], 'bound func with receiver, with too few args');
45 t.deepEqual(boundR(1, 2), [sentinel, 1, 2], 'bound func with receiver, with right args');
46 t.deepEqual(boundR(1, 2, 3), [sentinel, 1, 2], 'bound func with receiver, with too many args');
47
48 var boundArg = callBind(func, sentinel, 1);
49 t.equal(boundArg.length, func.length - 1, 'function length is preserved', { skip: !boundFnsHaveConfigurableLengths });
50 t.deepEqual(boundArg(), [sentinel, 1, undefined], 'bound func with receiver and arg, with too few args');
51 t.deepEqual(boundArg(2), [sentinel, 1, 2], 'bound func with receiver and arg, with right arg');
52 t.deepEqual(boundArg(2, 3), [sentinel, 1, 2], 'bound func with receiver and arg, with too many args');
53
54 t.test('callBind.apply', function (st) {
55 var aBound = callBind.apply(func);
56 st.deepEqual(aBound(sentinel), [sentinel, undefined, undefined], 'apply-bound func with no args');
57 st.deepEqual(aBound(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
58 st.deepEqual(aBound(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
59
60 var aBoundArg = callBind.apply(func);
61 st.deepEqual(aBoundArg(sentinel, [1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with too many args');
62 st.deepEqual(aBoundArg(sentinel, [1, 2], 4), [sentinel, 1, 2], 'apply-bound func with right args');
63 st.deepEqual(aBoundArg(sentinel, [1], 4), [sentinel, 1, undefined], 'apply-bound func with too few args');
64
65 var aBoundR = callBind.apply(func, sentinel);
66 st.deepEqual(aBoundR([1, 2, 3], 4), [sentinel, 1, 2], 'apply-bound func with receiver and too many args');
67 st.deepEqual(aBoundR([1, 2], 4), [sentinel, 1, 2], 'apply-bound func with receiver and right args');
68 st.deepEqual(aBoundR([1], 4), [sentinel, 1, undefined], 'apply-bound func with receiver and too few args');
69
70 st.end();
71 });
72
73 t.end();
74});
Note: See TracBrowser for help on using the repository browser.