Skip to content

Commit

Permalink
Issue #12586: Optimize testing methods to run check one time over a file
Browse files Browse the repository at this point in the history
  • Loading branch information
mahfouz72 committed Mar 5, 2024
1 parent fa8b95c commit cb3f403
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 28 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,7 @@

<!-- all bellow until https://github.com/checkstyle/checkstyle/issues/11446 -->
<exclude>**/CheckerTest.class</exclude>
<exclude>**/WriteTagCheckTest.class</exclude>

<!-- all bellow until https://github.com/checkstyle/checkstyle/issues/14465 -->
<!-- Excludes for createTempFile forbidden method -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,23 @@ protected final void verifyFilterWithInlineConfigParser(String filePath,
testInputConfiguration.createConfigurationWithoutFilters();
final List<TestInputViolation> violationsWithoutFilters =
new ArrayList<>(testInputConfiguration.getViolations());
final List<String> actualViolationsWithoutFilters =
getActualViolationsForFile(configWithoutFilters, filePath);
violationsWithoutFilters.addAll(testInputConfiguration.getFilteredViolations());
Collections.sort(violationsWithoutFilters);
verifyViolations(configWithoutFilters, filePath, violationsWithoutFilters);
verify(configWithoutFilters, filePath, expectedUnfiltered);
verifyViolations(filePath, violationsWithoutFilters, actualViolationsWithoutFilters);
assertWithMessage("Violations for %s differ.", filePath)
.that(actualViolationsWithoutFilters)
.containsExactlyElementsIn(expectedUnfiltered);
final DefaultConfiguration configWithFilters =
testInputConfiguration.createConfiguration();
verifyViolations(configWithFilters, filePath, testInputConfiguration.getViolations());
verify(configWithFilters, filePath, expectedFiltered);
final List<String> actualViolationsWithFilters =
getActualViolationsForFile(configWithFilters, filePath);
verifyViolations(filePath, testInputConfiguration.getViolations(),
actualViolationsWithFilters);
assertWithMessage("Violations for %s differ.", filePath)
.that(actualViolationsWithFilters)
.containsExactlyElementsIn(expectedFiltered);
}

/**
Expand All @@ -229,14 +238,17 @@ protected final void verifyFilterWithInlineConfigParser(String filePath,
* @param expected an array of expected messages.
* @throws Exception if exception occurs during verification process.
*/
protected final void verifyWithInlineConfigParser(String filePath, String... expected)
protected void verifyWithInlineConfigParser(String filePath, String... expected)
throws Exception {
final TestInputConfiguration testInputConfiguration =
InlineConfigParser.parse(filePath);
final DefaultConfiguration parsedConfig =
testInputConfiguration.createConfiguration();
verifyViolations(parsedConfig, filePath, testInputConfiguration.getViolations());
verify(parsedConfig, filePath, expected);
final List<String> actualViolations = getActualViolationsForFile(parsedConfig, filePath);
verifyViolations(filePath, testInputConfiguration.getViolations(), actualViolations);
assertWithMessage("Violations for %s differ.", filePath)
.that(actualViolations)
.containsExactlyElementsIn(expected);
}

/**
Expand Down Expand Up @@ -468,9 +480,8 @@ protected static void execute(Checker checker, String... filenames) throws Excep
* @param testInputViolations List of TestInputViolation objects.
* @throws Exception if exception occurs during verification process.
*/
private void verifyViolations(Configuration config,
String file,
List<TestInputViolation> testInputViolations)
protected void verifyViolations(Configuration config, String file,
List<TestInputViolation> testInputViolations)
throws Exception {
final List<String> actualViolations = getActualViolationsForFile(config, file);
final List<Integer> actualViolationLines = actualViolations.stream()
Expand All @@ -490,6 +501,33 @@ private void verifyViolations(Configuration config,
}
}

