Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #13307: Added support for multiple violation lines in test input files #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -105,6 +106,10 @@ public final class InlineConfigParser {
private static final Pattern MULTIPLE_VIOLATIONS_BELOW_PATTERN = Pattern
.compile(".*//\\s*(\\d+) violations below$");

/** A pattern to find the string: "X violations below". */
private static final Pattern MULTIPLE_VIOLATIONS_BLOCK_BELOW_PATTERN = Pattern
.compile(".*\\s*(\\d+) violations below$");

/** A pattern to find the string: "// filtered violation". */
private static final Pattern FILTERED_VIOLATION_PATTERN = Pattern
.compile(".*//\\s*filtered violation\\s*(?:['\"](.*)['\"])?$");
Expand All @@ -125,6 +130,10 @@ public final class InlineConfigParser {
private static final Pattern VIOLATION_SOME_LINES_BELOW_PATTERN = Pattern
.compile(".*//\\s*violation (\\d+) lines below\\s*(?:['\"](.*)['\"])?$");

/** A pattern to find the string: "'explanation'". */
private static final Pattern MULTIPLE_VIOLATIONS_EXPLANATION_PATTERN = Pattern
.compile(".*['\"](.*)['\"]");

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

Expand Down Expand Up @@ -475,6 +484,8 @@ private static void setViolations(TestInputConfiguration.Builder inputConfigBuil
VIOLATION_SOME_LINES_ABOVE_PATTERN.matcher(lines.get(lineNo));
final Matcher violationSomeLinesBelowMatcher =
VIOLATION_SOME_LINES_BELOW_PATTERN.matcher(lines.get(lineNo));
final Matcher multipleViolationsBlockBelowMatcher =
MULTIPLE_VIOLATIONS_BLOCK_BELOW_PATTERN.matcher(lines.get(lineNo));
if (violationMatcher.matches()) {
final String violationMessage = violationMatcher.group(1);
final int violationLineNum = lineNo + 1;
Expand Down Expand Up @@ -555,6 +566,21 @@ else if (useFilteredViolations) {
setFilteredViolation(inputConfigBuilder, lineNo + 1,
lines.get(lineNo), specifyViolationMessage);
}
else if (multipleViolationsBlockBelowMatcher.matches()) {
final int numberOfViolations = Integer.parseInt(multipleViolationsBlockBelowMatcher.group(1));
AtomicInteger idx = new AtomicInteger(1);
Collections
.nCopies(numberOfViolations,
lineNo + numberOfViolations + 1)
.forEach(actualLineNumber -> {
final Matcher multipleViolationsExplanationPattern =
MULTIPLE_VIOLATIONS_EXPLANATION_PATTERN.matcher(lines.get(lineNo + idx.get()));
final String violationMessageBelow =
multipleViolationsExplanationPattern.group(0);
inputConfigBuilder.addViolation(actualLineNumber,violationMessageBelow );
idx.getAndIncrement();
});
}
}

private static void setFilteredViolation(TestInputConfiguration.Builder inputConfigBuilder,
Expand Down
Loading