Permalink
Browse files

Fix: Add tests to cover default magic number tests (fixes #5385)

  • Loading branch information...
bsbeeks committed Feb 24, 2016
1 parent 40fec58 commit 694e1c19ff504dc9b8f67ba7707130ba1c1750a0
@@ -16,6 +16,23 @@ var rule = require("../../../lib/rules/complexity"),
// Helpers
//------------------------------------------------------------------------------
/**
* Generates a code string with the amount of complexity specified in the parameter
* @param {int} complexity The level of complexity
* @returns {string} Code with the amount of complexity specified in the parameter
* @private
*/
function createComplexity(complexity) {
var funcString = "function test (a) { if (a === 1) {";
for (var i = 2; i < complexity; i++) {
funcString += "} else if (a === " + i + ") {";
}
funcString += "} };";
return funcString;
}
var ruleTester = new RuleTester();
ruleTester.run("complexity", rule, {
@@ -65,6 +82,10 @@ ruleTester.run("complexity", rule, {
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {while(true){'bar';}})();}", options: [1], errors: 2 },
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {'bar';})();}", options: [1], errors: 1 },
{ code: "var obj = { a(x) { return x ? 0 : 1; } };", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Function 'a' has a complexity of 2."}] },
{ code: "var obj = { a: function b(x) { return x ? 0 : 1; } };", options: [1], errors: [{ message: "Function 'b' has a complexity of 2."}] }
{ code: "var obj = { a: function b(x) { return x ? 0 : 1; } };", options: [1], errors: [{ message: "Function 'b' has a complexity of 2."}] },
{
code: createComplexity(21),
errors: [{ message: "Function 'test' has a complexity of 21." }]
}
]
});
@@ -23,14 +23,15 @@ ruleTester.run("max-depth", rule, {
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [3] },
{ code: "function foo() { if (true) { } else if (false) { } else if (true) { } else if (false) {} }", options: [3] },
{ code: "var foo = () => { if (true) { if (false) { if (true) { } } } }", options: [3], parserOptions: { ecmaVersion: 6 } },
"function foo() { if (true) { if (false) { if (true) { } } } }"
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }" }
],
invalid: [
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [2], errors: [{ message: "Blocks are nested too deeply (3).", type: "IfStatement"}] },
{ code: "var foo = () => { if (true) { if (false) { if (true) { } } } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Blocks are nested too deeply (3).", type: "IfStatement"}] },
{ code: "function foo() { if (true) {} else { for(;;) {} } }", options: [1], errors: [{ message: "Blocks are nested too deeply (2).", type: "ForStatement"}] },
{ code: "function foo() { while (true) { if (true) {} } }", options: [1], errors: [{ message: "Blocks are nested too deeply (2).", type: "IfStatement"}] },
{ code: "function foo() { for (let x of foo) { if (true) {} } }", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Blocks are nested too deeply (2).", type: "IfStatement"}] },
{ code: "function foo() { while (true) { if (true) { if (false) { } } } }", options: [1], errors: [{ message: "Blocks are nested too deeply (2).", type: "IfStatement"}, { message: "Blocks are nested too deeply (3).", type: "IfStatement"}] }
{ code: "function foo() { while (true) { if (true) { if (false) { } } } }", options: [1], errors: [{ message: "Blocks are nested too deeply (2).", type: "IfStatement"}, { message: "Blocks are nested too deeply (3).", type: "IfStatement"}] },
{ code: "function foo() { if (true) { if (false) { if (true) { if (false) { if (true) { } } } } } }", errors: [{ message: "Blocks are nested too deeply (5).", type: "IfStatement"}] }
]
});
@@ -20,7 +20,7 @@ var rule = require("../../../lib/rules/max-params"),
var ruleTester = new RuleTester();
ruleTester.run("max-params", rule, {
valid: [
"function test(d, e, f) {}",
{ code: "function test(d, e, f) {}" },
{ code: "var test = function(a, b, c) {};", options: [3] },
{ code: "var test = (a, b, c) => {};", options: [3], parserOptions: { ecmaVersion: 6 } },
{ code: "var test = function test(a, b, c) {};", options: [3] }
@@ -25,7 +25,7 @@ ruleTester.run("max-statements", rule, {
{ code: "function foo() { var bar = 1; function qux () { var noCount = 2; } return 3; }", options: [3] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [6]},
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [5]},
"function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); }",
{ code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); }" },
{ code: "(function() { var bar = 1; return function () { return 42; }; })()", options: [1, {ignoreTopLevelFunctions: true}] },
{ code: "function foo() { var bar = 1; var baz = 2; }", options: [1, {ignoreTopLevelFunctions: true}] },
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; })", options: [1, {ignoreTopLevelFunctions: true}] }
@@ -41,6 +41,7 @@ ruleTester.run("max-statements", rule, {
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [4], errors: [{ message: "This function has too many statements (5). Maximum allowed is 4."}] },
{ code: ";(function() { var bar = 1; return function () { var z; return 42; }; })()", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: ";(function() { var bar = 1; var baz = 2; })(); (function() { var bar = 1; var baz = 2; })()", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}, { message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; return function () { var z; return 42; }; })", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}] }
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; return function () { var z; return 42; }; })", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); foo(); }", errors: [{ message: "This function has too many statements (11). Maximum allowed is 10."}] }
]
});

0 comments on commit 694e1c1

Please sign in to comment.