diff --git a/core/src/main/java/com/google/common/truth/IterableSubject.java b/core/src/main/java/com/google/common/truth/IterableSubject.java index d4a470b65..1a9b25468 100644 --- a/core/src/main/java/com/google/common/truth/IterableSubject.java +++ b/core/src/main/java/com/google/common/truth/IterableSubject.java @@ -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), @@ -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> duplicates = newArrayList(); + List> duplicates = new ArrayList<>(); for (Multiset.Entry entry : LinkedHashMultiset.create(checkNotNull(actual)).entrySet()) { if (entry.getCount() > 1) { duplicates.add(entry); @@ -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... @@ -312,21 +314,17 @@ public final Ordered containsAtLeastElementsIn(@Nullable Iterable expectedIte return ordered ? IN_ORDER - : new Ordered() { - @Override - public void inOrder() { - ImmutableList.Builder 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 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 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 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()); } }; } @@ -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); } @@ -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. @@ -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); } @@ -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)); } /** @@ -1288,17 +1281,13 @@ public Ordered containsExactlyElementsIn(@Nullable Iterable 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.builder() .add(simpleFact("contents match, but order was wrong")) .add(fact("expected", expected)) .addAll(correspondence.describeForIterable()) .build()); - } - }; } /** @@ -1488,7 +1477,7 @@ private ImmutableList formatExtras( // index must be in there once. return asList(); } - List notIndexed = newArrayList(); + List notIndexed = new ArrayList<>(); for (int index = 0; index < list.size(); index++) { if (!indexes.contains(index)) { notIndexed.add(list.get(index)); @@ -1628,17 +1617,13 @@ public Ordered containsAtLeastElementsIn(Iterable 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.builder() .add(simpleFact("required elements were all found, but order was wrong")) .add(fact("expected order for required elements", expected)) .addAll(correspondence.describeForIterable()) .build()); - } - }; } /** @@ -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 unpairedExpectedValues = newArrayList(); + private final List 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 unpairedActualValues = newArrayList(); + private final List unpairedActualValues = new ArrayList<>(); } } } diff --git a/core/src/test/java/com/google/common/truth/ChainingTest.java b/core/src/test/java/com/google/common/truth/ChainingTest.java index 318c68773..8631adf0b 100644 --- a/core/src/test/java/com/google/common/truth/ChainingTest.java +++ b/core/src/test/java/com/google/common/truth/ChainingTest.java @@ -225,14 +225,6 @@ public void badFormat() { */ private static final class MyObjectSubject extends Subject { - static final Factory FACTORY = - new Factory() { - @Override - public MyObjectSubject createSubject(FailureMetadata metadata, Object actual) { - return new MyObjectSubject(metadata, actual); - } - }; - private MyObjectSubject(FailureMetadata metadata, Object actual) { super(metadata, actual); } @@ -272,7 +264,7 @@ MyObjectSubject delegatingToNamedNoNeedToDisplayBoth(Object actual, String name) } private static Subject.Factory myObjects() { - return MyObjectSubject.FACTORY; + return MyObjectSubject::new; } private MyObjectSubject expectFailureWhenTestingThat(Object actual) { diff --git a/core/src/test/java/com/google/common/truth/DoubleSubjectTest.java b/core/src/test/java/com/google/common/truth/DoubleSubjectTest.java index b1b2f21e2..14bbb9f0a 100644 --- a/core/src/test/java/com/google/common/truth/DoubleSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/DoubleSubjectTest.java @@ -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 DOUBLE_SUBJECT_FACTORY = - new Subject.Factory() { - @Override - public DoubleSubject createSubject(FailureMetadata metadata, Double that) { - return new DoubleSubject(metadata, that); - } - }; - @CanIgnoreReturnValue private static AssertionError expectFailure( SimpleSubjectBuilderCallback callback) { - return ExpectFailure.expectFailureAbout(DOUBLE_SUBJECT_FACTORY, callback); + return ExpectFailure.expectFailureAbout(DoubleSubject::new, callback); } @Test diff --git a/core/src/test/java/com/google/common/truth/ExpectFailureTest.java b/core/src/test/java/com/google/common/truth/ExpectFailureTest.java index 06e1c4ef1..54e8113d8 100644 --- a/core/src/test/java/com/google/common/truth/ExpectFailureTest.java +++ b/core/src/test/java/com/google/common/truth/ExpectFailureTest.java @@ -137,12 +137,7 @@ public void expectFail_whenTestingWithoutInContext_shouldFail() { } private static Subject.Factory strings() { - return new Subject.Factory() { - @Override - public StringSubject createSubject(FailureMetadata fm, String that) { - return new StringSubject(fm, that); - } - }; + return StringSubject::new; } private static class BadSubject extends Subject { @@ -163,13 +158,8 @@ public void isEqualTo(Object expected) { } } - private static Subject.Factory badSubject() { - return new Subject.Factory() { - @Override - public BadSubject createSubject(FailureMetadata fm, Integer that) { - return new BadSubject(fm, that); - } - }; + private static Factory badSubject() { + return BadSubject::new; } } } diff --git a/core/src/test/java/com/google/common/truth/FloatSubjectTest.java b/core/src/test/java/com/google/common/truth/FloatSubjectTest.java index 50ded2ba0..54ee01dd5 100644 --- a/core/src/test/java/com/google/common/truth/FloatSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/FloatSubjectTest.java @@ -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 FLOAT_SUBJECT_FACTORY = - new Subject.Factory() { - @Override - public FloatSubject createSubject(FailureMetadata metadata, Float that) { - return new FloatSubject(metadata, that); - } - }; - @CanIgnoreReturnValue private static AssertionError expectFailure( SimpleSubjectBuilderCallback callback) { - return ExpectFailure.expectFailureAbout(FLOAT_SUBJECT_FACTORY, callback); + return ExpectFailure.expectFailureAbout(FloatSubject::new, callback); } @Test diff --git a/core/src/test/java/com/google/common/truth/IntegerSubjectTest.java b/core/src/test/java/com/google/common/truth/IntegerSubjectTest.java index f97f0b563..8f4bae98f 100644 --- a/core/src/test/java/com/google/common/truth/IntegerSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/IntegerSubjectTest.java @@ -227,18 +227,10 @@ private static void isNotWithinNegativeToleranceThrowsIAE( } } - private static final Subject.Factory INTEGER_SUBJECT_FACTORY = - new Subject.Factory() { - @Override - public IntegerSubject createSubject(FailureMetadata metadata, Integer that) { - return new IntegerSubject(metadata, that); - } - }; - @CanIgnoreReturnValue private static AssertionError expectFailure( SimpleSubjectBuilderCallback callback) { - return ExpectFailure.expectFailureAbout(INTEGER_SUBJECT_FACTORY, callback); + return ExpectFailure.expectFailureAbout(IntegerSubject::new, callback); } private IntegerSubject expectFailureWhenTestingThat(Integer actual) { diff --git a/core/src/test/java/com/google/common/truth/LongSubjectTest.java b/core/src/test/java/com/google/common/truth/LongSubjectTest.java index 638737922..7392ecf4a 100644 --- a/core/src/test/java/com/google/common/truth/LongSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/LongSubjectTest.java @@ -255,18 +255,10 @@ private static void isNotWithinNegativeToleranceThrowsIAE( } } - private static final Subject.Factory LONG_SUBJECT_FACTORY = - new Subject.Factory() { - @Override - public LongSubject createSubject(FailureMetadata metadata, Long that) { - return new LongSubject(metadata, that); - } - }; - @CanIgnoreReturnValue private static AssertionError expectFailure( SimpleSubjectBuilderCallback callback) { - return ExpectFailure.expectFailureAbout(LONG_SUBJECT_FACTORY, callback); + return ExpectFailure.expectFailureAbout(LongSubject::new, callback); } private LongSubject expectFailureWhenTestingThat(Long actual) { diff --git a/core/src/test/java/com/google/common/truth/NumericComparisonTest.java b/core/src/test/java/com/google/common/truth/NumericComparisonTest.java index a2bc9b362..2c0f23e38 100644 --- a/core/src/test/java/com/google/common/truth/NumericComparisonTest.java +++ b/core/src/test/java/com/google/common/truth/NumericComparisonTest.java @@ -121,17 +121,9 @@ public void testNumericPrimitiveTypes_isNotEqual_shouldFail_charToInt() { assertFailureValue("but was; string representation of actual value", "*"); } - private static final Subject.Factory DEFAULT_SUBJECT_FACTORY = - new Subject.Factory() { - @Override - public Subject createSubject(FailureMetadata metadata, Object that) { - return new Subject(metadata, that); - } - }; - private static void expectFailure( ExpectFailure.SimpleSubjectBuilderCallback callback) { - AssertionError unused = ExpectFailure.expectFailureAbout(DEFAULT_SUBJECT_FACTORY, callback); + AssertionError unused = ExpectFailure.expectFailureAbout(Subject::new, callback); } @Test diff --git a/core/src/test/java/com/google/common/truth/extension/EmployeeSubject.java b/core/src/test/java/com/google/common/truth/extension/EmployeeSubject.java index 72c6a1778..eebf571e7 100644 --- a/core/src/test/java/com/google/common/truth/extension/EmployeeSubject.java +++ b/core/src/test/java/com/google/common/truth/extension/EmployeeSubject.java @@ -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 employees() { + public static Factory employees() { return EmployeeSubject::new; } diff --git a/extensions/java8/src/test/java/com/google/common/truth/ExpectFailure8Test.java b/extensions/java8/src/test/java/com/google/common/truth/ExpectFailure8Test.java index 197bfcf13..da13a1a13 100644 --- a/extensions/java8/src/test/java/com/google/common/truth/ExpectFailure8Test.java +++ b/extensions/java8/src/test/java/com/google/common/truth/ExpectFailure8Test.java @@ -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; @@ -43,10 +42,8 @@ public void testExpectFailure() throws Exception { public void testExpectFailureAbout() { AssertionError unused = expectFailureAbout( - STRINGS, + StringSubject::new, (SimpleSubjectBuilderCallback) whenTesting -> whenTesting.that("foo").contains("bar")); } - - private static final Subject.Factory STRINGS = StringSubject::new; } diff --git a/extensions/re2j/src/main/java/com/google/common/truth/extensions/re2j/Re2jSubjects.java b/extensions/re2j/src/main/java/com/google/common/truth/extensions/re2j/Re2jSubjects.java index ecaa6a61d..ba85ae122 100644 --- a/extensions/re2j/src/main/java/com/google/common/truth/extensions/re2j/Re2jSubjects.java +++ b/extensions/re2j/src/main/java/com/google/common/truth/extensions/re2j/Re2jSubjects.java @@ -41,7 +41,7 @@ public final class Re2jSubjects { * @see com.google.common.truth.StringSubject */ public static Subject.Factory re2jString() { - return Re2jStringSubject.FACTORY; + return Re2jStringSubject::new; } /** @@ -51,15 +51,6 @@ public static Subject.Factory re2jString() { * @see #re2jString */ public static final class Re2jStringSubject extends Subject { - private static final Subject.Factory FACTORY = - new Subject.Factory() { - @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) {