errors.test.js (8095B)
1 var assert = require('assert'); 2 var typed = require('../typed-function'); 3 4 describe('errors', function () { 5 it('should give correct error in case of too few arguments (named function)', function() { 6 var fn = typed('fn1', {'string, boolean': function () {}}); 7 8 assert.throws(function () {fn()}, /TypeError: Too few arguments in function fn1 \(expected: string, index: 0\)/); 9 assert.throws(function () {fn('foo')}, /TypeError: Too few arguments in function fn1 \(expected: boolean, index: 1\)/); 10 }); 11 12 it('should give correct error in case of too few arguments (unnamed function)', function() { 13 var fn = typed({'string, boolean': function () {}}); 14 15 assert.throws(function () {fn()}, /TypeError: Too few arguments in function unnamed \(expected: string, index: 0\)/); 16 assert.throws(function () {fn('foo')}, /TypeError: Too few arguments in function unnamed \(expected: boolean, index: 1\)/); 17 }); 18 19 it('should give correct error in case of too few arguments (rest params)', function() { 20 var fn = typed({'...string': function () {}}); 21 22 assert.throws(function () {fn()}, /TypeError: Too few arguments in function unnamed \(expected: string, index: 0\)/); 23 }); 24 25 it('should give correct error in case of too few arguments (rest params) (2)', function() { 26 var fn = typed({'boolean, ...string': function () {}}); 27 28 assert.throws(function () {fn()}, /TypeError: Too few arguments in function unnamed \(expected: boolean, index: 0\)/); 29 assert.throws(function () {fn(true)}, /TypeError: Too few arguments in function unnamed \(expected: string, index: 1\)/); 30 }); 31 32 it('should give correct error in case of too many arguments (unnamed function)', function() { 33 var fn = typed({'string, boolean': function () {}}); 34 35 assert.throws(function () {fn('foo', true, 2)}, /TypeError: Too many arguments in function unnamed \(expected: 2, actual: 3\)/); 36 assert.throws(function () {fn('foo', true, 2, 1)}, /TypeError: Too many arguments in function unnamed \(expected: 2, actual: 4\)/); 37 }); 38 39 it('should give correct error in case of too many arguments (named function)', function() { 40 var fn = typed('fn2', {'string, boolean': function () {}}); 41 42 assert.throws(function () {fn('foo', true, 2)}, /TypeError: Too many arguments in function fn2 \(expected: 2, actual: 3\)/); 43 assert.throws(function () {fn('foo', true, 2, 1)}, /TypeError: Too many arguments in function fn2 \(expected: 2, actual: 4\)/); 44 }); 45 46 it('should give correct error in case of wrong type of argument (unnamed function)', function() { 47 var fn = typed({'boolean': function () {}}); 48 49 assert.throws(function () {fn('foo')}, /TypeError: Unexpected type of argument in function unnamed \(expected: boolean, actual: string, index: 0\)/); 50 }); 51 52 it('should give correct error in case of wrong type of argument (named function)', function() { 53 var fn = typed('fn3', {'boolean': function () {}}); 54 55 assert.throws(function () {fn('foo')}, /TypeError: Unexpected type of argument in function fn3 \(expected: boolean, actual: string, index: 0\)/); 56 }); 57 58 it('should give correct error in case of wrong type of argument (union args)', function() { 59 var fn = typed({'boolean | string | Date': function () {}}); 60 61 assert.throws(function () {fn(2)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string or boolean or Date, actual: number, index: 0\)/); 62 }); 63 64 it('should give correct error in case of conflicting union arguments', function() { 65 assert.throws(function () { 66 var fn = typed({ 67 'string | number': function () {}, 68 'string': function () {} 69 }); 70 }, /TypeError: Conflicting signatures "string\|number" and "string"/); 71 }); 72 73 it('should give correct error in case of conflicting union arguments (2)', function() { 74 assert.throws(function () { 75 var fn = typed({ 76 '...string | number': function () {}, 77 '...string': function () {} 78 }); 79 }, /TypeError: Conflicting signatures "...string\|number" and "...string"/); 80 }); 81 82 it('should give correct error in case of conflicting rest params (1)', function() { 83 assert.throws(function () { 84 var fn = typed({ 85 '...string': function () {}, 86 'string': function () {} 87 }); 88 }, /TypeError: Conflicting signatures "...string" and "string"/); 89 }); 90 91 it('should give correct error in case of conflicting rest params (2)', function() { 92 // should not throw 93 var fn = typed({ 94 '...string': function () {}, 95 'string, number': function () {} 96 }); 97 98 assert.throws(function () { 99 var fn = typed({ 100 '...string': function () {}, 101 'string, string': function () {} 102 }); 103 }, /TypeError: Conflicting signatures "...string" and "string,string"/); 104 }); 105 106 it('should give correct error in case of conflicting rest params (3)', function() { 107 assert.throws(function () { 108 var fn = typed({ 109 '...number|string': function () {}, 110 'number, string': function () {} 111 }); 112 }, /TypeError: Conflicting signatures "...number\|string" and "number,string"/); 113 }); 114 115 it('should give correct error in case of wrong type of argument (rest params)', function() { 116 var fn = typed({'...number': function () {}}); 117 118 assert.throws(function () {fn(true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 0\)/); 119 assert.throws(function () {fn(2, true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 1\)/); 120 assert.throws(function () {fn(2, 3, true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 2\)/); 121 }); 122 123 it('should give correct error in case of wrong type of argument (nested rest params)', function() { 124 var fn = typed({'string, ...number': function () {}}); 125 126 assert.throws(function () {fn(true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string, actual: boolean, index: 0\)/); 127 assert.throws(function () {fn('foo', true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 1\)/); 128 assert.throws(function () {fn('foo', 2, true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 2\)/); 129 assert.throws(function () {fn('foo', 2, 3, true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: number, actual: boolean, index: 3\)/); 130 }); 131 132 it('should give correct error in case of wrong type of argument (union and rest params)', function() { 133 var fn = typed({'...number|boolean': function () {}}); 134 135 assert.throws(function () {fn('foo')}, /TypeError: Unexpected type of argument in function unnamed \(expected: number or boolean, actual: string, index: 0\)/); 136 assert.throws(function () {fn(2, 'foo')}, /TypeError: Unexpected type of argument in function unnamed \(expected: number or boolean, actual: string, index: 1\)/); 137 assert.throws(function () {fn(2, true, 'foo')}, /TypeError: Unexpected type of argument in function unnamed \(expected: number or boolean, actual: string, index: 2\)/); 138 }); 139 140 it('should only list matches of exact and convertable types', function() { 141 var typed2 = typed.create(); 142 typed2.conversions.push({ 143 from: 'number', 144 to: 'string', 145 convert: function (x) { 146 return +x; 147 } 148 }); 149 150 var fn1 = typed2({'string': function () {}}); 151 var fn2 = typed2({'...string': function () {}}); 152 153 assert.throws(function () {fn1(true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string or number, actual: boolean, index: 0\)/); 154 assert.throws(function () {fn2(true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string or number, actual: boolean, index: 0\)/); 155 assert.throws(function () {fn2(2, true)}, /TypeError: Unexpected type of argument in function unnamed \(expected: string or number, actual: boolean, index: 1\)/); 156 }); 157 });