| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- 'use strict';
- var overload = require('../');
- module.exports = {
- 'no parameters, valid args': function (test) {
- var called = 0;
- var fn = overload([
- [function () { called++; return 42; }]
- ]);
- var ret = fn();
- test.equals(called, 1);
- test.equals(ret, 42);
- test.done();
- },
- 'no parameters, invalid args': function (test) {
- var called = 0;
- var fn = overload([
- [function () { called++; }]
- ]);
- try {
- fn(1);
- test.fail('should throw exception');
- } catch (ex) {
- }
- test.done();
- },
- 'default callback': function (test) {
- var called = 0;
- var args = null;
- var fn = overload([
- [overload.func, function (fn) { test.fail('should not be called.'); }],
- function () {
- called++;
- args = Array.prototype.slice.call(arguments);
- }
- ]);
- fn('test');
- test.equals(called, 1, 'overload not called');
- test.deepEqual(args, ['test']);
- test.done();
- },
- 'one parameter, valid args': function (test) {
- var called = 0;
- var argFnCalled = 0;
- var fn = overload([
- [overload.func, function (fn) {
- called++;
- fn();
- }]
- ]);
- fn(function () { argFnCalled++; });
- test.equals(called, 1, 'overload not called');
- test.equals(argFnCalled, 1, 'arg function not called');
- test.done();
- },
- 'one parameter, invalid args': function (test) {
- var called = 0;
- var fn = overload([
- [overload.func, function () { called++; }]
- ]);
- try {
- fn(1);
- test.fail('should throw exception');
- } catch (ex) {
- }
- test.done();
- },
- 'optional parameter, valid args': function (test) {
- var called = 0;
- var argFnCalled = 0;
- var fn = overload([
- [overload.funcOptional, function (fn) {
- called++;
- fn();
- }]
- ]);
- fn(function () { argFnCalled++; });
- test.equals(called, 1, 'overload not called');
- test.equals(argFnCalled, 1, 'arg function not called');
- test.done();
- },
- 'optional parameter, no args': function (test) {
- var called = 0;
- var fn = overload([
- [overload.funcOptional, function (fn) {
- called++;
- if (fn) {
- test.fail('no function should be passed');
- }
- }]
- ]);
- fn();
- test.equals(called, 1, 'overload not called');
- test.done();
- },
- 'optional parameter, invalid args': function (test) {
- var called = 0;
- var fn = overload([
- [overload.funcOptional, function () { called++; }]
- ]);
- try {
- fn(1);
- test.fail('should throw exception');
- } catch (ex) {
- }
- test.done();
- },
- 'multiple overloads, valid args': function (test) {
- var called = 0;
- var val = null;
- var fn = overload([
- [overload.number, function (n) { test.fail('this function should not be called'); }],
- [overload.string, function (s) {
- val = s;
- called++;
- }]
- ]);
- fn('test');
- test.equals(called, 1, 'overload not called');
- test.equals(val, 'test', 'overload called with wrong value');
- test.done();
- },
- 'multiple overloads, invalid args': function (test) {
- var called = 0;
- var fn = overload([
- [overload.number, function (n) { test.fail('this function should not be called'); }],
- [overload.string, function (s) { called++; }]
- ]);
- try {
- fn(function () {});
- test.fail('should throw exception');
- } catch (ex) {
- }
- test.done();
- },
- 'optional parameter, with default value': function (test) {
- var called = 0;
- var val = null;
- var fn = overload([
- [overload.numberOptionalWithDefault(5), overload.func, function (n) {
- called++;
- val = n;
- }]
- ]);
- fn(function() {});
- test.equals(called, 1, 'overload not called');
- test.equals(val, 5);
- test.done();
- },
- 'number optional parameter, called with non-zero number': function (test) {
- var called = 0;
- var val = null;
- var fn = overload([
- [overload.numberOptionalWithDefault(5), overload.func, function (n) {
- called++;
- val = n;
- }]
- ]);
- fn(3, function() {});
- test.equals(called, 1, 'overload not called');
- test.equals(val, 3);
- test.done();
- },
- 'number optional parameter, called with zero': function (test) {
- var called = 0;
- var val = null;
- var fn = overload([
- [overload.numberOptionalWithDefault(5), overload.func, function (n) {
- called++;
- val = n;
- }]
- ]);
- fn(0, function() {});
- test.equals(called, 1, 'overload not called');
- test.equals(val, 0);
- test.done();
- },
- 'string optional parameter, called with empty string': function (test) {
- var called = 0;
- var val = null;
- var fn = overload([
- [overload.stringOptionalWithDefault("hello world"), overload.func, function (n) {
- called++;
- val = n;
- }]
- ]);
- fn("", function() {});
- test.equals(called, 1, 'overload not called');
- test.equals(val, "");
- test.done();
- },
- 'callback, with default value': function (test) {
- var called = 0;
- var fn = overload([
- [overload.callbackOptional, function (callback) {
- callback();
- }]
- ]);
- fn(function () { called++; });
- test.equals(called, 1, 'overload not called');
- test.done();
- },
- 'multiple optionals': function (test) {
- var called = 0;
- var args;
- var fn = overload([
- [overload.string, overload.arrayOptionalWithDefault(null), overload.callbackOptional, function (str, arr, callback) {
- args = arguments;
- called++;
- }]
- ]);
- fn('test');
- test.equals(called, 1, 'overload not called');
- test.equals(args.length, 3);
- test.equals(args[0], 'test');
- test.ok(args[1] === null);
- test.equals(typeof(args[2]), 'function');
- test.done();
- },
- 'multiple optionals, skipping middle one': function (test) {
- var called = 0;
- var args;
- var fn = overload([
- [overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
- args = arguments;
- called++;
- }]
- ]);
- var fnTest = function zzz() {};
- fn('test', fnTest);
- test.equals(called, 1, 'overload not called');
- test.equals(args.length, 3);
- test.equals(args[0], 'test');
- test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
- test.equals(args[2].name, 'zzz');
- test.done();
- },
- 'multiple optionals, null middle one': function (test) {
- var called = 0;
- var args;
- var fn = overload([
- [overload.string, overload.arrayOptional, overload.callbackOptional, function (str, arr, callback) {
- args = arguments;
- called++;
- }]
- ]);
- var fnTest = function zzz() {};
- fn('test', null, fnTest);
- test.equals(called, 1, 'overload not called');
- test.equals(args.length, 3);
- test.equals(args[0], 'test');
- test.ok(args[1] === undefined, 'invalid argument, expected undefined: ' + args[1]);
- test.equals(args[2].name, 'zzz');
- test.done();
- },
- 'complex': function (test) {
- var called = 0;
- var args = null;
- var fn = overload([
- [overload.string, function () { test.fail('this function should not be called'); }],
- [overload.string, overload.number, overload.funcOptional, function () {
- args = Array.prototype.slice.call(arguments, 0);
- called++;
- }]
- ]);
- var argFn = function () {};
- fn('test', 5, argFn);
- test.equals(called, 1, 'overload not called');
- test.deepEqual(args, ['test', 5, argFn], 'overload called with wrong args');
- test.done();
- }
- };
|