Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _more_ lambdas. #1394

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,7 @@ public final void isInStrictOrder(Comparator<?> comparator) {
checkNotNull(comparator);
pairwiseCheck(
"expected to be in strict order",
new PairwiseChecker() {
@Override
public boolean check(@Nullable Object prev, @Nullable Object next) {
return ((Comparator<@Nullable Object>) comparator).compare(prev, next) < 0;
}
});
(prev, next) -> ((Comparator<@Nullable Object>) comparator).compare(prev, next) < 0);
}

/**
Expand All @@ -818,12 +813,7 @@ public final void isInOrder(Comparator<?> comparator) {
checkNotNull(comparator);
pairwiseCheck(
"expected to be in order",
new PairwiseChecker() {
@Override
public boolean check(@Nullable Object prev, @Nullable Object next) {
return ((Comparator<@Nullable Object>) comparator).compare(prev, next) <= 0;
}
});
(prev, next) -> ((Comparator<@Nullable Object>) comparator).compare(prev, next) <= 0);
}

private interface PairwiseChecker {
Expand Down
8 changes: 2 additions & 6 deletions core/src/main/java/com/google/common/truth/MapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,8 @@ public Ordered containsAtLeastEntriesIn(Map<?, ? extends E> expectedMap) {
getCastSubject(),
expectedMap,
allowUnexpected,
new ValueTester<A, E>() {
@Override
public boolean test(A actualValue, E expectedValue) {
return correspondence.safeCompare(actualValue, expectedValue, exceptions);
}
});
(actualValue, expectedValue) ->
correspondence.safeCompare(actualValue, expectedValue, exceptions));
if (diff.isEmpty()) {
// The maps correspond exactly. There's no need to check exceptions here, because if
// Correspondence.compare() threw then safeCompare() would return false and the diff would
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,8 @@ public final Ordered containsAtLeast(
}

private Factory<IterableSubject, Iterable<?>> iterableEntries() {
return new Factory<IterableSubject, Iterable<?>>() {
@Override
public IterableSubject createSubject(FailureMetadata metadata, @Nullable Iterable<?> actual) {
return new IterableEntries(metadata, MultimapSubject.this, checkNotNull(actual));
}
};
return (metadata, actual) ->
new IterableEntries(metadata, MultimapSubject.this, checkNotNull(actual));
}

private static class IterableEntries extends IterableSubject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,8 @@ public DoubleArrayAsIterable usingTolerance(double tolerance) {
Correspondence.from(
// If we were allowed lambdas, this would be:
// (a, e) -> Double.doubleToLongBits(a) == Double.doubleToLongBits(checkedToDouble(e)),
new Correspondence.BinaryPredicate<Double, Number>() {
@Override
public boolean apply(Double actual, Number expected) {
return Double.doubleToLongBits(actual)
== Double.doubleToLongBits(checkedToDouble(expected));
}
},
(actual, expected) ->
Double.doubleToLongBits(actual) == Double.doubleToLongBits(checkedToDouble(expected)),
"is exactly equal to");

private static double checkedToDouble(Number expected) {
Expand Down Expand Up @@ -246,12 +241,7 @@ private IterableSubject iterableSubject() {
* PrimitiveDoubleArraySubject.this.toString(), too, someday?
*/
private Factory<IterableSubject, Iterable<?>> iterablesWithCustomDoubleToString() {
return new Factory<IterableSubject, Iterable<?>>() {
@Override
public IterableSubject createSubject(FailureMetadata metadata, @Nullable Iterable<?> actual) {
return new IterableSubjectWithInheritedToString(metadata, actual);
}
};
return IterableSubjectWithInheritedToString::new;
}

