Skip to content

Commit 5015d69

Browse files
committed
Fix test failures that aries when running under different Java configurations.
TreeDifferTest was relying on the assumption that TreePath.getPath() would find a valid tree path from CompilationUnitTree A to subtree B if that subtree was identical to one found by a traversal of CompilationUnitTree A. This behavior is apparently not guaranteed. JavaSourcesSubjectFactoryTest makes some assumptions about how many diagnostics will be reported before failure. This CL changes it to only look for the specific diagnostics for which it is testing. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=70104902
1 parent 3e4e2bb commit 5015d69

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,18 @@ static final TreeDifference diffCompilationUnits(@Nullable CompilationUnitTree e
108108
}
109109

110110
/**
111-
* Returns a {@code TreeDifference} describing the difference between the two
112-
* sub-{@code Tree}s of the {@code CompilationUnitTree}s provided.
111+
* Returns a {@link TreeDifference} describing the difference between the two
112+
* sub-{@code Tree}s. The trees diffed are the leaves of the {@link TreePath}s
113+
* provided.
113114
*
114-
* @throws IllegalArgumentException if the subtrees given are not members of their respective
115-
* compilation units.
115+
* <p>Used for testing.
116116
*/
117-
static final TreeDifference diffSubtrees(CompilationUnitTree expectedCompilationUnit,
118-
@Nullable Tree expectedSubtree, CompilationUnitTree actualCompilationUnit,
119-
@Nullable Tree actualSubtree) {
117+
static final TreeDifference diffSubtrees(@Nullable TreePath pathToExpected,
118+
@Nullable TreePath pathToActual) {
120119
TreeDifference.Builder diffBuilder = new TreeDifference.Builder();
121120
DiffVisitor diffVisitor = new DiffVisitor(diffBuilder,
122-
expectedCompilationUnit, expectedSubtree, actualCompilationUnit, actualSubtree);
123-
diffVisitor.scan(expectedSubtree, actualSubtree);
121+
pathToExpected, pathToActual);
122+
diffVisitor.scan(pathToExpected.getLeaf(), pathToActual.getLeaf());
124123
return diffBuilder.build();
125124
}
126125

@@ -142,22 +141,14 @@ public DiffVisitor(TreeDifference.Builder diffBuilder) {
142141
}
143142

144143
/**
145-
* Constructs a DiffVisitor whose {@code TreePath}s are initialized with paths constructed
146-
* from the trees provided
147-
*
148-
* @throws IllegalArgumentException if the subtrees given are not members of their respective
149-
* compilation units.
144+
* Constructs a DiffVisitor whose {@code TreePath}s are initialized with the paths
145+
* provided.
150146
*/
151147
public DiffVisitor(TreeDifference.Builder diffBuilder,
152-
CompilationUnitTree expectedCompilationUnit, Tree expectedSubtree,
153-
CompilationUnitTree actualCompilationUnit, Tree actualSubtree) {
148+
TreePath pathToExpected, TreePath pathToActual) {
154149
this.diffBuilder = diffBuilder;
155-
expectedPath = TreePath.getPath(expectedCompilationUnit, expectedSubtree);
156-
actualPath = TreePath.getPath(actualCompilationUnit, actualSubtree);
157-
checkArgument(expectedPath != null, "Couldn't initialize differ because no "
158-
+ "path could be found connecting the node and compilation root given.");
159-
checkArgument(actualPath != null, "Couldn't initialize differ because no "
160-
+ "path could be found connecting the node and compilation root given.");
150+
expectedPath = pathToExpected;
151+
actualPath = pathToActual;
161152
}
162153

