Skip to content

Commit cb6486c

Browse files
cushonCompile-Testing Team
authored andcommitted
Update TreeDiffer to scan resource variables
RELNOTES=Fix false negative when comparing source files that differ only in try-with-resources specifications. PiperOrigin-RevId: 371001119
1 parent a2702ef commit cb6486c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/main/java/com/google/testing/compile/TreeDiffer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ public Void visitTry(TryTree expected, Tree actual) {
831831
return null;
832832
}
833833

834+
parallelScan(expected.getResources(), other.get().getResources());
834835
scan(expected.getBlock(), other.get().getBlock());
835836
parallelScan(expected.getCatches(), other.get().getCatches());
836837
scan(expected.getFinallyBlock(), other.get().getFinallyBlock());

src/test/java/com/google/testing/compile/TreeDifferTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ public class TreeDifferTest {
155155
" };",
156156
"}");
157157

158+
private static final CompilationUnitTree TRY_WITH_RESOURCES_1 =
159+
MoreTrees.parseLinesToTree("package test;",
160+
"final class TestClass {",
161+
" void f() {",
162+
" try (Resource1 r = new Resource1()) {}",
163+
" }",
164+
"}");
165+
166+
private static final CompilationUnitTree TRY_WITH_RESOURCES_2 =
167+
MoreTrees.parseLinesToTree("package test;",
168+
"final class TestClass {",
169+
" void f() {",
170+
" try (Resource2 r = new Resource2()) {}",
171+
" }",
172+
"}");
173+
158174
@Test
159175
public void scan_differingCompilationUnits() {
160176
TreeDifference diff = TreeDiffer.diffCompilationUnits(EXPECTED_TREE, ACTUAL_TREE);
@@ -284,6 +300,20 @@ public void scan_testLambdaVersusAnonymousClass() {
284300
assertThat(diff.isEmpty()).isFalse();
285301
}
286302

303+
@Test
304+
public void scan_testTryWithResources() {
305+
TreeDifference diff =
306+
TreeDiffer.diffCompilationUnits(TRY_WITH_RESOURCES_1, TRY_WITH_RESOURCES_1);
307+
assertThat(diff.isEmpty()).isTrue();
308+
}
309+
310+
@Test
311+
public void scan_testTryWithResourcesDifferent() {
312+
TreeDifference diff =
313+
TreeDiffer.diffCompilationUnits(TRY_WITH_RESOURCES_1, TRY_WITH_RESOURCES_2);
314+
assertThat(diff.isEmpty()).isFalse();
315+
}
316+
287317
@Test
288318
public void matchCompilationUnits() {
289319
ParseResult actual =

0 commit comments

Comments
 (0)