Skip to content

Commit

Permalink
fixup rule configuration asserts and add tests
Browse files Browse the repository at this point in the history
- fixup bug in checkOption method (not it works)
- add tests for all rules

Fixes #23
Closes #44
  • Loading branch information
Alexej Yaroshevich committed Nov 10, 2014
1 parent c6f52bc commit ef018b9
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 31 deletions.
22 changes: 11 additions & 11 deletions lib/rules/validate-jsdoc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var validatorsByName = module.exports = {

Object.defineProperty(validatorsByName, 'load', {
value: function loadValidators(passedOptions) {
var validators = [];

if (!passedOptions) {
return validators;
return [];
}

var validators = [];

Object.keys(validatorsByName).forEach(function(name) {
var v = validatorsByName[name];

Expand All @@ -50,22 +50,22 @@ Object.defineProperty(validatorsByName, 'load', {

Object.defineProperty(validatorsByName, 'checkOptions', {
value: function checkOptions(validator, options) {
Object.keys(validator.options).forEach(function(data, option) {
Object.keys(validator.options).forEach(function(option) {
var data = validator.options[option];
if (!data.allowedValues) {
return;
}

var values;
if (typeof data.allowedValues === 'function') {
values = data.allowedValues();
var values = data.allowedValues;
if (typeof values === 'function') {
values = values();
}

if (!Array.isArray(values)) {
throw new Error('Internal error in jsDoc validator ' + validator._name);
assert(Array.isArray(values), 'Internal error in jsDoc validator ' + validator._name);

} else if (values.length > 1) {
if (values.length > 1) {
assert(values.indexOf(options[option]) !== -1,
'Available values for option jsDoc.' + option + ' are ' + values.map(JSON.stringify).join(', '));
'Accepted values for option jsDoc.' + option + ' are ' + values.map(JSON.stringify).join(', '));

} else if (values.length) {
assert(values[0] === options[option],
Expand Down
1 change: 1 addition & 0 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var expect = chai.expect;
global.parse = parse;
global.fnBody = fnBody;
global.checker = rulesChecker;
global.expect = expect;

function fnBody(func) {
var str = func.toString();
Expand Down
20 changes: 18 additions & 2 deletions test/lib/rules/validate-jsdoc/check-param-names.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
describe('rules/validate-jsdoc', function () {
describe('lib/rules/validate-jsdoc/check-param-names', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('check-param-names', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkParamNames: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkParamNames: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkParamNames: true});

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

describe('check-redundant-access', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantAccess: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantAccess: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkRedundantAccess: true});

checker.cases([
/* jshint ignore:start */
{
Expand Down
20 changes: 18 additions & 2 deletions test/lib/rules/validate-jsdoc/check-redundant-params.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
describe('rules/validate-jsdoc', function () {
describe('lib/rules/validate-jsdoc/check-redundant-params', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('check-redundant-params', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantParams: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantParams: {}});
}).to.throws(/accepted value/i);
});

});

describe('with this', function() {
checker.rules({checkRedundantParams: true});

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

describe('check-redundant-returns', function() {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantReturns: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkRedundantReturns: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkRedundantReturns: true});

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

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

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkReturnTypes: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkReturnTypes: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkReturnTypes: true});

checker.cases([
/* jshint ignore:start */
{
Expand Down
20 changes: 18 additions & 2 deletions test/lib/rules/validate-jsdoc/check-types.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
describe('rules/validate-jsdoc', function() {
describe('lib/rules/validate-jsdoc/check-types', function() {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

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

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkTypes: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({checkTypes: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({checkTypes: true});

checker.cases([
/* jshint ignore:start */
{
Expand Down
20 changes: 18 additions & 2 deletions test/lib/rules/validate-jsdoc/enforce-existence.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
describe('rules/validate-jsdoc', function () {
describe('lib/rules/validate-jsdoc/enforce-existence', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('enforce-existence', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({enforceExistence: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({enforceExistence: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({enforceExistence: true});

checker.cases([
/* jshint ignore:start */
{
Expand Down
21 changes: 19 additions & 2 deletions test/lib/rules/validate-jsdoc/leading-underscore-access.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
describe('rules/validate-jsdoc', function () {
describe('lib/rules/validate-jsdoc/leading-underscore-access', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('leading-underscore-access', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({leadingUnderscoreAccess: undefined});
}).to.throws(/accepted value/i);
});

it('with object should throws', function() {
global.expect(function() {
checker.configure({leadingUnderscoreAccess: {}});
}).to.throws(/accepted value/i);
});

});

describe('common', function() {

checker.cases([
/* jshint ignore:start */
{
it: 'should not throw',
rules: {leadingUnderscoreAccess: true},
code: function() {
function yay(yey) {
}
Expand Down
20 changes: 18 additions & 2 deletions test/lib/rules/validate-jsdoc/require-param-types.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
describe('rules/validate-jsdoc', function () {
describe('lib/rules/validate-jsdoc/require-param-types', function () {
var checker = global.checker({
additionalRules: ['lib/rules/validate-jsdoc.js']
});

describe('require-param-types', function () {
describe('configured', function() {

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({requireParamTypes: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({requireParamTypes: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({requireParamTypes: true});

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

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

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({requireReturnTypes: undefined});
}).to.throws(/accepted value/i);
});

it('with undefined should throws', function() {
global.expect(function() {
checker.configure({requireReturnTypes: {}});
}).to.throws(/accepted value/i);
});

});

describe('with true', function() {
checker.rules({requireReturnTypes: true});

checker.cases([
/* jshint ignore:start */
{
Expand Down

0 comments on commit ef018b9

Please sign in to comment.