/**
* Performs verification of violation lines.
*
* @param file file path.
* @param testInputViolations List of TestInputViolation objects.
* @param actualViolations for a file
*/
private void verifyViolations(String file,
List<TestInputViolation> testInputViolations,
List<String> actualViolations) {
final List<Integer> actualViolationLines = actualViolations.stream()
.map(violation -> violation.substring(0, violation.indexOf(':')))
.map(Integer::valueOf)
.collect(Collectors.toUnmodifiableList());
final List<Integer> expectedViolationLines = testInputViolations.stream()
.map(TestInputViolation::getLineNo)
.collect(Collectors.toUnmodifiableList());
assertWithMessage("Violation lines for %s differ.", file)
.that(actualViolationLines)
.isEqualTo(expectedViolationLines);
for (int index = 0; index < actualViolations.size(); index++) {
assertWithMessage("Actual and expected violations differ.")
.that(actualViolations.get(index))
.matches(testInputViolations.get(index).toRegex());
}
}

/**
* Tests the file with the check config.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ public void testPositionOne() throws Exception {
verifyWithInlineConfigParser(getPath("InputAbstractJavadocPositionOne.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 21, but verifyWithInlineConfigParser verify file twice
.isEqualTo(42);
.isEqualTo(21);
}

@Test
Expand All @@ -207,9 +205,7 @@ public void testPositionTwo() throws Exception {
verifyWithInlineConfigParser(getPath("InputAbstractJavadocPositionTwo.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 29, but verifyWithInlineConfigParser verify file twice
.isEqualTo(58);
.isEqualTo(29);
}

@Test
Expand All @@ -219,9 +215,7 @@ public void testPositionThree() throws Exception {
verifyWithInlineConfigParser(getPath("InputAbstractJavadocPositionThree.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 15, but verifyWithInlineConfigParser verify file twice
.isEqualTo(30);
.isEqualTo(15);
}

@Test
Expand All @@ -232,9 +226,7 @@ public void testPositionWithSinglelineCommentsOne() throws Exception {
getPath("InputAbstractJavadocPositionWithSinglelineCommentsOne.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 21, but verifyWithInlineConfigParser verify file twice
.isEqualTo(42);
.isEqualTo(21);
}

@Test
Expand All @@ -245,9 +237,7 @@ public void testPositionWithSinglelineCommentsTwo() throws Exception {
getPath("InputAbstractJavadocPositionWithSinglelineCommentsTwo.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 29, but verifyWithInlineConfigParser verify file twice
.isEqualTo(58);
.isEqualTo(29);
}

@Test
Expand All @@ -258,9 +248,7 @@ public void testPositionWithSinglelineCommentsThree() throws Exception {
getPath("InputAbstractJavadocPositionWithSinglelineCommentsThree.java"), expected);
assertWithMessage("Invalid number of javadocs")
.that(JavadocCatchCheck.javadocsNumber)
// until https://github.com/checkstyle/checkstyle/issues/12586
// actual javadoc count is 15, but verifyWithInlineConfigParser verify file twice
.isEqualTo(30);
.isEqualTo(15);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.bdd.InlineConfigParser;
import com.puppycrawl.tools.checkstyle.bdd.TestInputConfiguration;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

/**
Expand Down Expand Up @@ -188,6 +191,17 @@ public void testWriteTagRecordsAndCompactCtors() throws Exception {
getNonCompilablePath("InputWriteTagRecordsAndCompactCtors.java"), expected);
}

@Override
protected void verifyWithInlineConfigParser(String filePath, String... expected)
throws Exception {
final TestInputConfiguration testInputConfiguration =
InlineConfigParser.parse(filePath);
final DefaultConfiguration parsedConfig =
testInputConfiguration.createConfiguration();
verifyViolations(parsedConfig, filePath, testInputConfiguration.getViolations());
verify(parsedConfig, filePath, expected);
}

@Override
protected void verify(Checker checker,
File[] processedFiles,
Expand Down

0 comments on commit cb3f403

Please sign in to comment.