Skip to content

Commit

Permalink
Issue #10752: Added support for violation N lines above/below in Inli…
Browse files Browse the repository at this point in the history
…ne Config Parser and migrated to Inline Config Parser in NonEmptyAtclauseDescriptionCheckTest
  • Loading branch information
Vyom-Yadav authored and rnveach committed Dec 9, 2021
1 parent defd358 commit e4cf277
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
2 changes: 0 additions & 2 deletions pom.xml
Expand Up @@ -1607,8 +1607,6 @@

<exclude>**/AllBlockCommentsTest.class</exclude>

<exclude>**/NonEmptyAtclauseDescriptionCheckTest.class</exclude>

<exclude>**/WriteTagCheckTest.class</exclude>

<exclude>**/AbstractJavadocCheckTest.class</exclude>
Expand Down
Expand Up @@ -76,6 +76,14 @@ public final class InlineConfigParser {
private static final Pattern FILTERED_VIOLATION_BELOW_PATTERN = Pattern
.compile(".*//\\s*filtered violation below(?:\\W+'(.*)')?$");

/** A pattern to find the string: "// violation X lines above". */
private static final Pattern VIOLATION_SOME_LINES_ABOVE_PATTERN = Pattern
.compile(".*//\\s*violation (\\d+) lines above(?:\\W+'(.*)')?$");

/** A pattern to find the string: "// violation X lines below". */
private static final Pattern VIOLATION_SOME_LINES_BELOW_PATTERN = Pattern
.compile(".*//\\s*violation (\\d+) lines below(?:\\W+'(.*)')?$");

/** The String "(null)". */
private static final String NULL_STRING = "(null)";

Expand Down Expand Up @@ -285,6 +293,10 @@ private static void setViolations(TestInputConfiguration.Builder inputConfigBuil
MULTIPLE_VIOLATIONS_ABOVE_PATTERN.matcher(lines.get(lineNo));
final Matcher multipleViolationsBelowMatcher =
MULTIPLE_VIOLATIONS_BELOW_PATTERN.matcher(lines.get(lineNo));
final Matcher violationSomeLinesAboveMatcher =
VIOLATION_SOME_LINES_ABOVE_PATTERN.matcher(lines.get(lineNo));
final Matcher violationSomeLinesBelowMatcher =
VIOLATION_SOME_LINES_BELOW_PATTERN.matcher(lines.get(lineNo));
if (violationMatcher.matches()) {
inputConfigBuilder.addViolation(lineNo + 1, violationMatcher.group(1));
}
Expand All @@ -294,6 +306,16 @@ else if (violationAboveMatcher.matches()) {
else if (violationBelowMatcher.matches()) {
inputConfigBuilder.addViolation(lineNo + 2, violationBelowMatcher.group(1));
}
else if (violationSomeLinesAboveMatcher.matches()) {
final int linesAbove = Integer.parseInt(violationSomeLinesAboveMatcher.group(1)) - 1;
inputConfigBuilder.addViolation(lineNo - linesAbove,
violationSomeLinesAboveMatcher.group(2));
}
else if (violationSomeLinesBelowMatcher.matches()) {
final int linesBelow = Integer.parseInt(violationSomeLinesBelowMatcher.group(1)) + 1;
inputConfigBuilder.addViolation(lineNo + linesBelow,
violationSomeLinesBelowMatcher.group(2));
}
else if (multipleViolationsMatcher.matches()) {
Collections
.nCopies(Integer.parseInt(multipleViolationsMatcher.group(1)), lineNo + 1)
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class NonEmptyAtclauseDescriptionCheckTest
Expand Down Expand Up @@ -55,38 +54,35 @@ public void testGetRequiredTokens() {
}

@Test
public void testCheck()
throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(NonEmptyAtclauseDescriptionCheck.class);
public void testCheck() throws Exception {
final String[] expected = {
// this is a case with description that is sequences of spaces
"36: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"37: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"38: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"47: " + getCheckMessage(MSG_KEY),
"39: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"50: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"48: " + getCheckMessage(MSG_KEY),
"51: " + getCheckMessage(MSG_KEY),
// this is a case with description that is sequences of spaces
"49: " + getCheckMessage(MSG_KEY),
"85: " + getCheckMessage(MSG_KEY),
"86: " + getCheckMessage(MSG_KEY),
"87: " + getCheckMessage(MSG_KEY),
"88: " + getCheckMessage(MSG_KEY),
"89: " + getCheckMessage(MSG_KEY),
"90: " + getCheckMessage(MSG_KEY),
"99: " + getCheckMessage(MSG_KEY),
"100: " + getCheckMessage(MSG_KEY),
"101: " + getCheckMessage(MSG_KEY),
"102: " + getCheckMessage(MSG_KEY),
"103: " + getCheckMessage(MSG_KEY),
"130: " + getCheckMessage(MSG_KEY),
"139: " + getCheckMessage(MSG_KEY),
"52: " + getCheckMessage(MSG_KEY),
"92: " + getCheckMessage(MSG_KEY),
"93: " + getCheckMessage(MSG_KEY),
"94: " + getCheckMessage(MSG_KEY),
"95: " + getCheckMessage(MSG_KEY),
"96: " + getCheckMessage(MSG_KEY),
"97: " + getCheckMessage(MSG_KEY),
"110: " + getCheckMessage(MSG_KEY),
"111: " + getCheckMessage(MSG_KEY),
"112: " + getCheckMessage(MSG_KEY),
"113: " + getCheckMessage(MSG_KEY),
"114: " + getCheckMessage(MSG_KEY),
"143: " + getCheckMessage(MSG_KEY),
"152: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputNonEmptyAtclauseDescription.java"), expected);
verifyWithInlineConfigParser(getPath("InputNonEmptyAtclauseDescription.java"), expected);
}

}
Expand Up @@ -31,23 +31,27 @@ public InputNonEmptyAtclauseDescription(String a)

}

// violation 3 lines below
/**
* Some javadoc.
* @param a
* @param b
* @param c
*/
*/ // violation 2 lines above
// violation 2 lines above
public InputNonEmptyAtclauseDescription(String a, int b, double c)
{

}

// violation 3 lines below
/**
*
* @param a
* @param e
* @deprecated
*/
*/ // violation 2 lines above
// violation 2 lines above
public InputNonEmptyAtclauseDescription(String a, boolean e)
{

Expand Down Expand Up @@ -80,6 +84,9 @@ public int foo2(String a, int b, double c) throws Exception
return 1;
}

// violation 5 lines below
// violation 5 lines below
// violation 5 lines below
/**
*
* @param a
Expand All @@ -88,20 +95,26 @@ public int foo2(String a, int b, double c) throws Exception
* @deprecated
* @throws Exception
* @deprecated
*/ // violation above
*/ // violation 3 lines above
// violation 3 lines above
// violation 3 lines above
public int foo3(String a, int b, double c) throws Exception
{
return 1;
}

// violation 4 lines below
// violation 4 lines below
/**
*
* @param a
* @param b
* @param c
* @deprecated
* @throws Exception
*/
*/ // violation 3 lines above
// violation 3 lines above
// violation 3 lines above
public int foo4(String a, int b, double c) throws Exception
{
return 1;
Expand All @@ -128,7 +141,7 @@ public int foo5(String a, int b, double c) throws Exception
* @param c Some javadoc
* @return Some javadoc
* @exception Exception
*/
*/ // violation above
public int foo6(String a, int b, double c) throws Exception
{
return 1;
Expand All @@ -137,7 +150,7 @@ public int foo6(String a, int b, double c) throws Exception
/**
* @param a xxx
* @return
*/ // ^ violation
*/ // violation above
int foo(int a) {
return a;
}
Expand Down

0 comments on commit e4cf277

Please sign in to comment.