Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable examples tests for CyclomaticComplexit…
Browse files Browse the repository at this point in the history
…yCheck
  • Loading branch information
AmitKumarDeoghoria authored and romani committed Nov 20, 2024
1 parent c0c1fe0 commit bc02282
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 137 deletions.
4 changes: 4 additions & 0 deletions config/checkstyle-examples-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<suppress checks="CommentsIndentation"
files="commentsindentation[\\/]Example[1-9].java"/>

<!-- violation required by to show behavior of Check -->
<suppress checks="FileLength"
files="cyclomaticcomplexity[\\/]Example\d+.*"/>

<!-- Examples contains important violations that need to be shown -->
<suppress checks="FileLength"
files="illegaltype[\\/]Example\d+.*"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

package com.puppycrawl.tools.checkstyle.checks.metrics;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;

@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
public class CyclomaticComplexityCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -34,27 +32,27 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {

"32:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 13, 10),
};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}

@Test
public void testExample2() throws Exception {
final String[] expected = {

"17:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 5, 4),
};

verifyWithInlineConfigParser(getPath("Example2.txt"), expected);
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
}

@Test
public void testExample3() throws Exception {
final String[] expected = {

"34:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 11, 10),
};

verifyWithInlineConfigParser(getPath("Example3.txt"), expected);
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="CyclomaticComplexity"/>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity;

// xdoc section -- start
class Example1 {
int a, b, c, d, e, n;

public void testMethod1() {
while (a < b && a > c) {
fun1();
}
if (a == b) {
do {
fun1();
} while (d==a);
} else if (c == d) {
while (c > 0) {
fun1();
}
do {
fun1();
} while (a==d);
}
}
// violation below, 'Cyclomatic Complexity is 13 (max allowed is 10)'
public void testMethod2() { // 1, function declaration
if (a == b) { // 2, if
fun1();
} else if (a == 0 // 3, if
&& b == c) { // 4, && operator
if (c == -1) { // 5, if
fun1();
}
} else if (a == c // 6, if
|| a == d) { // 7, || operator
fun1();
} else if (d == e) { //8, if
try {
fun1();
} catch (Exception e) { // 9, catch
}
} else {
switch(n) {
case 1: // 10, case
fun1();
break;
case 2: // 11, case
fun1();
break;
case 3: // 12, case
fun1();
break;
default:
break;
}
}
a = a > 0 ? b : c; // 13, ternary operator
}
private void fun1() {}
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="CyclomaticComplexity">
<property name="max" value="4"/>
<property name="tokens" value="LITERAL_WHILE, LITERAL_DO"/>
</module>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity;

// xdoc section -- start
class Example2 {
int a, b, c, d, e, n;
// violation below, 'Cyclomatic Complexity is 5 (max allowed is 4)'
public void testMethod1() { // 1, function declaration
while (a < b && a > c) { // 2, while
fun1();
}
if (a == b) {
do { // 3, do
fun1();
} while (d==a);
} else if (c == d) {
while (c > 0) { // 4, while
fun1();
}
do { // 5, do-while
fun1();
} while (a==d);
}
}

public void testMethod2() {
if (a == b) {
fun1();
} else if (a == 0
&& b == c) {
if (c == -1) {
fun1();
}
} else if (a == c
|| a == d) {
fun1();
} else if (d == e) {
try {
fun1();
} catch (Exception e) {
}
} else {
switch(n) {
case 1:
fun1();
break;
case 2:
fun1();
break;
case 3:
fun1();
break;
default:
break;
}
}
a = a > 0 ? b : c;
}
private void fun1() {}
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,44 @@
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity;

// xdoc section -- start
class CyclomaticComplexity {
// Cyclomatic Complexity = 11
class Example3 {
int a, b, c, d, e, n;
public void foo() { // 1, function declaration

public void testMethod1() {
while (a < b && a > c) {
fun1();
}
if (a == b) {
do {
fun1();
} while (d==a);
} else if (c == d) {
while (c > 0) {
fun1();
}
do {
fun1();
} while (a==d);
}
}
// violation below, 'Cyclomatic Complexity is 11 (max allowed is 10)'
public void testMethod2() { // 1, function declaration
if (a == b) { // 2, if
fun1();
} else if (a == 0 // 3, if
&& b == c) { // 4, && operator
if (c == -1) { // 5, if
fun2();
fun1();
}
} else if (a == c // 6, if
|| a == d) { // 7, || operator
fun3();
fun1();
} else if (d == e) { // 8, if
try {
fun4();
fun1();
} catch (Exception e) { // 9, catch
}
} else {
Expand All @@ -34,13 +53,17 @@ public void foo() { // 1, function declaration
fun1();
break;
case 2:
fun2();
fun1();
break;
case 3: // 10, case
fun1();
break;
default:
break;
}
}
a = a > 0 ? b : c; // 11, ternary operator
}
private void fun1() {}
}
// xdoc section -- end
Loading

0 comments on commit bc02282

Please sign in to comment.