Skip to content

Commit

Permalink
Assorted simplifications.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 724381458
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Feb 7, 2025
1 parent bf125c6 commit c5e21b9
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 124 deletions.
69 changes: 27 additions & 42 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public final void hasSize(int expectedSize) {
/** Checks (with a side-effect failure) that the subject contains the supplied item. */
public final void contains(@Nullable Object element) {
if (!Iterables.contains(checkNotNull(actual), element)) {
List<@Nullable Object> elementList = newArrayList(element);
List<@Nullable Object> elementList = asList(element);
if (hasMatchingToStringPair(actual, elementList)) {
failWithoutActual(
fact("expected to contain", element),
Expand All @@ -198,7 +198,7 @@ public final void doesNotContain(@Nullable Object element) {

/** Checks that the subject does not contain duplicate elements. */
public final void containsNoDuplicates() {
List<Multiset.Entry<?>> duplicates = newArrayList();
List<Multiset.Entry<?>> duplicates = new ArrayList<>();
for (Multiset.Entry<?> entry : LinkedHashMultiset.create(checkNotNull(actual)).entrySet()) {
if (entry.getCount() > 1) {
duplicates.add(entry);
Expand Down Expand Up @@ -280,13 +280,15 @@ public final Ordered containsAtLeast(
* on the object returned by this method. The expected elements must appear in the given order
* within the actual elements, but they are not required to be consecutive.
*/
// Some builder calls need to be separate, so let's keep them all separate.
@SuppressWarnings("BuilderCollapser")
@CanIgnoreReturnValue
public final Ordered containsAtLeastElementsIn(@Nullable Iterable<?> expectedIterable) {
List<?> actual = Lists.newLinkedList(checkNotNull(this.actual));
Collection<?> expected = iterableToCollection(expectedIterable);

List<@Nullable Object> missing = newArrayList();
List<@Nullable Object> actualNotInOrder = newArrayList();
List<@Nullable Object> missing = new ArrayList<>();
List<@Nullable Object> actualNotInOrder = new ArrayList<>();

boolean ordered = true;
// step through the expected elements...
Expand All @@ -312,21 +314,17 @@ public final Ordered containsAtLeastElementsIn(@Nullable Iterable<?> expectedIte

return ordered
? IN_ORDER
: new Ordered() {
@Override
public void inOrder() {
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
facts.add(simpleFact("required elements were all found, but order was wrong"));
facts.add(fact("expected order for required elements", expected));
List<Object> actualOrder =
Lists.newArrayList(checkNotNull(IterableSubject.this.actual));
if (actualOrder.retainAll(expected)) {
facts.add(fact("but order was", actualOrder));
facts.add(fullContents());
failWithoutActual(facts.build());
} else {
failWithActual(facts.build());
}
: () -> {
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
facts.add(simpleFact("required elements were all found, but order was wrong"));
facts.add(fact("expected order for required elements", expected));
List<Object> actualOrder = newArrayList(checkNotNull(IterableSubject.this.actual));
if (actualOrder.retainAll(expected)) {
facts.add(fact("but order was", actualOrder));
facts.add(fullContents());
failWithoutActual(facts.build());
} else {
failWithActual(facts.build());
}
};
}
Expand Down Expand Up @@ -396,7 +394,7 @@ private static void moveElements(
@CanIgnoreReturnValue
public final Ordered containsExactly(@Nullable Object @Nullable ... varargs) {
List<@Nullable Object> expected =
(varargs == null) ? newArrayList((@Nullable Object) null) : asList(varargs);
varargs == null ? asList((@Nullable Object) null) : asList(varargs);
return containsExactlyElementsIn(
expected, varargs != null && varargs.length == 1 && varargs[0] instanceof Iterable);
}
Expand Down Expand Up @@ -484,12 +482,12 @@ private Ordered containsExactlyElementsIn(
return ALREADY_FAILED;
}
// Missing elements; elements that are not missing will be removed as we iterate.
List<@Nullable Object> missing = newArrayList();
List<@Nullable Object> missing = new ArrayList<>();
missing.add(requiredElement);
Iterators.addAll(missing, requiredIter);

// Extra elements that the subject had but shouldn't have.
List<@Nullable Object> extra = newArrayList();
List<@Nullable Object> extra = new ArrayList<>();

// Remove all actual elements from missing, and add any that weren't in missing
// to extra.
Expand All @@ -508,13 +506,9 @@ private Ordered containsExactlyElementsIn(
* This containsExactly() call is a success. But the iterables were not in the same order,
* so return an object that will fail the test if the user calls inOrder().
*/
return new Ordered() {
@Override
public void inOrder() {
return () ->
failWithActual(
simpleFact("contents match, but order was wrong"), fact("expected", required));
}
};
}
return failExactly(required, addElementsInWarning, missing, extra);
}
Expand Down Expand Up @@ -1220,8 +1214,7 @@ public void doesNotContain(E excluded) {
@SafeVarargs
@CanIgnoreReturnValue
public final Ordered containsExactly(@Nullable E @Nullable ... expected) {
return containsExactlyElementsIn(
(expected == null) ? newArrayList((E) null) : asList(expected));
return containsExactlyElementsIn(expected == null ? asList((E) null) : asList(expected));
}

/**
Expand Down Expand Up @@ -1288,17 +1281,13 @@ public Ordered containsExactlyElementsIn(@Nullable Iterable<? extends E> expecte
}
// The 1:1 mapping is complete, so the test succeeds (but we know from above that the mapping
// is not in order).
return new Ordered() {
@Override
public void inOrder() {
return () ->
subject.failWithActual(
ImmutableList.<Fact>builder()
.add(simpleFact("contents match, but order was wrong"))
.add(fact("expected", expected))
.addAll(correspondence.describeForIterable())
.build());
}
};
}

/**
Expand Down Expand Up @@ -1488,7 +1477,7 @@ private ImmutableList<Fact> formatExtras(
// index must be in there once.
return asList();
}
List<T> notIndexed = newArrayList();
List<T> notIndexed = new ArrayList<>();
for (int index = 0; index < list.size(); index++) {
if (!indexes.contains(index)) {
notIndexed.add(list.get(index));
Expand Down Expand Up @@ -1628,17 +1617,13 @@ public Ordered containsAtLeastElementsIn(Iterable<? extends E> expected) {
}
// The 1:1 mapping maps all the expected elements, so the test succeeds (but we know from
// above that the mapping is not in order).
return new Ordered() {
@Override
public void inOrder() {
return () ->
subject.failWithActual(
ImmutableList.<Fact>builder()
.add(simpleFact("required elements were all found, but order was wrong"))
.add(fact("expected order for required elements", expected))
.addAll(correspondence.describeForIterable())
.build());
}
};
}

/**
Expand Down Expand Up @@ -2124,13 +2109,13 @@ private final class Pairing {
* List of the expected values not used in the pairing. Iterates in the order they appear in
* the input.
*/
private final List<E> unpairedExpectedValues = newArrayList();
private final List<E> unpairedExpectedValues = new ArrayList<>();

/**
* List of the actual values not used in the pairing. Iterates in the order they appear in the
* input.
*/
private final List<A> unpairedActualValues = newArrayList();
private final List<A> unpairedActualValues = new ArrayList<>();
}
}
}
10 changes: 1 addition & 9 deletions core/src/test/java/com/google/common/truth/ChainingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,6 @@ public void badFormat() {
*/

private static final class MyObjectSubject extends Subject {
static final Factory<MyObjectSubject, Object> FACTORY =
new Factory<MyObjectSubject, Object>() {
@Override
public MyObjectSubject createSubject(FailureMetadata metadata, Object actual) {
return new MyObjectSubject(metadata, actual);
}
};

private MyObjectSubject(FailureMetadata metadata, Object actual) {
super(metadata, actual);
}
Expand Down Expand Up @@ -272,7 +264,7 @@ MyObjectSubject delegatingToNamedNoNeedToDisplayBoth(Object actual, String name)
}

private static Subject.Factory<MyObjectSubject, Object> myObjects() {
return MyObjectSubject.FACTORY;
return MyObjectSubject::new;
}

private MyObjectSubject expectFailureWhenTestingThat(Object actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,10 @@ public class DoubleSubjectTest extends BaseSubjectTestCase {
private static final double GOLDEN = 1.23;
private static final double OVER_GOLDEN = 1.2300000000000002;

private static final Subject.Factory<DoubleSubject, Double> DOUBLE_SUBJECT_FACTORY =
new Subject.Factory<DoubleSubject, Double>() {
@Override
public DoubleSubject createSubject(FailureMetadata metadata, Double that) {
return new DoubleSubject(metadata, that);
}
};

@CanIgnoreReturnValue
private static AssertionError expectFailure(
SimpleSubjectBuilderCallback<DoubleSubject, Double> callback) {
return ExpectFailure.expectFailureAbout(DOUBLE_SUBJECT_FACTORY, callback);
return ExpectFailure.expectFailureAbout(DoubleSubject::new, callback);
}

@Test
Expand Down
16 changes: 3 additions & 13 deletions core/src/test/java/com/google/common/truth/ExpectFailureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ public void expectFail_whenTestingWithoutInContext_shouldFail() {
}

private static Subject.Factory<StringSubject, String> strings() {
return new Subject.Factory<StringSubject, String>() {
@Override
public StringSubject createSubject(FailureMetadata fm, String that) {
return new StringSubject(fm, that);
}
};
return StringSubject::new;
}

private static class BadSubject extends Subject {
Expand All @@ -163,13 +158,8 @@ public void isEqualTo(Object expected) {
}
}

private static Subject.Factory<BadSubject, Integer> badSubject() {
return new Subject.Factory<BadSubject, Integer>() {
@Override
public BadSubject createSubject(FailureMetadata fm, Integer that) {
return new BadSubject(fm, that);
}
};
private static Factory<BadSubject, Integer> badSubject() {
return BadSubject::new;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,10 @@ public class FloatSubjectTest extends BaseSubjectTestCase {
private static final float GOLDEN = 1.23f;
private static final float JUST_OVER_GOLDEN = 1.2300001f;

private static final Subject.Factory<FloatSubject, Float> FLOAT_SUBJECT_FACTORY =
new Subject.Factory<FloatSubject, Float>() {
@Override
public FloatSubject createSubject(FailureMetadata metadata, Float that) {
return new FloatSubject(metadata, that);
}
};

@CanIgnoreReturnValue
private static AssertionError expectFailure(
SimpleSubjectBuilderCallback<FloatSubject, Float> callback) {
return ExpectFailure.expectFailureAbout(FLOAT_SUBJECT_FACTORY, callback);
return ExpectFailure.expectFailureAbout(FloatSubject::new, callback);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,10 @@ private static void isNotWithinNegativeToleranceThrowsIAE(
}
}

private static final Subject.Factory<IntegerSubject, Integer> INTEGER_SUBJECT_FACTORY =
new Subject.Factory<IntegerSubject, Integer>() {
@Override
public IntegerSubject createSubject(FailureMetadata metadata, Integer that) {
return new IntegerSubject(metadata, that);
}
};

@CanIgnoreReturnValue
private static AssertionError expectFailure(
SimpleSubjectBuilderCallback<IntegerSubject, Integer> callback) {
return ExpectFailure.expectFailureAbout(INTEGER_SUBJECT_FACTORY, callback);
return ExpectFailure.expectFailureAbout(IntegerSubject::new, callback);
}

private IntegerSubject expectFailureWhenTestingThat(Integer actual) {
Expand Down
10 changes: 1 addition & 9 deletions core/src/test/java/com/google/common/truth/LongSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,18 +255,10 @@ private static void isNotWithinNegativeToleranceThrowsIAE(
}
}

private static final Subject.Factory<LongSubject, Long> LONG_SUBJECT_FACTORY =
new Subject.Factory<LongSubject, Long>() {
@Override
public LongSubject createSubject(FailureMetadata metadata, Long that) {
return new LongSubject(metadata, that);
}
};

@CanIgnoreReturnValue
private static AssertionError expectFailure(
SimpleSubjectBuilderCallback<LongSubject, Long> callback) {
return ExpectFailure.expectFailureAbout(LONG_SUBJECT_FACTORY, callback);
return ExpectFailure.expectFailureAbout(LongSubject::new, callback);
}

private LongSubject expectFailureWhenTestingThat(Long actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,9 @@ public void testNumericPrimitiveTypes_isNotEqual_shouldFail_charToInt() {
assertFailureValue("but was; string representation of actual value", "*");
}

private static final Subject.Factory<Subject, Object> DEFAULT_SUBJECT_FACTORY =
new Subject.Factory<Subject, Object>() {
@Override
public Subject createSubject(FailureMetadata metadata, Object that) {
return new Subject(metadata, that);
}
};

private static void expectFailure(
ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> callback) {
AssertionError unused = ExpectFailure.expectFailureAbout(DEFAULT_SUBJECT_FACTORY, callback);
AssertionError unused = ExpectFailure.expectFailureAbout(Subject::new, callback);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static EmployeeSubject assertThat(@Nullable Employee employee) {
}

// Static method for getting the subject factory (for use with assertAbout())
public static Subject.Factory<EmployeeSubject, Employee> employees() {
public static Factory<EmployeeSubject, Employee> employees() {
return EmployeeSubject::new;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static com.google.common.truth.ExpectFailure.assertThat;
import static com.google.common.truth.ExpectFailure.expectFailure;
import static com.google.common.truth.ExpectFailure.expectFailureAbout;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.truth.ExpectFailure.SimpleSubjectBuilderCallback;
import org.junit.Test;
Expand All @@ -43,10 +42,8 @@ public void testExpectFailure() throws Exception {
public void testExpectFailureAbout() {
AssertionError unused =
expectFailureAbout(
STRINGS,
StringSubject::new,
(SimpleSubjectBuilderCallback<StringSubject, String>)
whenTesting -> whenTesting.that("foo").contains("bar"));
}

private static final Subject.Factory<StringSubject, String> STRINGS = StringSubject::new;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class Re2jSubjects {
* @see com.google.common.truth.StringSubject
*/
public static Subject.Factory<Re2jStringSubject, String> re2jString() {
return Re2jStringSubject.FACTORY;
return Re2jStringSubject::new;
}

/**
Expand All @@ -51,15 +51,6 @@ public static Subject.Factory<Re2jStringSubject, String> re2jString() {
* @see #re2jString
*/
public static final class Re2jStringSubject extends Subject {
private static final Subject.Factory<Re2jStringSubject, String> FACTORY =
new Subject.Factory<Re2jStringSubject, String>() {
@Override
public Re2jStringSubject createSubject(
FailureMetadata failureMetadata, @Nullable String target) {
return new Re2jStringSubject(failureMetadata, target);
}
};

private final @Nullable String actual;

private Re2jStringSubject(FailureMetadata failureMetadata, @Nullable String subject) {
Expand Down

0 comments on commit c5e21b9

Please sign in to comment.