Skip to content

Commit

Permalink
Fix crash when ExpectedExceptionChecker finds a thrown.expect(...) fo…
Browse files Browse the repository at this point in the history
…llowed only by assertion-like methods.

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=153011866
  • Loading branch information
nick-someone authored and cushon committed Apr 20, 2017
1 parent f5d7aa2 commit 831df35
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Expand Up @@ -53,14 +53,14 @@ protected Description handleMatch(
BaseFix baseFix = buildBaseFix(state, expectations); BaseFix baseFix = buildBaseFix(state, expectations);
// provide fixes to wrap each of the trailing statements in a lambda // provide fixes to wrap each of the trailing statements in a lambda
// skip statements that look like assertions // skip statements that look like assertions
List<Fix> fixes = ImmutableList<Fix> fixes =
Lists.reverse(suffix) Lists.reverse(suffix)
.stream() .stream()
.filter(t -> !JUnitMatchers.containsTestMethod(t)) .filter(t -> !JUnitMatchers.containsTestMethod(t))
.map(t -> baseFix.build(ImmutableList.of(t))) .map(t -> baseFix.build(ImmutableList.of(t)))
.collect(toImmutableList()); .collect(toImmutableList());
if (fixes.isEmpty()) { if (fixes.isEmpty()) {
fixes.add(baseFix.build(ImmutableList.of(getLast(suffix)))); fixes = ImmutableList.of(baseFix.build(ImmutableList.of(getLast(suffix))));
} }
return buildDescription(tree).addAllFixes(fixes).build(); return buildDescription(tree).addAllFixes(fixes).build();
} }
Expand Down
Expand Up @@ -329,4 +329,40 @@ public void typedMatcher() throws IOException {
"}") "}")
.doTest(); .doTest();
} }

@Test
public void nothingButAsserts() throws IOException {
BugCheckerRefactoringTestHelper.newInstance(new ExpectedExceptionChecker(), getClass())
.addInputLines(
"in/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"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.expect(RuntimeException.class);",
" assertThat(false).isFalse();",
" assertThat(true).isTrue();",
" }",
"}")
.addOutputLines(
"out/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"import static org.junit.Assert.assertThrows;",
"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 {",
" assertThat(false).isFalse();",
" assertThrows(RuntimeException.class, () -> assertThat(true).isTrue());",
" }",
"}")
.doTest();
}
} }

0 comments on commit 831df35

Please sign in to comment.