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 #9142: finalize #11151

Merged
merged 1 commit into from
Jan 9, 2022
Merged

Issue #9142: finalize #11151

merged 1 commit into from
Jan 9, 2022

Conversation

rnveach
Copy link
Member

@rnveach rnveach commented Jan 3, 2022

Closes #9142

Doing a search on org.junit.jupiter.api.Assertions only provides the following results:

M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\ant\CheckstyleAntTaskTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\checks\header\HeaderCheckTest.java (1 hit)
	Line 25: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\checks\imports\ImportControlLoaderTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\checks\javadoc\AbstractJavadocCheckTest.java (1 hit)
	Line 30: import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\checks\regexp\RegexpOnFilenameCheckTest.java (1 hit)
	Line 25: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\ConfigurationLoaderTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\DefaultLoggerTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\PropertyCacheFileTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\utils\ChainedPropertyUtilTest.java (1 hit)
	Line 24: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\utils\CommonUtilTest.java (1 hit)
	Line 25: import static org.junit.jupiter.api.Assertions.assertThrows;
  M:\checkstyleWorkspace\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\xpath\AttributeNodeTest.java (1 hit)
	Line 23: import static org.junit.jupiter.api.Assertions.assertThrows;

@@ -75,7 +73,9 @@ public void testLoadFromUrl() throws Exception {
}
}

assumeTrue(actualFilterSet != null, "No Internet connection.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assumeXXX should not be replaced with an assert.
This test requires an internet connection, which may not be available.
This can be replaced with if, but an assume method is better, since it will output the reason to the tests log.

Copy link
Member Author

@rnveach rnveach Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not understanding why it can't be replaced. Can you provide clarification on why we can't use assert here?

Why aren't the 2 equal?

assumeTrue(variable, "Text");
assertWithMessage("Text").that(variable).isTrue();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false conditions in assume methods will not break the build. It simply reports that the test is ignored due to "Text".
false condition in assert methods will break the build.

@@ -223,7 +222,9 @@ public void testFinishLocalSetup() {
@Test
public void testLanguageIsValid() {
final String language = DEFAULT_LOCALE.getLanguage();
assumeFalse(language.isEmpty(), "Locale not set");
assertWithMessage("Locale not set")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -235,7 +236,9 @@ public void testLanguageIsValid() {
@Test
public void testCountryIsValid() {
final String country = DEFAULT_LOCALE.getCountry();
assumeFalse(country.isEmpty(), "Locale not set");
assertWithMessage("Locale not set")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -248,8 +251,9 @@ public void testCountryIsValid() {
@Test
public void testLocaleIsSupported() throws Exception {
final String language = DEFAULT_LOCALE.getLanguage();
assumeFalse(language.isEmpty() || Locale.ENGLISH.getLanguage().equals(language),
"Custom locale not set");
assertWithMessage("Custom locale not set")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -251,22 +250,28 @@ protected final void verify(Checker checker,
for (int i = 0; i < expected.length; i++) {
final String expectedResult = messageFileName + ":" + expected[i];
final String actual = lnr.readLine();
assertEquals(expectedResult, actual, "error message " + i);
assertWithMessage("error message " + i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use formatted message here:

assertWithMessage("error message %s", i)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is better about it? The readability or is there some performance?

Copy link
Member

@pbludov pbludov Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both assertWithMessage("error message %s", i) and `assertWithMessage("error message " + i) have the same readability and performance.

The difference is that "error message " + i is actually String.concat("error message "), i.toString()). And this .toString() method can (in theory) have side effects. For example, it may throw an NPE, which will cause the test to fail for no clear reason.

This is absolutely inapplicable to any primitive type such as int, but this is a style issue. If we all agree to use this style, it will avoid possible problems in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about #11151 (comment) ?

|| theWarnings.remove((Integer) lineNumber),
"input file is expected to have a warning comment on line number "
+ lineNumber);
assertWithMessage("input file is expected to have a warning comment on line number "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member

@pbludov pbludov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor change please

assertEquals(expected.length,
errs, "unexpected output: " + lnr.readLine());
assertEquals(0, theWarnings.size(), "unexpected warnings " + theWarnings);
assertWithMessage("unexpected output: " + lnr.readLine())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

assertWithMessage("unexpected output: " + lnr.readLine())
.that(errs)
.isEqualTo(expected.length);
assertWithMessage("unexpected warnings " + theWarnings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@@ -154,7 +153,8 @@ public void testGetAccessModifierFromModifiersTokenWrongTokenType() {

try {
CheckUtil.getAccessModifierFromModifiersToken(modifiers);
fail(IllegalArgumentException.class.getSimpleName() + " was expected.");
assertWithMessage(IllegalArgumentException.class.getSimpleName() + " was expected.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

if (!node.isPresent()) {
fail("Cannot find node of specified type: " + type);
}
assertWithMessage("Cannot find node of specified type: " + type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@rnveach
Copy link
Member Author

rnveach commented Jan 9, 2022

@pbludov all done

@pbludov pbludov merged commit a1c3c35 into checkstyle:master Jan 9, 2022
@rnveach rnveach deleted the issue_9142_25 branch January 9, 2022 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infra: migrate to Truth in tests
3 participants