|
| 1 | +/* global describe, it */ |
| 2 | + |
| 3 | +const argsert = require('../lib/argsert') |
| 4 | +const expect = require('chai').expect |
| 5 | + |
| 6 | +require('chai').should() |
| 7 | + |
| 8 | +describe('Argsert', function () { |
| 9 | + it('does not throw exception if optional argument is not provided', function () { |
| 10 | + argsert('[object]', [].slice.call(arguments)) |
| 11 | + }) |
| 12 | + |
| 13 | + it('throws exception if wrong type is provided for optional argument', function () { |
| 14 | + function foo (opts) { |
| 15 | + argsert('[object|number]', [].slice.call(arguments)) |
| 16 | + } |
| 17 | + expect(function () { |
| 18 | + foo('hello') |
| 19 | + }).to.throw(/Invalid first argument. Expected object or number but received string./) |
| 20 | + }) |
| 21 | + |
| 22 | + it('does not throw exception if optional argument is valid', function () { |
| 23 | + function foo (opts) { |
| 24 | + argsert('[object]', [].slice.call(arguments)) |
| 25 | + } |
| 26 | + foo({foo: 'bar'}) |
| 27 | + }) |
| 28 | + |
| 29 | + it('throws exception if required argument is not provided', function () { |
| 30 | + expect(function () { |
| 31 | + argsert('<object>', [].slice.call(arguments)) |
| 32 | + }).to.throw(/Not enough arguments provided. Expected 1 but received 0./) |
| 33 | + }) |
| 34 | + |
| 35 | + it('throws exception if required argument is of wrong type', function () { |
| 36 | + function foo (opts) { |
| 37 | + argsert('<object>', [].slice.call(arguments)) |
| 38 | + } |
| 39 | + expect(function () { |
| 40 | + foo('bar') |
| 41 | + }).to.throw(/Invalid first argument. Expected object but received string./) |
| 42 | + }) |
| 43 | + |
| 44 | + it('supports a combination of required and optional arguments', function () { |
| 45 | + function foo (opts) { |
| 46 | + argsert('<array> <string|object> [string|object]', [].slice.call(arguments)) |
| 47 | + } |
| 48 | + foo([], 'foo', {}) |
| 49 | + }) |
| 50 | + |
| 51 | + it('throws an exception if too many arguments are provided', function () { |
| 52 | + function foo (expected) { |
| 53 | + argsert('<array> [batman]', [].slice.call(arguments)) |
| 54 | + } |
| 55 | + expect(function () { |
| 56 | + foo([], 33, 99) |
| 57 | + }).to.throw(/Too many arguments provided. Expected max 2 but received 3./) |
| 58 | + }) |
| 59 | + |
| 60 | + it('configures function to accept 0 parameters, if only arguments object is provided', function () { |
| 61 | + function foo (expected) { |
| 62 | + argsert([].slice.call(arguments)) |
| 63 | + } |
| 64 | + expect(function () { |
| 65 | + foo(99) |
| 66 | + }).to.throw(/Too many arguments provided. Expected max 0 but received 1./) |
| 67 | + }) |
| 68 | + |
| 69 | + it('allows for any type if * is provided', function () { |
| 70 | + function foo (opts) { |
| 71 | + argsert('<*>', [].slice.call(arguments)) |
| 72 | + } |
| 73 | + foo('bar') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should ignore trailing undefined values', function () { |
| 77 | + function foo (opts) { |
| 78 | + argsert('<*>', [].slice.call(arguments)) |
| 79 | + } |
| 80 | + foo('bar', undefined, undefined) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should not ignore undefined values that are not trailing', function () { |
| 84 | + function foo (opts) { |
| 85 | + argsert('<*>', [].slice.call(arguments)) |
| 86 | + } |
| 87 | + expect(function () { |
| 88 | + foo('bar', undefined, undefined, 33) |
| 89 | + }).to.throw(/Too many arguments provided. Expected max 1 but received 4./) |
| 90 | + }) |
| 91 | + |
| 92 | + it('supports null as special type', function () { |
| 93 | + function foo (arg) { |
| 94 | + argsert('<null>', [].slice.call(arguments)) |
| 95 | + } |
| 96 | + foo(null) |
| 97 | + }) |
| 98 | +}) |
0 commit comments