Skip to content

Commit ca2126b

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Use canonical class name in exception message rather than Class.toString().
This means we say `unsupported type java.math.BigDecimal` instead of `unsupported type class java.math.BigDecimal`. RELNOTES=n/a PiperOrigin-RevId: 777682119
1 parent 12f4ff2 commit ca2126b

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ public DoubleArrayAsIterable usingTolerance(double tolerance) {
125125

126126
private static double checkedToDouble(Number expected) {
127127
checkNotNull(expected);
128-
checkArgument(
128+
boolean okType =
129129
expected instanceof Double
130130
|| expected instanceof Float
131131
|| expected instanceof Integer
132-
|| expected instanceof Long,
133-
"Expected value in assertion using exact double equality was of unsupported type %s "
134-
+ "(it may not have an exact double representation)",
135-
expected.getClass());
132+
|| expected instanceof Long;
133+
if (!okType) {
134+
throw new IllegalArgumentException(
135+
"Expected value in assertion using exact double equality was of unsupported type "
136+
+ SubjectUtils.longName(expected.getClass())
137+
+ " (it may not have an exact double representation)");
138+
}
136139
if (expected instanceof Long) {
137140
checkArgument(
138141
abs((Long) expected) <= 1L << 53,

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,14 @@ private static float checkedToFloat(Number expected) {
127127
!(expected instanceof Double),
128128
"Expected value in assertion using exact float equality was a double, which is not "
129129
+ "supported as a double may not have an exact float representation");
130-
checkArgument(
131-
expected instanceof Float || expected instanceof Integer || expected instanceof Long,
132-
"Expected value in assertion using exact float equality was of unsupported type %s "
133-
+ "(it may not have an exact float representation)",
134-
expected.getClass());
130+
boolean okType =
131+
expected instanceof Float || expected instanceof Integer || expected instanceof Long;
132+
if (!okType) {
133+
throw new IllegalArgumentException(
134+
"Expected value in assertion using exact float equality was of unsupported type "
135+
+ SubjectUtils.longName(expected.getClass())
136+
+ " (it may not have an exact float representation)");
137+
}
135138
if (expected instanceof Integer) {
136139
checkArgument(
137140
abs((Integer) expected) <= 1 << 24,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public void usingExactEquality_contains_otherTypes_bigIntegerNotSupported() {
531531
.factValue("first exception")
532532
.contains(
533533
"Expected value in assertion using exact double equality was of unsupported type "
534-
+ BigInteger.class
534+
+ BigInteger.class.getCanonicalName()
535535
+ " (it may not have an exact double representation)");
536536
}
537537

@@ -558,7 +558,7 @@ public void usingExactEquality_contains_otherTypes_bigDecimalNotSupported() {
558558
.factValue("first exception")
559559
.contains(
560560
"Expected value in assertion using exact double equality was of unsupported type "
561-
+ BigDecimal.class
561+
+ BigDecimal.class.getCanonicalName()
562562
+ " (it may not have an exact double representation)");
563563
}
564564

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void floatConstants_matchNextAfter() {
5858
assertThat(nextAfter(3.3f + DEFAULT_TOLERANCE, NEGATIVE_INFINITY)).isEqualTo(TOLERABLE_3POINT3);
5959
assertThat(nextAfter(3.3f + DEFAULT_TOLERANCE, POSITIVE_INFINITY))
6060
.isEqualTo(INTOLERABLE_3POINT3);
61-
assertThat(nextAfter(Long.MIN_VALUE, NEGATIVE_INFINITY)).isEqualTo(UNDER_LONG_MIN);
61+
assertThat(nextAfter((float) Long.MIN_VALUE, NEGATIVE_INFINITY)).isEqualTo(UNDER_LONG_MIN);
6262
assertThat(nextAfter(2.2f + DEFAULT_TOLERANCE, NEGATIVE_INFINITY)).isEqualTo(TOLERABLE_2POINT2);
6363
assertThat(nextAfter(2.2f + DEFAULT_TOLERANCE, POSITIVE_INFINITY))
6464
.isEqualTo(INTOLERABLE_2POINT2);
@@ -598,7 +598,7 @@ public void usingExactEquality_contains_otherTypes_bigIntegerNotSupported() {
598598
.factValue("first exception")
599599
.contains(
600600
"Expected value in assertion using exact float equality was of unsupported type "
601-
+ BigInteger.class
601+
+ BigInteger.class.getCanonicalName()
602602
+ " (it may not have an exact float representation)");
603603
}
604604

@@ -630,7 +630,7 @@ public void usingExactEquality_contains_otherTypes_bigDecimalNotSupported() {
630630
.factValue("first exception")
631631
.contains(
632632
"Expected value in assertion using exact float equality was of unsupported type "
633-
+ BigDecimal.class
633+
+ BigDecimal.class.getCanonicalName()
634634
+ " (it may not have an exact float representation)");
635635
}
636636

0 commit comments

Comments
 (0)