163154
/**

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ public void failsToCompile_throwsNotInFile() {
175175
fail();
176176
} catch (VerificationException expected) {
177177
ASSERT.that(expected.getMessage())
178-
.isEqualTo(String.format("Expected an error in %s, but only found errors in %s",
179-
otherFileObject.getName(), ImmutableSet.of(fileObject.getName(),
180-
"(no associated file)")));
178+
.contains(String.format("Expected an error in %s", otherFileObject.getName()));
179+
ASSERT.that(expected.getMessage()).contains(fileObject.getName());
180+
// "(no associated file)")));
181181
}
182182
}
183183

@@ -192,10 +192,10 @@ public void failsToCompile_throwsNotOnLine() {
192192
.in(fileObject).onLine(1);
193193
fail();
194194
} catch (VerificationException expected) {
195+
int actualErrorLine = 18;
195196
ASSERT.that(expected.getMessage())
196-
.isEqualTo(String.format(
197-
"Expected an error on line 1 of %s, but only found errors on line(s) [18]",
198-
fileObject.getName()));
197+
.contains(String.format("Expected an error on line 1 of %s", fileObject.getName()));
198+
ASSERT.that(expected.getMessage()).contains("" + actualErrorLine);
199199
}
200200
}
201201

@@ -210,10 +210,10 @@ public void failsToCompile_throwsNotAtColumn() {
210210
.in(fileObject).onLine(18).atColumn(1);
211211
fail();
212212
} catch (VerificationException expected) {
213+
int actualErrorCol = 8;
213214
ASSERT.that(expected.getMessage())
214-
.isEqualTo(String.format(
215-
"Expected an error at 18:1 of %s, but only found errors at column(s) [8]",
216-
fileObject.getName()));
215+
.contains(String.format("Expected an error at 18:1 of %s", fileObject.getName()));
216+
ASSERT.that(expected.getMessage()).contains("" + actualErrorCol);
217217
}
218218
}
219219

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableList;
2121

2222
import com.sun.source.tree.*;
23+
import com.sun.source.util.TreePath;
2324

2425
import org.junit.Rule;
2526
import org.junit.Test;
@@ -140,27 +141,29 @@ public void scan_sameCompilationUnit() {
140141

141142
@Test
142143
public void scan_identicalMethods() {
143-
ASSERT.that(
144-
TreeDiffer.diffSubtrees(EXPECTED_TREE, baseToStringTree(), ACTUAL_TREE, diffToStringTree())
144+
ASSERT.that(TreeDiffer.diffSubtrees(baseToStringTree(), diffToStringTree())
145145
.isEmpty()).isTrue();
146146
}
147147

148148
@Test
149149
public void scan_differentTypes() {
150-
TreeDifference diff =
151-
TreeDiffer.diffSubtrees(EXPECTED_TREE, EXPECTED_TREE, ACTUAL_TREE, diffToStringTree());
150+
TreeDifference diff = TreeDiffer.diffSubtrees(asPath(EXPECTED_TREE), diffToStringTree());
152151
ASSERT.that(diff.isEmpty()).isFalse();
153152
for (TreeDifference.TwoWayDiff differingNode : diff.getDifferingNodes()) {
154153
ASSERT.that(differingNode.getDetails()).contains("Expected node kind to be");
155154
}
156155
}
157156

158-
private Tree baseToStringTree() {
159-
return MoreTrees.findSubtree(EXPECTED_TREE, Tree.Kind.METHOD, "toString");
157+
private TreePath asPath(CompilationUnitTree compilationUnit) {
158+
return MoreTrees.findSubtreePath(compilationUnit, Tree.Kind.COMPILATION_UNIT);
159+
}
160+
161+
private TreePath baseToStringTree() {
162+
return MoreTrees.findSubtreePath(EXPECTED_TREE, Tree.Kind.METHOD, "toString");
160163
}
161164

162-
private Tree diffToStringTree() {
163-
return MoreTrees.findSubtree(ACTUAL_TREE, Tree.Kind.METHOD, "toString");
165+
private TreePath diffToStringTree() {
166+
return MoreTrees.findSubtreePath(ACTUAL_TREE, Tree.Kind.METHOD, "toString");
164167
}
165168

166169
private static class SimplifiedDiff {

0 commit comments

Comments
 (0)