index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. var test = require('tape');
  2. var functionBind = require('../implementation');
  3. var getCurrentContext = function () { return this; };
  4. test('functionBind is a function', function (t) {
  5. t.equal(typeof functionBind, 'function');
  6. t.end();
  7. });
  8. test('non-functions', function (t) {
  9. var nonFunctions = [true, false, [], {}, 42, 'foo', NaN, /a/g];
  10. t.plan(nonFunctions.length);
  11. for (var i = 0; i < nonFunctions.length; ++i) {
  12. try { functionBind.call(nonFunctions[i]); } catch (ex) {
  13. t.ok(ex instanceof TypeError, 'throws when given ' + String(nonFunctions[i]));
  14. }
  15. }
  16. t.end();
  17. });
  18. test('without a context', function (t) {
  19. t.test('binds properly', function (st) {
  20. var args, context;
  21. var namespace = {
  22. func: functionBind.call(function () {
  23. args = Array.prototype.slice.call(arguments);
  24. context = this;
  25. })
  26. };
  27. namespace.func(1, 2, 3);
  28. st.deepEqual(args, [1, 2, 3]);
  29. st.equal(context, getCurrentContext.call());
  30. st.end();
  31. });
  32. t.test('binds properly, and still supplies bound arguments', function (st) {
  33. var args, context;
  34. var namespace = {
  35. func: functionBind.call(function () {
  36. args = Array.prototype.slice.call(arguments);
  37. context = this;
  38. }, undefined, 1, 2, 3)
  39. };
  40. namespace.func(4, 5, 6);
  41. st.deepEqual(args, [1, 2, 3, 4, 5, 6]);
  42. st.equal(context, getCurrentContext.call());
  43. st.end();
  44. });
  45. t.test('returns properly', function (st) {
  46. var args;
  47. var namespace = {
  48. func: functionBind.call(function () {
  49. args = Array.prototype.slice.call(arguments);
  50. return this;
  51. }, null)
  52. };
  53. var context = namespace.func(1, 2, 3);
  54. st.equal(context, getCurrentContext.call(), 'returned context is namespaced context');
  55. st.deepEqual(args, [1, 2, 3], 'passed arguments are correct');
  56. st.end();
  57. });
  58. t.test('returns properly with bound arguments', function (st) {
  59. var args;
  60. var namespace = {
  61. func: functionBind.call(function () {
  62. args = Array.prototype.slice.call(arguments);
  63. return this;
  64. }, null, 1, 2, 3)
  65. };
  66. var context = namespace.func(4, 5, 6);
  67. st.equal(context, getCurrentContext.call(), 'returned context is namespaced context');
  68. st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'passed arguments are correct');
  69. st.end();
  70. });
  71. t.test('called as a constructor', function (st) {
  72. var thunkify = function (value) {
  73. return function () { return value; };
  74. };
  75. st.test('returns object value', function (sst) {
  76. var expectedReturnValue = [1, 2, 3];
  77. var Constructor = functionBind.call(thunkify(expectedReturnValue), null);
  78. var result = new Constructor();
  79. sst.equal(result, expectedReturnValue);
  80. sst.end();
  81. });
  82. st.test('does not return primitive value', function (sst) {
  83. var Constructor = functionBind.call(thunkify(42), null);
  84. var result = new Constructor();
  85. sst.notEqual(result, 42);
  86. sst.end();
  87. });
  88. st.test('object from bound constructor is instance of original and bound constructor', function (sst) {
  89. var A = function (x) {
  90. this.name = x || 'A';
  91. };
  92. var B = functionBind.call(A, null, 'B');
  93. var result = new B();
  94. sst.ok(result instanceof B, 'result is instance of bound constructor');
  95. sst.ok(result instanceof A, 'result is instance of original constructor');
  96. sst.end();
  97. });
  98. st.end();
  99. });
  100. t.end();
  101. });
  102. test('with a context', function (t) {
  103. t.test('with no bound arguments', function (st) {
  104. var args, context;
  105. var boundContext = {};
  106. var namespace = {
  107. func: functionBind.call(function () {
  108. args = Array.prototype.slice.call(arguments);
  109. context = this;
  110. }, boundContext)
  111. };
  112. namespace.func(1, 2, 3);
  113. st.equal(context, boundContext, 'binds a context properly');
  114. st.deepEqual(args, [1, 2, 3], 'supplies passed arguments');
  115. st.end();
  116. });
  117. t.test('with bound arguments', function (st) {
  118. var args, context;
  119. var boundContext = {};
  120. var namespace = {
  121. func: functionBind.call(function () {
  122. args = Array.prototype.slice.call(arguments);
  123. context = this;
  124. }, boundContext, 1, 2, 3)
  125. };
  126. namespace.func(4, 5, 6);
  127. st.equal(context, boundContext, 'binds a context properly');
  128. st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'supplies bound and passed arguments');
  129. st.end();
  130. });
  131. t.test('returns properly', function (st) {
  132. var boundContext = {};
  133. var args;
  134. var namespace = {
  135. func: functionBind.call(function () {
  136. args = Array.prototype.slice.call(arguments);
  137. return this;
  138. }, boundContext)
  139. };
  140. var context = namespace.func(1, 2, 3);
  141. st.equal(context, boundContext, 'returned context is bound context');
  142. st.notEqual(context, getCurrentContext.call(), 'returned context is not lexical context');
  143. st.deepEqual(args, [1, 2, 3], 'passed arguments are correct');
  144. st.end();
  145. });
  146. t.test('returns properly with bound arguments', function (st) {
  147. var boundContext = {};
  148. var args;
  149. var namespace = {
  150. func: functionBind.call(function () {
  151. args = Array.prototype.slice.call(arguments);
  152. return this;
  153. }, boundContext, 1, 2, 3)
  154. };
  155. var context = namespace.func(4, 5, 6);
  156. st.equal(context, boundContext, 'returned context is bound context');
  157. st.notEqual(context, getCurrentContext.call(), 'returned context is not lexical context');
  158. st.deepEqual(args, [1, 2, 3, 4, 5, 6], 'passed arguments are correct');
  159. st.end();
  160. });
  161. t.test('passes the correct arguments when called as a constructor', function (st) {
  162. var expected = { name: 'Correct' };
  163. var namespace = {
  164. Func: functionBind.call(function (arg) {
  165. return arg;
  166. }, { name: 'Incorrect' })
  167. };
  168. var returned = new namespace.Func(expected);
  169. st.equal(returned, expected, 'returns the right arg when called as a constructor');
  170. st.end();
  171. });
  172. t.test('has the new instance\'s context when called as a constructor', function (st) {
  173. var actualContext;
  174. var expectedContext = { foo: 'bar' };
  175. var namespace = {
  176. Func: functionBind.call(function () {
  177. actualContext = this;
  178. }, expectedContext)
  179. };
  180. var result = new namespace.Func();
  181. st.equal(result instanceof namespace.Func, true);
  182. st.notEqual(actualContext, expectedContext);
  183. st.end();
  184. });
  185. t.end();
  186. });
  187. test('bound function length', function (t) {
  188. t.test('sets a correct length without thisArg', function (st) {
  189. var subject = functionBind.call(function (a, b, c) { return a + b + c; });
  190. st.equal(subject.length, 3);
  191. st.equal(subject(1, 2, 3), 6);
  192. st.end();
  193. });
  194. t.test('sets a correct length with thisArg', function (st) {
  195. var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {});
  196. st.equal(subject.length, 3);
  197. st.equal(subject(1, 2, 3), 6);
  198. st.end();
  199. });
  200. t.test('sets a correct length without thisArg and first argument', function (st) {
  201. var subject = functionBind.call(function (a, b, c) { return a + b + c; }, undefined, 1);
  202. st.equal(subject.length, 2);
  203. st.equal(subject(2, 3), 6);
  204. st.end();
  205. });
  206. t.test('sets a correct length with thisArg and first argument', function (st) {
  207. var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {}, 1);
  208. st.equal(subject.length, 2);
  209. st.equal(subject(2, 3), 6);
  210. st.end();
  211. });
  212. t.test('sets a correct length without thisArg and too many arguments', function (st) {
  213. var subject = functionBind.call(function (a, b, c) { return a + b + c; }, undefined, 1, 2, 3, 4);
  214. st.equal(subject.length, 0);
  215. st.equal(subject(), 6);
  216. st.end();
  217. });
  218. t.test('sets a correct length with thisArg and too many arguments', function (st) {
  219. var subject = functionBind.call(function (a, b, c) { return a + b + c; }, {}, 1, 2, 3, 4);
  220. st.equal(subject.length, 0);
  221. st.equal(subject(), 6);
  222. st.end();
  223. });
  224. });