private final class IterableSubjectWithInheritedToString extends IterableSubject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,8 @@ public FloatArrayAsIterable usingTolerance(double tolerance) {
Correspondence.from(
// If we were allowed lambdas, this would be:
// (a, e) -> Float.floatToIntBits(a) == Float.floatToIntBits(checkedToFloat(e)),
new Correspondence.BinaryPredicate<Float, Number>() {

@Override
public boolean apply(Float actual, Number expected) {
return Float.floatToIntBits(actual) == Float.floatToIntBits(checkedToFloat(expected));
}
},
(actual, expected) ->
Float.floatToIntBits(actual) == Float.floatToIntBits(checkedToFloat(expected)),
"is exactly equal to");

private static float checkedToFloat(Number expected) {
Expand Down Expand Up @@ -251,12 +246,7 @@ private IterableSubject iterableSubject() {
* PrimitiveFloatArraySubject.this.toString(), too, someday?
*/
private Factory<IterableSubject, Iterable<?>> iterablesWithCustomFloatToString() {
return new Factory<IterableSubject, Iterable<?>>() {
@Override
public IterableSubject createSubject(FailureMetadata metadata, @Nullable Iterable<?> actual) {
return new IterableSubjectWithInheritedToString(metadata, actual);
}
};
return IterableSubjectWithInheritedToString::new;
}

private final class IterableSubjectWithInheritedToString extends IterableSubject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,9 @@ public final class TruthFailureSubject extends ThrowableSubject {
* ExpectFailure#assertThat}.
*/
public static Factory<TruthFailureSubject, AssertionError> truthFailures() {
return FACTORY;
return (metadata, actual) -> new TruthFailureSubject(metadata, actual, "failure");
}

private static final Factory<TruthFailureSubject, AssertionError> FACTORY =
new Factory<TruthFailureSubject, AssertionError>() {
@Override
public TruthFailureSubject createSubject(
FailureMetadata metadata, @Nullable AssertionError actual) {
return new TruthFailureSubject(metadata, actual, "failure");
}
};

private final @Nullable AssertionError actual;

TruthFailureSubject(
Expand Down
92 changes: 12 additions & 80 deletions core/src/test/java/com/google/common/truth/DoubleSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,8 @@ public void isWithinOf() {
}

private static void assertThatIsWithinFails(double actual, double tolerance, double expected) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(actual).isWithin(tolerance).of(expected);
}
};
AssertionError failure = expectFailure(callback);
AssertionError failure =
expectFailure(whenTesting -> whenTesting.that(actual).isWithin(tolerance).of(expected));
assertThat(failure)
.factKeys()
.containsExactly("expected", "but was", "outside tolerance")
Expand All @@ -121,14 +115,8 @@ public void isNotWithinOf() {
}

private static void assertThatIsNotWithinFails(double actual, double tolerance, double expected) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(actual).isNotWithin(tolerance).of(expected);
}
};
AssertionError failure = expectFailure(callback);
AssertionError failure =
expectFailure(whenTesting -> whenTesting.that(actual).isNotWithin(tolerance).of(expected));
assertThat(failure).factValue("expected not to be").isEqualTo(formatNumericValue(expected));
assertThat(failure).factValue("within tolerance").isEqualTo(formatNumericValue(tolerance));
}
Expand Down Expand Up @@ -362,14 +350,7 @@ public void isEqualTo() {
}

private static void assertThatIsEqualToFails(double actual, double expected) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(actual).isEqualTo(expected);
}
};
expectFailure(callback);
expectFailure(whenTesting -> whenTesting.that(actual).isEqualTo(expected));
}

@Test
Expand All @@ -385,14 +366,7 @@ public void isNotEqualTo() {
}

private static void assertThatIsNotEqualToFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isNotEqualTo(value);
}
};
expectFailure(callback);
expectFailure(whenTesting -> whenTesting.that(value).isNotEqualTo(value));
}

@Test
Expand All @@ -407,14 +381,7 @@ public void isZero() {
}

private static void assertThatIsZeroFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isZero();
}
};
AssertionError failure = expectFailure(callback);
AssertionError failure = expectFailure(whenTesting -> whenTesting.that(value).isZero());
assertThat(failure).factKeys().containsExactly("expected zero", "but was").inOrder();
}

Expand All @@ -430,14 +397,7 @@ public void isNonZero() {
}

