|
1 | 1 | /* global describe, it */ |
2 | 2 |
|
3 | 3 | const argsert = require('../lib/argsert') |
4 | | -const expect = require('chai').expect |
| 4 | +const checkOutput = require('./helpers/utils').checkOutput |
5 | 5 |
|
6 | 6 | require('chai').should() |
7 | 7 |
|
8 | 8 | describe('Argsert', function () { |
9 | | - it('does not throw exception if optional argument is not provided', function () { |
10 | | - argsert('[object]', [].slice.call(arguments)) |
| 9 | + it('does not warn if optional argument is not provided', function () { |
| 10 | + var o = checkOutput(function () { |
| 11 | + argsert('[object]', [].slice.call(arguments)) |
| 12 | + }) |
| 13 | + |
| 14 | + o.warnings.length.should.equal(0) |
11 | 15 | }) |
12 | 16 |
|
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 () { |
| 17 | + it('warn if wrong type is provided for optional argument', function () { |
| 18 | + var o = checkOutput(function () { |
| 19 | + function foo (opts) { |
| 20 | + argsert('[object|number]', [].slice.call(arguments)) |
| 21 | + } |
| 22 | + |
18 | 23 | foo('hello') |
19 | | - }).to.throw(/Invalid first argument. Expected object or number but received string./) |
| 24 | + }) |
| 25 | + |
| 26 | + o.warnings[0].should.match(/Invalid first argument. Expected object or number but received string./) |
20 | 27 | }) |
21 | 28 |
|
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'}) |
| 29 | + it('does not warn if optional argument is valid', function () { |
| 30 | + var o = checkOutput(function () { |
| 31 | + function foo (opts) { |
| 32 | + argsert('[object]', [].slice.call(arguments)) |
| 33 | + } |
| 34 | + |
| 35 | + foo({foo: 'bar'}) |
| 36 | + }) |
| 37 | + |
| 38 | + o.warnings.length.should.equal(0) |
27 | 39 | }) |
28 | 40 |
|
29 | | - it('throws exception if required argument is not provided', function () { |
30 | | - expect(function () { |
| 41 | + it('warns if required argument is not provided', function () { |
| 42 | + var o = checkOutput(function () { |
31 | 43 | argsert('<object>', [].slice.call(arguments)) |
32 | | - }).to.throw(/Not enough arguments provided. Expected 1 but received 0./) |
| 44 | + }) |
| 45 | + |
| 46 | + o.warnings[0].should.match(/Not enough arguments provided. Expected 1 but received 0./) |
33 | 47 | }) |
34 | 48 |
|
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 () { |
| 49 | + it('warns if required argument is of wrong type', function () { |
| 50 | + var o = checkOutput(function () { |
| 51 | + function foo (opts) { |
| 52 | + argsert('<object>', [].slice.call(arguments)) |
| 53 | + } |
| 54 | + |
40 | 55 | foo('bar') |
41 | | - }).to.throw(/Invalid first argument. Expected object but received string./) |
| 56 | + }) |
| 57 | + |
| 58 | + o.warnings[0].should.match(/Invalid first argument. Expected object but received string./) |
42 | 59 | }) |
43 | 60 |
|
44 | 61 | 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', {}) |
| 62 | + var o = checkOutput(function () { |
| 63 | + function foo (opts) { |
| 64 | + argsert('<array> <string|object> [string|object]', [].slice.call(arguments)) |
| 65 | + } |
| 66 | + |
| 67 | + foo([], 'foo', {}) |
| 68 | + }) |
| 69 | + |
| 70 | + o.warnings.length.should.equal(0) |
49 | 71 | }) |
50 | 72 |
|
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 () { |
| 73 | + it('warns if too many arguments are provided', function () { |
| 74 | + var o = checkOutput(function () { |
| 75 | + function foo (expected) { |
| 76 | + argsert('<array> [batman]', [].slice.call(arguments)) |
| 77 | + } |
| 78 | + |
56 | 79 | foo([], 33, 99) |
57 | | - }).to.throw(/Too many arguments provided. Expected max 2 but received 3./) |
| 80 | + }) |
| 81 | + |
| 82 | + o.warnings[0].should.match(/Too many arguments provided. Expected max 2 but received 3./) |
58 | 83 | }) |
59 | 84 |
|
60 | 85 | 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 () { |
| 86 | + var o = checkOutput(function () { |
| 87 | + function foo (expected) { |
| 88 | + argsert([].slice.call(arguments)) |
| 89 | + } |
| 90 | + |
65 | 91 | foo(99) |
66 | | - }).to.throw(/Too many arguments provided. Expected max 0 but received 1./) |
| 92 | + }) |
| 93 | + |
| 94 | + o.warnings[0].should.match(/Too many arguments provided. Expected max 0 but received 1./) |
67 | 95 | }) |
68 | 96 |
|
69 | 97 | it('allows for any type if * is provided', function () { |
70 | | - function foo (opts) { |
71 | | - argsert('<*>', [].slice.call(arguments)) |
72 | | - } |
73 | | - foo('bar') |
| 98 | + var o = checkOutput(function () { |
| 99 | + function foo (opts) { |
| 100 | + argsert('<*>', [].slice.call(arguments)) |
| 101 | + } |
| 102 | + |
| 103 | + foo('bar') |
| 104 | + }) |
| 105 | + |
| 106 | + o.warnings.length.should.equal(0) |
74 | 107 | }) |
75 | 108 |
|
76 | 109 | it('should ignore trailing undefined values', function () { |
77 | | - function foo (opts) { |
78 | | - argsert('<*>', [].slice.call(arguments)) |
79 | | - } |
80 | | - foo('bar', undefined, undefined) |
| 110 | + var o = checkOutput(function () { |
| 111 | + function foo (opts) { |
| 112 | + argsert('<*>', [].slice.call(arguments)) |
| 113 | + } |
| 114 | + |
| 115 | + foo('bar', undefined, undefined) |
| 116 | + }) |
| 117 | + |
| 118 | + o.warnings.length.should.equal(0) |
81 | 119 | }) |
82 | 120 |
|
83 | 121 | it('should not ignore undefined values that are not trailing', function () { |
84 | | - function foo (opts) { |
85 | | - argsert('<*>', [].slice.call(arguments)) |
86 | | - } |
87 | | - expect(function () { |
| 122 | + var o = checkOutput(function () { |
| 123 | + function foo (opts) { |
| 124 | + argsert('<*>', [].slice.call(arguments)) |
| 125 | + } |
| 126 | + |
88 | 127 | foo('bar', undefined, undefined, 33) |
89 | | - }).to.throw(/Too many arguments provided. Expected max 1 but received 4./) |
| 128 | + }) |
| 129 | + |
| 130 | + o.warnings[0].should.match(/Too many arguments provided. Expected max 1 but received 4./) |
90 | 131 | }) |
91 | 132 |
|
92 | 133 | it('supports null as special type', function () { |
93 | | - function foo (arg) { |
94 | | - argsert('<null>', [].slice.call(arguments)) |
95 | | - } |
96 | | - foo(null) |
| 134 | + var o = checkOutput(function () { |
| 135 | + function foo (arg) { |
| 136 | + argsert('<null>', [].slice.call(arguments)) |
| 137 | + } |
| 138 | + foo(null) |
| 139 | + }) |
| 140 | + |
| 141 | + o.warnings.length.should.equal(0) |
97 | 142 | }) |
98 | 143 | }) |
0 commit comments