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

Add a newline between assertThrows and the following assertions: #1084

Closed
wants to merge 1 commit into from
Closed

Add a newline between assertThrows and the following assertions: #1084

wants to merge 1 commit into from

Conversation

dmitry-timofeev
Copy link
Contributor

Overview

Add a newline after assertThrows with an expression lambda and the following assertions,
so that each statement appears on its own line.

Note

com.google.errorprone.BugCheckerRefactoringTestHelper#addOutputLines does not seem to care about any whitespaces, therefore, I couldn't include a failing test.

@googlebot
Copy link
Collaborator

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

Add a newline after assertThrows with an expression lambda
and the following assertions.
@googlebot
Copy link
Collaborator

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Aug 7, 2018
@cushon
Copy link
Collaborator

cushon commented Aug 7, 2018

We usually run a formatter (e.g.) on any modified lines after running refactorings, instead of trying to make sure the suggested fixes are well formatted. Is that an option for you?

com.google.errorprone.BugCheckerRefactoringTestHelper#addOutputLines does not seem to care about any whitespaces, therefore, I couldn't include a failing test.

By default it does an AST diff and ignores whitespace, but there's an example of a whitespace-sensitive test here:

@dmitry-timofeev
Copy link
Contributor Author

We usually run a formatter (e.g.) on any modified lines after running refactorings, instead of trying to make sure the suggested fixes are well formatted. Is that an option for you?

I see, that makes sense! I certainly do not propose to properly indent the code, but having two statements on a single line results in quite a long line in a patch. Auto-formatting might not work if a user has a build configuration that checks that that the formatting is valid, but does not enforce a canonical formatting. In this case running a formatter that cannot operate on changed lines only might result in unrelated changes.

By default it does an AST diff and ignores whitespace, but there's an example of a whitespace-sensitive test here:

Thank you for the reference! Do you think it is worth including a whitespace-sensitive test in this case or would it be too fragile? I've attached a patch, if it's OK, I would push it to this branch.

positiveMatchingExceptionMessage test
Index: core/src/test/java/com/google/errorprone/bugpatterns/ExpectedExceptionRefactoringTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- core/src/test/java/com/google/errorprone/bugpatterns/ExpectedExceptionRefactoringTest.java	(date 1533650826000)
+++ core/src/test/java/com/google/errorprone/bugpatterns/ExpectedExceptionRefactoringTest.java	(date 1533655406000)
@@ -16,6 +16,8 @@
 
 package com.google.errorprone.bugpatterns;
 
+import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
+
 import com.google.errorprone.BugCheckerRefactoringTestHelper;
 import java.io.IOException;
 import org.junit.Test;
@@ -74,6 +76,51 @@
         .doTest();
   }
 
+  @Test
+  public void positiveMatchingExceptionMessage() throws IOException {
+    testHelper
+        .addInputLines(
+            "in/ExceptionTest.java",
+            "import static org.hamcrest.MatcherAssert.assertThat;",
+            "import static org.hamcrest.Matchers.nullValue;",
+            "import java.util.Arrays;",
+            "",
+            "import org.hamcrest.CoreMatchers;",
+            "import org.junit.Rule;",
+            "import org.junit.Test;",
+            "import org.junit.rules.ExpectedException;",
+            "class ExceptionTest {",
+            "  @Rule ExpectedException thrown = ExpectedException.none();",
+            "  @Test",
+            "  public void test() throws Exception {",
+            "    thrown.expectMessage(nullValue(String.class));",
+            "    thrown.expect(NullPointerException.class);",
+            "    Arrays.asList(null);",
+            "  }",
+            "}")
+        .addOutputLines(
+            "out/ExceptionTest.java",
+            "import static org.hamcrest.MatcherAssert.assertThat;",
+            "import static org.hamcrest.Matchers.nullValue;",
+            "import static org.junit.Assert.assertThrows;",
+            "",
+            "import java.util.Arrays;",
+            "import org.hamcrest.CoreMatchers;",
+            "import org.junit.Rule;",
+            "import org.junit.Test;",
+            "import org.junit.rules.ExpectedException;",
+            "class ExceptionTest {",
+            "",
+            "  @Test",
+            "  public void test() throws Exception {",
+            "",
+            "    NullPointerException thrown = assertThrows(NullPointerException.class, () -> Arrays.asList(null));",
+            "assertThat(thrown.getMessage(), nullValue(String.class));",
+            "  }",
+            "}")
+        .doTest(TEXT_MATCH);
+  }
+
   @Test
   public void noThrowingStatements() throws IOException {
     testHelper

@cushon
Copy link
Collaborator

cushon commented Aug 7, 2018

Do you think it is worth including a whitespace-sensitive test in this case or would it be too fragile?

Thanks, I think skipping the test is OK in this case. I'll merge this change momentarily.

@cushon cushon closed this in c589f53 Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants