overloadTest.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. 'use strict';
  2. var overload = require('../');
  3. module.exports = {
  4. 'no parameters, valid args': function (test) {
  5. var called = 0;
  6. var fn = overload([
  7. [function () { called++; return 42; }]
  8. ]);
  9. var ret = fn();
  10. test.equals(called, 1);
  11. test.equals(ret, 42);
  12. test.done();
  13. },
  14. 'no parameters, invalid args': function (test) {
  15. var called = 0;
  16. var fn = overload([
  17. [function () { called++; }]
  18. ]);
  19. try {
  20. fn(1);
  21. test.fail('should throw exception');
  22. } catch (ex) {
  23. }
  24. test.done();
  25. },
  26. 'default callback': function (test) {
  27. var called = 0;
  28. var args = null;
  29. var fn = overload([
  30. [overload.func, function (fn) { test.fail('should not be called.'); }],
  31. function () {
  32. called++;
  33. args = Array.prototype.slice.call(arguments);
  34. }
  35. ]);
  36. fn('test');
  37. test.equals(called, 1, 'overload not called');
  38. test.deepEqual(args, ['test']);
  39. test.done();
  40. },
  41. 'one parameter, valid args': function (test) {
  42. var called = 0;
  43. var argFnCalled = 0;
  44. var fn = overload([
  45. [overload.func, function (fn) {
  46. called++;
  47. fn();
  48. }]
  49. ]);
  50. fn(function () { argFnCalled++; });
  51. test.equals(called, 1, 'overload not called');
  52. test.equals(argFnCalled, 1, 'arg function not called');
  53. test.done();
  54. },
  55. 'one parameter, invalid args': function (test) {
  56. var called = 0;
  57. var fn = overload([
  58. [overload.func, function () { called++; }]
  59. ]);
  60. try {
  61. fn(1);
  62. test.fail('should throw exception');
  63. } catch (ex) {
  64. }
  65. test.done();
  66. },
  67. 'optional parameter, valid args': function (test) {
  68. var called = 0;
  69. var argFnCalled = 0;
  70. var fn = overload([
  71. [overload.funcOptional, function (fn) {
  72. called++;
  73. fn();
  74. }]
  75. ]);
  76. fn(function () { argFnCalled++; });
  77. test.equals(called, 1, 'overload not called');
  78. test.equals(argFnCalled, 1, 'arg function not called');
  79. test.done();
  80. },
  81. 'optional parameter, no args': function (test) {
  82. var called = 0;
  83. var fn = overload([
  84. [overload.funcOptional, function (fn) {
  85. called++;
  86. if (fn) {
  87. test.fail('no function should be passed');
  88. }
  89. }]
  90. ]);
  91. fn();
  92. test.equals(called, 1, 'overload not called');
  93. test.done();
  94. },
  95. 'optional parameter, invalid args': function (test) {
  96. var called = 0;
  97. var fn = overload([
  98. [overload.funcOptional, function () { called++; }]
  99. ]);
  100. try {
  101. fn(1);
  102. test.fail('should throw exception');
  103. } catch (ex) {
  104. }
  105. test.done();
  106. },
  107. 'multiple overloads, valid args': function (test) {
  108. var called = 0;
  109. var val = null;
  110. var fn = overload([
  111. [overload.number, function (n) { test.fail('this function should not be called'); }],
  112. [overload.string, function (s) {
  113. val = s;
  114. called++;
  115. }]
  116. ]);
  117. fn('test');
  118. test.equals(called, 1, 'overload not called');
  119. test.equals(val, 'test', 'overload called with wrong value');
  120. test.done();
  121. },
  122. 'multiple overloads, invalid args': function (test) {
  123. var called = 0;
  124. var fn = overload([
  125. [overload.number, function (n) { test.fail('this function should not be called'); }],
  126. [overload.string, function (s) { called++; }]
  127. ]);
  128. try {
  129. fn(function () {});
  130. test.fail('should throw exception');
  131. } catch (ex) {
  132. }
  133. test.done();
  134. },
  135. 'optional parameter, with default value': function (test) {
  136. var called = 0;
  137. var val = null;
  138. var fn = overload([
  139. [overload.numberOptionalWithDefault(5), overload.func, function (n) {
  140. called++;
  141. val = n;
  142. }]
  143. ]);
  144. fn(function() {});
  145. test.equals(called, 1, 'overload not called');
  146. test.equals(val, 5);
  147. test.done();
  148. },
  149. 'number optional parameter, called with non-zero number': function (test) {
  150. var called = 0;
  151. var val = null;
  152. var fn = overload([
  153. [overload.numberOptionalWithDefault(5), overload.func, function (n) {
  154. called++;
  155. val = n;
  156. }]
  157. ]);
  158. fn(3, function() {});
  159. test.equals(called, 1, 'overload not called');
  160. test.equals(val, 3);
  161. test.done();
  162. },
  163. 'number optional parameter, called with zero': function (test) {
  164. var called = 0;
  165. var val = null;
  166. var fn = overload([
  167. [overload.numberOptionalWithDefault(5), overload.func, function (n) {
  168. called++;
  169. val = n;
  170. }]
  171. ]);
  172. fn(0, function() {});
  173. test.equals(called, 1, 'overload not called');
  174. test.equals(val, 0);
  175. test.done();
  176. },
  177. 'string optional parameter, called with empty string': function (test) {
  178. var called = 0;
  179. var val = null;
  180. var fn = overload([
  181. [overload.stringOptionalWithDefault("hello world"), overload.func, function (n) {
  182. called++;
  183. val = n;
  184. }]
  185. ]);
  186. fn("", function() {});
  187. test.equals(called, 1, 'overload not called');
  188. test.equals(val, "");
  189. test.done();
  190. },
  191. 'callback, with default value': function (test) {
  192. var called = 0;
  193. var fn = overload([
  194. [overload.callbackOptional, function (callback) {
  195. callback();
  196. }]
  197. ]);
  198. fn(function () { called++; });
  199. test.equals(called, 1, 'overload not called');
  200. test.done();
  201. },
  202. 'multiple optionals': function (test) {
  203. var called = 0;
  204. var args;
  205. var fn = overload([
  206. [overload.string, overload.arrayOptionalWithDefault(null), overload.callbackOptional, function (str, arr, callback) {
  207. args = arguments;
  208. called++;
  209. }]
  210. ]);
  211. fn('test');
  212. test.equals(called, 1, 'overload not called');
  213. test.equals(args.length, 3);
  214. test.equals(args[0], 'test');
  215. test.ok(args[1] === null);
  216. test.equals(typeof(args[2]), 'function');
  217. test.done();
  218. },
  219. 'multiple optionals, skipping middle one': function (test) {
  220. var called = 0;
  221. var args;
  222. var fn = overload([
  223. [overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
  224. args = arguments;
  225. called++;
  226. }]
  227. ]);
  228. var fnTest = function zzz() {};
  229. fn('test', fnTest);
  230. test.equals(called, 1, 'overload not called');
  231. test.equals(args.length, 3);
  232. test.equals(args[0], 'test');
  233. test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
  234. test.equals(args[2].name, 'zzz');
  235. test.done();
  236. },
  237. 'multiple optionals, null middle one': function (test) {
  238. var called = 0;
  239. var args;
  240. var fn = overload([
  241. [overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
  242. args = arguments;
  243. called++;
  244. }]
  245. ]);
  246. var fnTest = function zzz() {};
  247. fn('test', null, fnTest);
  248. test.equals(called, 1, 'overload not called');
  249. test.equals(args.length, 3);
  250. test.equals(args[0], 'test');
  251. test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
  252. test.equals(args[2].name, 'zzz');
  253. test.done();
  254. },
  255. 'complex': function (test) {
  256. var called = 0;
  257. var args = null;
  258. var fn = overload([
  259. [overload.string, function () { test.fail('this function should not be called'); }],
  260. [overload.string, overload.number, overload.funcOptional, function () {
  261. args = Array.prototype.slice.call(arguments, 0);
  262. called++;
  263. }]
  264. ]);
  265. var argFn = function () {};
  266. fn('test', 5, argFn);
  267. test.equals(called, 1, 'overload not called');
  268. test.deepEqual(args, ['test', 5, argFn], 'overload called with wrong args');
  269. test.done();
  270. }
  271. };