From ef018b99c32d4b0f4182c1e89834f491ca9c63ae Mon Sep 17 00:00:00 2001 From: Alexej Yaroshevich Date: Tue, 11 Nov 2014 02:26:44 +0400 Subject: [PATCH] fixup rule configuration asserts and add tests - fixup bug in checkOption method (not it works) - add tests for all rules Fixes #23 Closes #44 --- lib/rules/validate-jsdoc/index.js | 22 +++++++++---------- test/init.js | 1 + .../rules/validate-jsdoc/check-param-names.js | 20 +++++++++++++++-- .../validate-jsdoc/check-redundant-access.js | 20 +++++++++++++++-- .../validate-jsdoc/check-redundant-params.js | 20 +++++++++++++++-- .../validate-jsdoc/check-redundant-returns.js | 20 +++++++++++++++-- .../validate-jsdoc/check-return-types.js | 20 +++++++++++++++-- test/lib/rules/validate-jsdoc/check-types.js | 20 +++++++++++++++-- .../rules/validate-jsdoc/enforce-existence.js | 20 +++++++++++++++-- .../leading-underscore-access.js | 21 ++++++++++++++++-- .../validate-jsdoc/require-param-types.js | 20 +++++++++++++++-- .../validate-jsdoc/require-return-types.js | 20 +++++++++++++++-- 12 files changed, 193 insertions(+), 31 deletions(-) diff --git a/lib/rules/validate-jsdoc/index.js b/lib/rules/validate-jsdoc/index.js index 944c6b9..d52abd2 100644 --- a/lib/rules/validate-jsdoc/index.js +++ b/lib/rules/validate-jsdoc/index.js @@ -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]; @@ -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], diff --git a/test/init.js b/test/init.js index ce383cb..725d614 100644 --- a/test/init.js +++ b/test/init.js @@ -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(); diff --git a/test/lib/rules/validate-jsdoc/check-param-names.js b/test/lib/rules/validate-jsdoc/check-param-names.js index cbf848c..8621e9f 100644 --- a/test/lib/rules/validate-jsdoc/check-param-names.js +++ b/test/lib/rules/validate-jsdoc/check-param-names.js @@ -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([ diff --git a/test/lib/rules/validate-jsdoc/check-redundant-access.js b/test/lib/rules/validate-jsdoc/check-redundant-access.js index 82cbba8..c58026d 100644 --- a/test/lib/rules/validate-jsdoc/check-redundant-access.js +++ b/test/lib/rules/validate-jsdoc/check-redundant-access.js @@ -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 */ { diff --git a/test/lib/rules/validate-jsdoc/check-redundant-params.js b/test/lib/rules/validate-jsdoc/check-redundant-params.js index 9f53dd8..e9e3a57 100644 --- a/test/lib/rules/validate-jsdoc/check-redundant-params.js +++ b/test/lib/rules/validate-jsdoc/check-redundant-params.js @@ -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([ diff --git a/test/lib/rules/validate-jsdoc/check-redundant-returns.js b/test/lib/rules/validate-jsdoc/check-redundant-returns.js index bde9239..8d47deb 100644 --- a/test/lib/rules/validate-jsdoc/check-redundant-returns.js +++ b/test/lib/rules/validate-jsdoc/check-redundant-returns.js @@ -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([ diff --git a/test/lib/rules/validate-jsdoc/check-return-types.js b/test/lib/rules/validate-jsdoc/check-return-types.js index ff8b209..684b278 100644 --- a/test/lib/rules/validate-jsdoc/check-return-types.js +++ b/test/lib/rules/validate-jsdoc/check-return-types.js @@ -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 */ { diff --git a/test/lib/rules/validate-jsdoc/check-types.js b/test/lib/rules/validate-jsdoc/check-types.js index c687951..439bf79 100644 --- a/test/lib/rules/validate-jsdoc/check-types.js +++ b/test/lib/rules/validate-jsdoc/check-types.js @@ -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 */ { diff --git a/test/lib/rules/validate-jsdoc/enforce-existence.js b/test/lib/rules/validate-jsdoc/enforce-existence.js index 9164ca0..694dc54 100644 --- a/test/lib/rules/validate-jsdoc/enforce-existence.js +++ b/test/lib/rules/validate-jsdoc/enforce-existence.js @@ -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 */ { diff --git a/test/lib/rules/validate-jsdoc/leading-underscore-access.js b/test/lib/rules/validate-jsdoc/leading-underscore-access.js index a7ca0d2..3ccaf35 100644 --- a/test/lib/rules/validate-jsdoc/leading-underscore-access.js +++ b/test/lib/rules/validate-jsdoc/leading-underscore-access.js @@ -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) { } diff --git a/test/lib/rules/validate-jsdoc/require-param-types.js b/test/lib/rules/validate-jsdoc/require-param-types.js index 52fc9f7..f80c0c6 100644 --- a/test/lib/rules/validate-jsdoc/require-param-types.js +++ b/test/lib/rules/validate-jsdoc/require-param-types.js @@ -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([ diff --git a/test/lib/rules/validate-jsdoc/require-return-types.js b/test/lib/rules/validate-jsdoc/require-return-types.js index 99a3fae..5b69e18 100644 --- a/test/lib/rules/validate-jsdoc/require-return-types.js +++ b/test/lib/rules/validate-jsdoc/require-return-types.js @@ -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 */ {