private static void assertThatIsNonZeroFails(@Nullable Double value, String factKey) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isNonZero();
}
};
AssertionError failure = expectFailure(callback);
AssertionError failure = expectFailure(whenTesting -> whenTesting.that(value).isNonZero());
assertThat(failure).factKeys().containsExactly(factKey, "but was").inOrder();
}

Expand All @@ -451,14 +411,7 @@ public void isPositiveInfinity() {
}

private static void assertThatIsPositiveInfinityFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isPositiveInfinity();
}
};
expectFailure(callback);
expectFailure(whenTesting -> whenTesting.that(value).isPositiveInfinity());
}

@Test
Expand All @@ -471,14 +424,7 @@ public void isNegativeInfinity() {
}

private static void assertThatIsNegativeInfinityFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isNegativeInfinity();
}
};
expectFailure(callback);
expectFailure(whenTesting -> whenTesting.that(value).isNegativeInfinity());
}

@Test
Expand All @@ -491,14 +437,7 @@ public void isNaN() {
}

private static void assertThatIsNaNFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isNaN();
}
};
expectFailure(callback);
expectFailure(whenTesting -> whenTesting.that(value).isNaN());
}

@Test
Expand All @@ -513,14 +452,7 @@ public void isFinite() {
}

private static void assertThatIsFiniteFails(@Nullable Double value) {
ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double> callback =
new ExpectFailure.SimpleSubjectBuilderCallback<DoubleSubject, Double>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DoubleSubject, Double> expect) {
expect.that(value).isFinite();
}
};
AssertionError failure = expectFailure(callback);
AssertionError failure = expectFailure(whenTesting -> whenTesting.that(value).isFinite());
assertThat(failure).factKeys().containsExactly("expected to be finite", "but was").inOrder();
}

Expand Down
41 changes: 14 additions & 27 deletions core/src/test/java/com/google/common/truth/ExpectWithStackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ public class ExpectWithStackTest {
@Test
public void testExpectTrace_simpleCase() {
verifyAssertionError.setErrorVerifier(
new ErrorVerifier() {
@Override
public void verify(AssertionError expected) {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("3 expectations failed:");
}
expected -> {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("3 expectations failed:");
});

expectWithTrace.that(true).isFalse();
Expand All @@ -51,15 +48,12 @@ public void verify(AssertionError expected) {
@Test
public void testExpectTrace_loop() {
verifyAssertionError.setErrorVerifier(
new ErrorVerifier() {
@Override
public void verify(AssertionError expected) {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("4 expectations failed:");
assertWithMessage("test method name should only show up once with following omitted")
.that(expected.getMessage().split("testExpectTrace_loop"))
.hasLength(2);
}
expected -> {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("4 expectations failed:");
assertWithMessage("test method name should only show up once with following omitted")
.that(expected.getMessage().split("testExpectTrace_loop"))
.hasLength(2);
});

for (int i = 0; i < 4; i++) {
Expand All @@ -70,12 +64,9 @@ public void verify(AssertionError expected) {
@Test
public void testExpectTrace_callerException() {
verifyAssertionError.setErrorVerifier(
new ErrorVerifier() {
@Override
public void verify(AssertionError expected) {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("2 expectations failed:");
}
expected -> {
assertThat(expected.getStackTrace()).hasLength(0);
assertThat(expected).hasMessageThat().startsWith("2 expectations failed:");
});

expectWithTrace.that(true).isFalse();
Expand All @@ -87,14 +78,10 @@ public void verify(AssertionError expected) {
@Test
public void testExpectTrace_onlyCallerException() {
verifyAssertionError.setErrorVerifier(
new ErrorVerifier() {
@Override
public void verify(AssertionError expected) {
expected ->
assertWithMessage("Should throw exception as it is if only caller exception")
.that(expected.getStackTrace().length)
.isAtLeast(2);
}
});
.isAtLeast(2));

expectWithTrace
.that(alwaysFailWithCause(getFirstException("First", getSecondException("Second", null))))
Expand Down
Loading