1 // jscs:disable requireUseStrict
3 var test = require('tape');
5 var functionBind = require('../implementation');
6 var getCurrentContext = function () { return this; };
8 test('functionBind is a function', function (t) {
9 t.equal(typeof functionBind, 'function');
13 test('non-functions', function (t) {
14 var nonFunctions = [true, false, [], {}, 42, 'foo', NaN, /a/g];
15 t.plan(nonFunctions.length);
16 for (var i = 0; i < nonFunctions.length; ++i) {
17 try { functionBind.call(nonFunctions[i]); } catch (ex) {
18 t.ok(ex instanceof TypeError, 'throws when given ' + String(nonFunctions[i]));
24 test('without a context', function (t) {
25 t.test('binds properly', function (st) {
28 func: functionBind.call(function () {
29 args = Array.prototype.slice.call(arguments);
33 namespace.func(1, 2, 3);
34 st.deepEqual(args, [1, 2, 3]);
35 st.equal(context, getCurrentContext.call());
39 t.test('binds properly, and still supplies bound arguments', function (st) {
42 func: functionBind.call(function () {
43 args = Array.prototype.slice.call(arguments);
45 }, undefined, 1, 2, 3)
47 namespace.func(4, 5, 6);
48 st.deepEqual(args, [1, 2, 3, 4, 5, 6]);
49 st.equal(context, getCurrentContext.call());
53 t.test('returns properly', function (st) {
56 func: functionBind.call(function () {
57 args = Array.prototype.slice.call(arguments);
61 var context = namespace.func(1, 2, 3);
62 st.equal(context, getCurrentContext.call(), 'returned context is namespaced context');
63 st.deepEqual(args, [1, 2, 3], 'passed arguments are correct');
67 t.test('returns properly with bound arguments', function (st) {
70 func: functionBind.call(function () {
71 args = Array.prototype.slice.call(arguments);
75 var context = namespace.func(4, 5, 6);
76 st.equal(context, getCurrentContext.call(), 'returned context is namespaced context');
77 st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'passed arguments are correct');
81 t.test('called as a constructor', function (st) {
82 var thunkify = function (value) {
83 return function () { return value; };
85 st.test('returns object value', function (sst) {
86 var expectedReturnValue = [1, 2, 3];
87 var Constructor = functionBind.call(thunkify(expectedReturnValue), null);
88 var result = new Constructor();
89 sst.equal(result, expectedReturnValue);
93 st.test('does not return primitive value', function (sst) {
94 var Constructor = functionBind.call(thunkify(42), null);
95 var result = new Constructor();
96 sst.notEqual(result, 42);
100 st.test('object from bound constructor is instance of original and bound constructor', function (sst) {
101 var A = function (x) {
102 this.name = x || 'A';
104 var B = functionBind.call(A, null, 'B');
106 var result = new B();
107 sst.ok(result instanceof B, 'result is instance of bound constructor');
108 sst.ok(result instanceof A, 'result is instance of original constructor');
118 test('with a context', function (t) {
119 t.test('with no bound arguments', function (st) {
121 var boundContext = {};
123 func: functionBind.call(function () {
124 args = Array.prototype.slice.call(arguments);
128 namespace.func(1, 2, 3);
129 st.equal(context, boundContext, 'binds a context properly');
130 st.deepEqual(args, [1, 2, 3], 'supplies passed arguments');
134 t.test('with bound arguments', function (st) {
136 var boundContext = {};
138 func: functionBind.call(function () {
139 args = Array.prototype.slice.call(arguments);
141 }, boundContext, 1, 2, 3)
143 namespace.func(4, 5, 6);
144 st.equal(context, boundContext, 'binds a context properly');
145 st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'supplies bound and passed arguments');
149 t.test('returns properly', function (st) {
150 var boundContext = {};
153 func: functionBind.call(function () {
154 args = Array.prototype.slice.call(arguments);
158 var context = namespace.func(1, 2, 3);
159 st.equal(context, boundContext, 'returned context is bound context');
160 st.notEqual(context, getCurrentContext.call(), 'returned context is not lexical context');
161 st.deepEqual(args, [1, 2, 3], 'passed arguments are correct');
165 t.test('returns properly with bound arguments', function (st) {
166 var boundContext = {};
169 func: functionBind.call(function () {
170 args = Array.prototype.slice.call(arguments);
172 }, boundContext, 1, 2, 3)
174 var context = namespace.func(4, 5, 6);
175 st.equal(context, boundContext, 'returned context is bound context');
176 st.notEqual(context, getCurrentContext.call(), 'returned context is not lexical context');
177 st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'passed arguments are correct');
181 t.test('passes the correct arguments when called as a constructor', function (st) {
182 var expected = { name: 'Correct' };
184 Func: functionBind.call(function (arg) {
186 }, { name: 'Incorrect' })
188 var returned = new namespace.Func(expected);
189 st.equal(returned, expected, 'returns the right arg when called as a constructor');
193 t.test('has the new instance\'s context when called as a constructor', function (st) {
195 var expectedContext = { foo: 'bar' };
197 Func: functionBind.call(function () {
198 actualContext = this;
201 var result = new namespace.Func();
202 st.equal(result instanceof namespace.Func, true);
203 st.notEqual(actualContext, expectedContext);
210 test('bound function length', function (t) {
211 t.test('sets a correct length without thisArg', function (st) {
212 var subject = functionBind.call(function (a, b, c) { return a + b + c; });
213 st.equal(subject.length, 3);
214 st.equal(subject(1, 2, 3), 6);
218 t.test('sets a correct length with thisArg', function (st) {
219 var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {});
220 st.equal(subject.length, 3);
221 st.equal(subject(1, 2, 3), 6);
225 t.test('sets a correct length without thisArg and first argument', function (st) {
226 var subject = functionBind.call(function (a, b, c) { return a + b + c; }, undefined, 1);
227 st.equal(subject.length, 2);
228 st.equal(subject(2, 3), 6);
232 t.test('sets a correct length with thisArg and first argument', function (st) {
233 var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {}, 1);
234 st.equal(subject.length, 2);
235 st.equal(subject(2, 3), 6);
239 t.test('sets a correct length without thisArg and too many arguments', function (st) {
240 var subject = functionBind.call(function (a, b, c) { return a + b + c; }, undefined, 1, 2, 3, 4);
241 st.equal(subject.length, 0);
242 st.equal(subject(), 6);
246 t.test('sets a correct length with thisArg and too many arguments', function (st) {
247 var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {}, 1, 2, 3, 4);
248 st.equal(subject.length, 0);
249 st.equal(subject(), 6);