Skip to content

Commit f2047a9

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Merge java8 tests into core, and finish migrating core off the legacy ExpectFailure API.
Plus, actually run the `java8` tests under Android and J2CL. And tweak `OptionalDoubleSubject` to produce a consistent number format even under J2CL. RELNOTES=n/a PiperOrigin-RevId: 733349573
1 parent 7d8a09d commit f2047a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+142
-179
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.truth.Fact.fact;
1919
import static com.google.common.truth.Fact.simpleFact;
20+
import static com.google.common.truth.Platform.doubleToString;
2021

2122
import java.util.OptionalDouble;
2223
import org.jspecify.annotations.Nullable;
@@ -57,7 +58,7 @@ public void isEmpty() {
5758
} else if (actual.isPresent()) {
5859
failWithoutActual(
5960
simpleFact("expected to be empty"),
60-
fact("but was present with value", actual.getAsDouble()));
61+
fact("but was present with value", doubleToString(actual.getAsDouble())));
6162
}
6263
}
6364

@@ -73,7 +74,8 @@ public void hasValue(double expected) {
7374
if (actual == null) {
7475
failWithActual("expected an optional with value", expected);
7576
} else if (!actual.isPresent()) {
76-
failWithoutActual(fact("expected to have value", expected), simpleFact("but was absent"));
77+
failWithoutActual(
78+
fact("expected to have value", doubleToString(expected)), simpleFact("but was absent"));
7779
} else {
7880
checkNoNeedToDisplayBothValues("getAsDouble()")
7981
.that(actual.getAsDouble())

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

-33
This file was deleted.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.google.common.truth;
1717

1818
import static com.google.common.truth.ExpectFailure.expectFailure;
19+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
20+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
1921
import static com.google.common.truth.Truth.assertThat;
2022
import static java.math.BigDecimal.TEN;
2123

@@ -30,7 +32,7 @@
3032
* @author Kurt Alfred Kluever
3133
*/
3234
@RunWith(JUnit4.class)
33-
public class BigDecimalSubjectTest extends BaseSubjectTestCase {
35+
public class BigDecimalSubjectTest {
3436
@SuppressWarnings("TruthSelfEquals")
3537
@Test
3638
public void isEqualTo() {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.google.common.truth;
1717

1818
import static com.google.common.truth.ExpectFailure.expectFailure;
19+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
20+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
1921
import static com.google.common.truth.Truth.assertThat;
2022

2123
import org.junit.Test;
@@ -28,7 +30,7 @@
2830
* @author Christian Gruber ([email protected])
2931
*/
3032
@RunWith(JUnit4.class)
31-
public class BooleanSubjectTest extends BaseSubjectTestCase {
33+
public class BooleanSubjectTest {
3234

3335
@Test
3436
public void isTrue() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
/** Tests for chained subjects (produced with {@link Subject#check(String, Object...)}, etc.). */
2828
@RunWith(JUnit4.class)
29-
public final class ChainingTest extends BaseSubjectTestCase {
29+
public final class ChainingTest {
3030
private static final Throwable throwable = new Throwable("root");
3131

3232
@Test

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.common.truth;
1717

1818
import static com.google.common.truth.ExpectFailure.expectFailure;
19+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
1920
import static com.google.common.truth.Truth.assertThat;
2021

2122
import org.junit.Test;
@@ -28,7 +29,7 @@
2829
* @author Christian Gruber ([email protected])
2930
*/
3031
@RunWith(JUnit4.class)
31-
public class ClassSubjectTest extends BaseSubjectTestCase {
32+
public class ClassSubjectTest {
3233
@Test
3334
public void testIsAssignableTo_same() {
3435
assertThat(String.class).isAssignableTo(String.class);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919
import static com.google.common.truth.ExpectFailure.assertThat;
2020
import static com.google.common.truth.ExpectFailure.expectFailure;
21+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
2122
import static com.google.common.truth.Truth.assertThat;
2223
import static com.google.common.truth.Truth.assertWithMessage;
2324
import static org.junit.Assert.fail;
@@ -33,7 +34,7 @@
3334
* @author Kurt Alfred Kluever
3435
*/
3536
@RunWith(JUnit4.class)
36-
public class ComparableSubjectTest extends BaseSubjectTestCase {
37+
public class ComparableSubjectTest {
3738

3839
@Test
3940
// test of a mistaken call and of unnecessary use of isEquivalentAccordingToCompareTo

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @author Pete Gillin
3333
*/
3434
@RunWith(JUnit4.class)
35-
public final class CorrespondenceExceptionStoreTest extends BaseSubjectTestCase {
35+
public final class CorrespondenceExceptionStoreTest {
3636

3737
@Test
3838
public void hasCompareException_empty() {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static com.google.common.truth.Correspondence.tolerance;
2020
import static com.google.common.truth.ExpectFailure.assertThat;
2121
import static com.google.common.truth.ExpectFailure.expectFailure;
22+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
23+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
2224
import static com.google.common.truth.TestCorrespondences.INT_DIFF_FORMATTER;
2325
import static com.google.common.truth.Truth.assertThat;
2426
import static java.util.Arrays.asList;
@@ -35,7 +37,7 @@
3537
* @author Pete Gillin
3638
*/
3739
@RunWith(JUnit4.class)
38-
public final class CorrespondenceTest extends BaseSubjectTestCase {
40+
public final class CorrespondenceTest {
3941
// Tests of the abstract base class (just assert that equals and hashCode throw).
4042

4143
private static final Correspondence<Object, Object> INSTANCE =

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.common.truth;
1717

18+
import static com.google.common.truth.ExpectFailure.expectFailure;
1819
import static com.google.common.truth.Truth.assertThat;
1920
import static com.google.common.truth.Truth.assertWithMessage;
2021
import static com.google.common.truth.Truth.assert_;
@@ -31,14 +32,15 @@
3132
*/
3233
@SuppressWarnings("LenientFormatStringValidation") // Intentional for testing
3334
@RunWith(JUnit4.class)
34-
public class CustomFailureMessageTest extends BaseSubjectTestCase {
35+
public class CustomFailureMessageTest {
3536

3637
@Test
3738
public void assertWithMessageThat() {
38-
expectFailure.whenTesting().withMessage("This is a custom message").that(false).isTrue();
39-
assertThat(expectFailure.getFailure())
40-
.hasMessageThat()
41-
.startsWith("This is a custom message\n");
39+
AssertionError e =
40+
expectFailure(
41+
whenTesting ->
42+
whenTesting.withMessage("This is a custom message").that(false).isTrue());
43+
assertThat(e).hasMessageThat().startsWith("This is a custom message\n");
4244
}
4345

4446
@Test
@@ -59,14 +61,14 @@ public void countPlaceholders() {
5961

6062
@Test
6163
public void assertWithMessageThat_withPlaceholders() {
62-
expectFailure
63-
.whenTesting()
64-
.withMessage("This is a %s %s", "custom", "message")
65-
.that(false)
66-
.isTrue();
67-
assertThat(expectFailure.getFailure())
68-
.hasMessageThat()
69-
.startsWith("This is a custom message\n");
64+
AssertionError e =
65+
expectFailure(
66+
whenTesting ->
67+
whenTesting
68+
.withMessage("This is a %s %s", "custom", "message")
69+
.that(false)
70+
.isTrue());
71+
assertThat(e).hasMessageThat().startsWith("This is a custom message\n");
7072
}
7173

7274
@Test

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.truth.ExpectFailure.assertThat;
1919
import static com.google.common.truth.Fact.formatNumericValue;
20+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
2021
import static com.google.common.truth.Truth.assertThat;
2122
import static org.junit.Assert.fail;
2223

@@ -34,7 +35,7 @@
3435
* @author Kurt Alfred Kluever
3536
*/
3637
@RunWith(JUnit4.class)
37-
public class DoubleSubjectTest extends BaseSubjectTestCase {
38+
public class DoubleSubjectTest {
3839

3940
private static final double NEARLY_MAX = 1.7976931348623155E308;
4041
private static final double NEGATIVE_NEARLY_MAX = -1.7976931348623155E308;

extensions/java8/src/test/java/com/google/common/truth/FailureAssertions.java core/src/test/java/com/google/common/truth/FailureAssertions.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static com.google.common.truth.ExpectFailure.assertThat;
1919

20-
/** Convenience methods for Java 8 Truth Subject tests, similar to BaseSubjectTestCase. */
20+
/** Convenience methods for {@link Subject} tests. */
2121
final class FailureAssertions {
2222
static void assertFailureKeys(AssertionError e, String... keys) {
2323
assertThat(e).factKeys().containsExactlyElementsIn(keys).inOrder();

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.truth.ExpectFailure.assertThat;
1919
import static com.google.common.truth.Fact.formatNumericValue;
20+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
2021
import static com.google.common.truth.Truth.assertThat;
2122
import static org.junit.Assert.fail;
2223

@@ -34,7 +35,7 @@
3435
* @author Kurt Alfred Kluever
3536
*/
3637
@RunWith(JUnit4.class)
37-
public class FloatSubjectTest extends BaseSubjectTestCase {
38+
public class FloatSubjectTest {
3839
private static final float NEARLY_MAX = 3.4028233E38f;
3940
private static final float NEGATIVE_NEARLY_MAX = -3.4028233E38f;
4041
private static final float JUST_OVER_MIN = 2.8E-45f;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package com.google.common.truth;
1717

1818
import static com.google.common.truth.ExpectFailure.expectFailure;
19+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
20+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
1921
import static com.google.common.truth.Truth.assertThat;
2022
import static org.junit.Assert.fail;
2123

@@ -30,7 +32,7 @@
3032
* @author Christian Gruber ([email protected])
3133
*/
3234
@RunWith(JUnit4.class)
33-
public class GuavaOptionalSubjectTest extends BaseSubjectTestCase {
35+
public class GuavaOptionalSubjectTest {
3436

3537
@Test
3638
public void isPresent() {

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Kurt Alfred Kluever
3535
*/
3636
@RunWith(JUnit4.class)
37-
public class IntegerSubjectTest extends BaseSubjectTestCase {
37+
public class IntegerSubjectTest {
3838

3939
@Test
4040
@SuppressWarnings("TruthSelfEquals")
@@ -50,17 +50,17 @@ public void simpleInequality() {
5050
@Test
5151
public void equalityWithLongs() {
5252
assertThat(0).isEqualTo(0L);
53-
expectFailureWhenTestingThat(0).isNotEqualTo(0L);
53+
expectFailure(whenTesting -> whenTesting.that(0).isNotEqualTo(0L));
5454
}
5555

5656
@Test
5757
public void equalityFail() {
58-
expectFailureWhenTestingThat(4).isEqualTo(5);
58+
expectFailure(whenTesting -> whenTesting.that(4).isEqualTo(5));
5959
}
6060

6161
@Test
6262
public void inequalityFail() {
63-
expectFailureWhenTestingThat(4).isNotEqualTo(4);
63+
expectFailure(whenTesting -> whenTesting.that(4).isNotEqualTo(4));
6464
}
6565

6666
@Test
@@ -70,12 +70,12 @@ public void equalityOfNulls() {
7070

7171
@Test
7272
public void equalityOfNullsFail_nullActual() {
73-
expectFailureWhenTestingThat(null).isEqualTo(5);
73+
expectFailure(whenTesting -> whenTesting.that(null).isEqualTo(5));
7474
}
7575

7676
@Test
7777
public void equalityOfNullsFail_nullExpected() {
78-
expectFailureWhenTestingThat(5).isEqualTo(null);
78+
expectFailure(whenTesting -> whenTesting.that(5).isEqualTo(null));
7979
}
8080

8181
@Test
@@ -86,7 +86,7 @@ public void inequalityOfNulls() {
8686

8787
@Test
8888
public void inequalityOfNullsFail() {
89-
expectFailureWhenTestingThat(null).isNotEqualTo(null);
89+
expectFailure(whenTesting -> whenTesting.that(null).isNotEqualTo(null));
9090
}
9191

9292
@Test
@@ -103,22 +103,24 @@ public void overflowOnPrimitives() {
103103

104104
@Test
105105
public void overflowOnPrimitives_shouldBeEqualAfterCast_min() {
106-
expectFailureWhenTestingThat(Integer.MIN_VALUE).isNotEqualTo((long) Integer.MIN_VALUE);
106+
expectFailure(
107+
whenTesting -> whenTesting.that(Integer.MIN_VALUE).isNotEqualTo((long) Integer.MIN_VALUE));
107108
}
108109

109110
@Test
110111
public void overflowOnPrimitives_shouldBeEqualAfterCast_max() {
111-
expectFailureWhenTestingThat(Integer.MAX_VALUE).isNotEqualTo((long) Integer.MAX_VALUE);
112+
expectFailure(
113+
whenTesting -> whenTesting.that(Integer.MAX_VALUE).isNotEqualTo((long) Integer.MAX_VALUE));
112114
}
113115

114116
@Test
115117
public void overflowBetweenIntegerAndLong_shouldBeDifferent_min() {
116-
expectFailureWhenTestingThat(Integer.MIN_VALUE).isEqualTo(Long.MIN_VALUE);
118+
expectFailure(whenTesting -> whenTesting.that(Integer.MIN_VALUE).isEqualTo(Long.MIN_VALUE));
117119
}
118120

119121
@Test
120122
public void overflowBetweenIntegerAndLong_shouldBeDifferent_max() {
121-
expectFailureWhenTestingThat(Integer.MAX_VALUE).isEqualTo(Long.MAX_VALUE);
123+
expectFailure(whenTesting -> whenTesting.that(Integer.MAX_VALUE).isEqualTo(Long.MAX_VALUE));
122124
}
123125

124126
@Test
@@ -220,8 +222,4 @@ private static AssertionError expectFailure(
220222
SimpleSubjectBuilderCallback<IntegerSubject, Integer> callback) {
221223
return ExpectFailure.expectFailureAbout(IntegerSubject::new, callback);
222224
}
223-
224-
private IntegerSubject expectFailureWhenTestingThat(Integer actual) {
225-
return expectFailure.whenTesting().that(actual);
226-
}
227225
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import static com.google.common.truth.Correspondence.tolerance;
2222
import static com.google.common.truth.ExpectFailure.assertThat;
2323
import static com.google.common.truth.ExpectFailure.expectFailure;
24+
import static com.google.common.truth.FailureAssertions.assertFailureKeys;
25+
import static com.google.common.truth.FailureAssertions.assertFailureValue;
26+
import static com.google.common.truth.FailureAssertions.assertFailureValueIndexed;
2427
import static com.google.common.truth.TestCorrespondences.CASE_INSENSITIVE_EQUALITY;
2528
import static com.google.common.truth.TestCorrespondences.CASE_INSENSITIVE_EQUALITY_HALF_NULL_SAFE;
2629
import static com.google.common.truth.TestCorrespondences.NULL_SAFE_RECORD_ID;
@@ -55,7 +58,7 @@
5558
* @author Pete Gillin
5659
*/
5760
@RunWith(JUnit4.class)
58-
public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
61+
public class IterableSubjectCorrespondenceTest {
5962

6063
@Test
6164
// test of a mistaken call

0 commit comments

Comments
 (0)