Skip to content

Commit

Permalink
basic return rules: refactor tests
Browse files Browse the repository at this point in the history
- split one big file to separate rules tests
  • Loading branch information
Alexej Yaroshevich committed Nov 8, 2014
1 parent 3d9648b commit 165ed20
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 384 deletions.
1 change: 1 addition & 0 deletions lib/rules/validate-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function() {};

module.exports.prototype = {

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

Expand Down
52 changes: 52 additions & 0 deletions test/lib/rules/validate-jsdoc/check-redundant-returns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe('rules/validate-jsdoc', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('check-redudant-returns', function() {

checker.rules({checkRedundantReturns: true});
checker.cases([
/* jshint ignore:start */
{
it: 'should report redundant @returns for function',
errors: 3,
code: function () {
/**
* @return {string}
*/
function funcName() {
// dummy
}

/**
* @returns {String}
*/
function funcName() {
var x = function () { return 1; }
}

/**
* @returns {String}
*/
function funcName() {
return;
}
}
}, {
it: 'should not report redundant @returns for function',
code: function () {
/**
* @returns {String}
*/
function funcName() {
var x = function () { return 1; }
if (true) { return x; }
}
}
}
/* jshint ignore:end */
]);

});
});
254 changes: 254 additions & 0 deletions test/lib/rules/validate-jsdoc/check-return-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
describe('rules/validate-jsdoc', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('check-return-types', function () {

checker.rules({checkReturnTypes: true});
checker.cases([
/* jshint ignore:start */
{
it: 'should neither throw nor report',
code: function () {
/**
* @return {{a: number, b: string}}
*/
function foo() {
return {};
}
}
}, {
it: 'should report invalid @returns type in function',
errors: 1,
code: function() {
/**
* @returns {Object}
*/
function funcName() {
return "";
}
}
}, {
it: 'should not report valid resulting type with object type in method',
code: function() {
Cls.prototype = {
/**
* @return {{bar: number}}
*/
run: function (xxx) {
return {};
}
};
}
}, {
it: 'should not report valid resulting type with object type in function',
code: function() {
/**
* @return {Object}
*/
function funcName() {
return new Object();
}
}
}, {
it: 'should not report comparition jsdoc type to any expression in function',
code: function() {
/**
* @return {Object}
*/
function funcName() {
return Object.create();
}
/**
* @return {string}
*/
function funcName() {
return makeMyDay("zxc");
}
}
}, {
it: 'should not report valid resulting array type for function',
code: function() {
/**
* @return {Array}
*/
function funcName() {
return [1, 2];
}
/**
* @return {Array}
*/
function funcName() {
return new Array("zxc");
}
}
}, {
it: 'should not report valid resulting regexp type for function',
code: function() {
/**
* @return {RegExp}
*/
function funcName() {
return /[a-z]+/i;
}
/**
* @return {RegExp}
*/
function funcName() {
return new RegExp("[a-z]+", "i");
}
}
}, {
it: 'should not report valid resulting array.<String> and Object[] for function',
code: function() {
/**
* @return {String[]}
*/
function funcName() {
return ["a", "b", "c"];
}
/**
* @return {Object[]}
*/
function funcName() {
return [{}, {}];
}
/**
* @return {Array.<Number>}
*/
function funcName() {
return [1, 2, 3];
}
}
}, {
it: 'should not throw exception on `@returns {null|undefined}` directive. issue #7',
code: function() {
/**
* @return {null}
*/
function funcName() {
return null;
}
/**
* @return {undefined}
*/
function funcName() {
return undefined;
}
/**
* @return {null|undefined}
*/
function funcName(flag) {
if (flag) {
return null;
}
return undefined;
}
}
}, {
it: 'should report on `@returns {null|undefined}` vs (string|number|regexp). issue #7',
errors: 3,
code: function() {
/**
* @return {null|undefined}
*/
function funcName(flag) {
if (flag) {
return /qwe/i;
} else {
return 2;
}
return "";
}
}
}, {
it: 'should report on `@returns {null|undefined}` vs (array|object). issue #7',
errors: 2,
code: function() {
/**
* @return {null|undefined}
*/
function funcName(flag) {
if (flag) {
return [];
}
return {q: 1};
}
}
}, {
it: 'should not report on `@returns {string|null}` vs (null). issue #8',
code: function() {
/**
* @return {string|null}
*/
function funcName(flag) {
if (!this.something) {
return null;
}
}
/**
* @return {?string}
*/
function funcName(flag) {
if (!this.something) {
return null;
}
}
}
}, {
it: 'should not report on `@returns {?number}` vs (null|number). issue #8',
code: function() {
/**
* @return {number|null}
*/
function funcName(flag) {
if (!this.something) {
return null;
}
return 3;
}
/**
* @return {?number}
*/
function funcName(flag) {
if (!this.something) {
return null;
}
return 3;
}
}
}, {
it: 'should not report on `@returns {foo.Bar}`. issue #16',
code: function() {
module.exports = {
/**
* @return {foo.Bar}
*/
foo: function () {
return new foo.Bar();
},
/**
* @return {foo.Bar.Baz}
*/
bar: function () {
return new foo.Bar.Baz();
},
/**
* @return {foo.Bar.Baz|foo.Bar.Baz.Moo}
*/
baz: function (t) {
if (t) {
return new foo.Bar.Baz.Moo();
}
return new foo.Bar.Baz();
}
};
}
}
/* jshint ignore:end */
]);

});

});
40 changes: 40 additions & 0 deletions test/lib/rules/validate-jsdoc/require-return-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
describe('rules/validate-jsdoc', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('require-return-types', function() {

checker.rules({requireReturnTypes: true});
checker.cases([
/* jshint ignore:start */
{
it: 'should report invalid @returns',
errors: 1,
code: function() {
var x = 1;
/**
* @return
*/
function funcName() {
// dummy
}
}
}, {
it: 'should not report valid jsdoc with object type in method',
code: function() {
Cls.prototype = {
/**
* @return {{bar: number}}
*/
run: function (xxx) {
return {};
}
};
}
}
/* jshint ignore:end */
]);

});
});
Loading

0 comments on commit 165ed20

Please sign in to comment.