Skip to content

Commit 165ed20

Browse files
author
Alexej Yaroshevich
committed
basic return rules: refactor tests
- split one big file to separate rules tests
1 parent 3d9648b commit 165ed20

5 files changed

Lines changed: 347 additions & 384 deletions

File tree

lib/rules/validate-jsdoc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = function() {};
77

88
module.exports.prototype = {
99

10+
// load all rules and init them
1011
configure: function(options) {
1112
assert(typeof options === 'object', 'jsDoc option requires object value');
1213

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
describe('rules/validate-jsdoc', function () {
2+
var checker = global.checker({
3+
additionalRules: ['lib/rules/validate-jsdoc.js']
4+
});
5+
6+
describe('check-redudant-returns', function() {
7+
8+
checker.rules({checkRedundantReturns: true});
9+
checker.cases([
10+
/* jshint ignore:start */
11+
{
12+
it: 'should report redundant @returns for function',
13+
errors: 3,
14+
code: function () {
15+
/**
16+
* @return {string}
17+
*/
18+
function funcName() {
19+
// dummy
20+
}
21+
22+
/**
23+
* @returns {String}
24+
*/
25+
function funcName() {
26+
var x = function () { return 1; }
27+
}
28+
29+
/**
30+
* @returns {String}
31+
*/
32+
function funcName() {
33+
return;
34+
}
35+
}
36+
}, {
37+
it: 'should not report redundant @returns for function',
38+
code: function () {
39+
/**
40+
* @returns {String}
41+
*/
42+
function funcName() {
43+
var x = function () { return 1; }
44+
if (true) { return x; }
45+
}
46+
}
47+
}
48+
/* jshint ignore:end */
49+
]);
50+
51+
});
52+
});
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
describe('rules/validate-jsdoc', function () {
2+
var checker = global.checker({
3+
additionalRules: ['lib/rules/validate-jsdoc.js']
4+
});
5+
6+
describe('check-return-types', function () {
7+
8+
checker.rules({checkReturnTypes: true});
9+
checker.cases([
10+
/* jshint ignore:start */
11+
{
12+
it: 'should neither throw nor report',
13+
code: function () {
14+
/**
15+
* @return {{a: number, b: string}}
16+
*/
17+
function foo() {
18+
return {};
19+
}
20+
}
21+
}, {
22+
it: 'should report invalid @returns type in function',
23+
errors: 1,
24+
code: function() {
25+
/**
26+
* @returns {Object}
27+
*/
28+
function funcName() {
29+
return "";
30+
}
31+
}
32+
}, {
33+
it: 'should not report valid resulting type with object type in method',
34+
code: function() {
35+
Cls.prototype = {
36+
/**
37+
* @return {{bar: number}}
38+
*/
39+
run: function (xxx) {
40+
return {};
41+
}
42+
};
43+
}
44+
}, {
45+
it: 'should not report valid resulting type with object type in function',
46+
code: function() {
47+
/**
48+
* @return {Object}
49+
*/
50+
function funcName() {
51+
return new Object();
52+
}
53+
}
54+
}, {
55+
it: 'should not report comparition jsdoc type to any expression in function',
56+
code: function() {
57+
/**
58+
* @return {Object}
59+
*/
60+
function funcName() {
61+
return Object.create();
62+
}
63+
/**
64+
* @return {string}
65+
*/
66+
function funcName() {
67+
return makeMyDay("zxc");
68+
}
69+
}
70+
}, {
71+
it: 'should not report valid resulting array type for function',
72+
code: function() {
73+
/**
74+
* @return {Array}
75+
*/
76+
function funcName() {
77+
return [1, 2];
78+
}
79+
/**
80+
* @return {Array}
81+
*/
82+
function funcName() {
83+
return new Array("zxc");
84+
}
85+
}
86+
}, {
87+
it: 'should not report valid resulting regexp type for function',
88+
code: function() {
89+
/**
90+
* @return {RegExp}
91+
*/
92+
function funcName() {
93+
return /[a-z]+/i;
94+
}
95+
/**
96+
* @return {RegExp}
97+
*/
98+
function funcName() {
99+
return new RegExp("[a-z]+", "i");
100+
}
101+
}
102+
}, {
103+
it: 'should not report valid resulting array.<String> and Object[] for function',
104+
code: function() {
105+
/**
106+
* @return {String[]}
107+
*/
108+
function funcName() {
109+
return ["a", "b", "c"];
110+
}
111+
/**
112+
* @return {Object[]}
113+
*/
114+
function funcName() {
115+
return [{}, {}];
116+
}
117+
/**
118+
* @return {Array.<Number>}
119+
*/
120+
function funcName() {
121+
return [1, 2, 3];
122+
}
123+
}
124+
}, {
125+
it: 'should not throw exception on `@returns {null|undefined}` directive. issue #7',
126+
code: function() {
127+
/**
128+
* @return {null}
129+
*/
130+
function funcName() {
131+
return null;
132+
}
133+
/**
134+
* @return {undefined}
135+
*/
136+
function funcName() {
137+
return undefined;
138+
}
139+
/**
140+
* @return {null|undefined}
141+
*/
142+
function funcName(flag) {
143+
if (flag) {
144+
return null;
145+
}
146+
return undefined;
147+
}
148+
}
149+
}, {
150+
it: 'should report on `@returns {null|undefined}` vs (string|number|regexp). issue #7',
151+
errors: 3,
152+
code: function() {
153+
/**
154+
* @return {null|undefined}
155+
*/
156+
function funcName(flag) {
157+
if (flag) {
158+
return /qwe/i;
159+
} else {
160+
return 2;
161+
}
162+
return "";
163+
}
164+
}
165+
}, {
166+
it: 'should report on `@returns {null|undefined}` vs (array|object). issue #7',
167+
errors: 2,
168+
code: function() {
169+
/**
170+
* @return {null|undefined}
171+
*/
172+
function funcName(flag) {
173+
if (flag) {
174+
return [];
175+
}
176+
return {q: 1};
177+
}
178+
}
179+
}, {
180+
it: 'should not report on `@returns {string|null}` vs (null). issue #8',
181+
code: function() {
182+
/**
183+
* @return {string|null}
184+
*/
185+
function funcName(flag) {
186+
if (!this.something) {
187+
return null;
188+
}
189+
}
190+
/**
191+
* @return {?string}
192+
*/
193+
function funcName(flag) {
194+
if (!this.something) {
195+
return null;
196+
}
197+
}
198+
}
199+
}, {
200+
it: 'should not report on `@returns {?number}` vs (null|number). issue #8',
201+
code: function() {
202+
/**
203+
* @return {number|null}
204+
*/
205+
function funcName(flag) {
206+
if (!this.something) {
207+
return null;
208+
}
209+
return 3;
210+
}
211+
/**
212+
* @return {?number}
213+
*/
214+
function funcName(flag) {
215+
if (!this.something) {
216+
return null;
217+
}
218+
return 3;
219+
}
220+
}
221+
}, {
222+
it: 'should not report on `@returns {foo.Bar}`. issue #16',
223+
code: function() {
224+
module.exports = {
225+
/**
226+
* @return {foo.Bar}
227+
*/
228+
foo: function () {
229+
return new foo.Bar();
230+
},
231+
/**
232+
* @return {foo.Bar.Baz}
233+
*/
234+
bar: function () {
235+
return new foo.Bar.Baz();
236+
},
237+
/**
238+
* @return {foo.Bar.Baz|foo.Bar.Baz.Moo}
239+
*/
240+
baz: function (t) {
241+
if (t) {
242+
return new foo.Bar.Baz.Moo();
243+
}
244+
return new foo.Bar.Baz();
245+
}
246+
};
247+
}
248+
}
249+
/* jshint ignore:end */
250+
]);
251+
252+
});
253+
254+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
describe('rules/validate-jsdoc', function () {
2+
var checker = global.checker({
3+
additionalRules: ['lib/rules/validate-jsdoc.js']
4+
});
5+
6+
describe('require-return-types', function() {
7+
8+
checker.rules({requireReturnTypes: true});
9+
checker.cases([
10+
/* jshint ignore:start */
11+
{
12+
it: 'should report invalid @returns',
13+
errors: 1,
14+
code: function() {
15+
var x = 1;
16+
/**
17+
* @return
18+
*/
19+
function funcName() {
20+
// dummy
21+
}
22+
}
23+
}, {
24+
it: 'should not report valid jsdoc with object type in method',
25+
code: function() {
26+
Cls.prototype = {
27+
/**
28+
* @return {{bar: number}}
29+
*/
30+
run: function (xxx) {
31+
return {};
32+
}
33+
};
34+
}
35+
}
36+
/* jshint ignore:end */
37+
]);
38+
39+
});
40+
});

0 commit comments

Comments
 (0)