Skip to content

Commit 7261f72

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Make ActualValueInference see through casts and also recognize some of Kotlin's collection-factory methods as having names that are "boring."
RELNOTES=n/a PiperOrigin-RevId: 803004852
1 parent 3caa0e8 commit 7261f72

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

core/src/main/java/com/google/common/truth/ActualValueInference.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ StackEntry actualValue() {
149149
@Nullable String description() {
150150
return null;
151151
}
152+
153+
abstract StackEntry withType(InferredType newType);
152154
}
153155

154156
/** An entry that we know nothing about except for its type. */
@@ -157,6 +159,11 @@ StackEntry actualValue() {
157159
@GwtIncompatible
158160
@J2ktIncompatible
159161
abstract static class OpaqueEntry extends StackEntry {
162+
@Override
163+
StackEntry withType(InferredType newType) {
164+
return opaque(newType);
165+
}
166+
160167
@Override
161168
public final String toString() {
162169
return "unknown";
@@ -177,6 +184,11 @@ private static StackEntry opaque(InferredType type) {
177184
@GwtIncompatible
178185
@J2ktIncompatible
179186
abstract static class DescribedEntry extends StackEntry {
187+
@Override
188+
StackEntry withType(InferredType newType) {
189+
return described(newType, description());
190+
}
191+
180192
@Override
181193
abstract String description();
182194

@@ -203,6 +215,11 @@ private static StackEntry described(InferredType type, String description) {
203215
@GwtIncompatible
204216
@J2ktIncompatible
205217
abstract static class SubjectEntry extends StackEntry {
218+
@Override
219+
StackEntry withType(InferredType newType) {
220+
return subjectFor(newType, actualValue());
221+
}
222+
206223
@Override
207224
abstract StackEntry actualValue();
208225

@@ -698,8 +715,7 @@ public void visitTypeInsn(int opcode, String type) {
698715
pushDescriptor('[' + descriptor);
699716
break;
700717
case Opcodes.CHECKCAST:
701-
pop();
702-
pushDescriptor(descriptor);
718+
push(pop().withType(InferredType.create(convertToDescriptor(type))));
703719
break;
704720
case Opcodes.INSTANCEOF:
705721
pop();
@@ -1445,6 +1461,8 @@ public void visit(
14451461
*/
14461462
private static final ImmutableSet<String> BORING_NAMES =
14471463
ImmutableSet.of(
1464+
// keep-sorted start
1465+
"_build",
14481466
"asList",
14491467
"build",
14501468
"collect",
@@ -1453,10 +1471,17 @@ public void visit(
14531471
"from",
14541472
"get",
14551473
"iterator",
1474+
"listOf",
1475+
"mapOf",
14561476
"of",
1477+
"setOf",
1478+
"sortedMapOf",
1479+
"sortedSetOf",
14571480
"toArray",
14581481
"toString",
1459-
"valueOf");
1482+
"valueOf"
1483+
// keep-sorted end
1484+
);
14601485

14611486
private static boolean isThatOrAssertThat(String owner, String name) {
14621487
/*

core/src/test/java/com/google/common/truth/ActualValueInferenceTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public void simple() {
6161
assertThat(e).factValue("value of").isEqualTo("instanceOneArg(...)");
6262
}
6363

64+
@Test
65+
public void cast() {
66+
AssertionError e;
67+
68+
e = expectFailure(whenTesting -> whenTesting.that((String) secretlyString()).isEqualTo("b"));
69+
assertThat(e).factValue("value of").isEqualTo("secretlyString()");
70+
}
71+
6472
@Test
6573
public void autoBox() {
6674
AssertionError e;
@@ -264,4 +272,8 @@ char someChar() {
264272
String someNumberString() {
265273
return "0";
266274
}
275+
276+
Object secretlyString() {
277+
return "a";
278+
}
267279
}

0 commit comments

Comments
 (0)