Skip to content

Commit

Permalink
Remove type parameters from Subject.
Browse files Browse the repository at this point in the history
Also, return plain Subject instead of DefaultSubject, and make DefaultSubject extend raw Subject. But keep DefaultSubject around (and extensible) until after the next release, since that makes it easier for Kotlin code to migrate.

RELNOTES=Removed the type parameters from `Subject`. If you subclass this type (or declare it as a method return type, etc.), you will have to update those usages at the same time you update Truth. Or you can remove the type parameters from your usages (temporarily introducing rawtypes/unchecked warnings, which you may wish to suppress) and then update Truth (at which point the warnings will go away and you can remove any suppressions).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=252679650
  • Loading branch information
cpovirk committed Jun 11, 2019
1 parent 8252a3d commit 3740ee6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* soon go away, so you may wish to start using raw {@code Subject} now to prepare.
*/
@Deprecated
public class DefaultSubject extends Subject<DefaultSubject, Object> {
public class DefaultSubject extends Subject {
/**
* Constructor for use by subclasses. If you want to create an instance of this class itself, call
* {@link Subject#check}{@code .that(actual)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public final BigDecimalSubject that(@NullableDecl BigDecimal actual) {
return new BigDecimalSubject(metadata(), actual);
}

public final Subject<DefaultSubject, Object> that(@NullableDecl Object actual) {
return new DefaultSubject(metadata(), actual);
public final Subject that(@NullableDecl Object actual) {
return new Subject(metadata(), actual);
}

@GwtIncompatible("ClassSubject.java")
Expand Down
46 changes: 9 additions & 37 deletions core/src/main/java/com/google/common/truth/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import com.google.common.truth.FailureMetadata.OldAndNewValuesAreSimilar;
import com.google.errorprone.annotations.CompatibleWith;
import com.google.errorprone.annotations.ForOverride;
import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -65,23 +64,10 @@
* <p>For information about writing a custom {@link Subject}, see <a
* href="https://google.github.io/truth/extension">our doc on extensions</a>.
*
* @param <DeprecatedSelfTypeUseRawTypesInsteadOfParameterizedSubjectT> <b>deprecated -</b> the
* self-type, allowing {@code this}-returning methods to avoid needing subclassing. <i>Both type
* parameters will be removed, as the methods that need them are being removed. You can prepare
* for this change by editing your class to refer to raw {@code Subject} today.</i>
* @param <DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT> <b>deprecated -</b> the
* type of the object being tested by this {@code Subject}. <i>Both type parameters will be
* removed, as the methods that need them are being removed. You can prepare for this change by
* editing your class to refer to raw {@code Subject} today.</i>
* @author David Saff
* @author Christian Gruber
*/
public class Subject<
DeprecatedSelfTypeUseRawTypesInsteadOfParameterizedSubjectT extends
Subject<
DeprecatedSelfTypeUseRawTypesInsteadOfParameterizedSubjectT,
DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT>,
DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT> {
public class Subject {
/**
* In a fluent assertion chain, the argument to the common overload of {@link
* StandardSubjectBuilder#about(Subject.Factory) about}, the method that specifies what kind of
Expand All @@ -107,17 +93,15 @@ public void fail(AssertionError failure) {}
};

private final FailureMetadata metadata;
private final DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT actual;
private final Object actual;
private String customName = null;
@NullableDecl private final String typeDescriptionOverride;

/**
* Constructor for use by subclasses. If you want to create an instance of this class itself, call
* {@link Subject#check}{@code .that(actual)}.
*/
protected Subject(
FailureMetadata metadata,
@NullableDecl DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT actual) {
protected Subject(FailureMetadata metadata, @NullableDecl Object actual) {
this(metadata, actual, /*typeDescriptionOverride=*/ null);
}

Expand All @@ -134,7 +118,7 @@ protected Subject(
*/
Subject(
FailureMetadata metadata,
@NullableDecl DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT actual,
@NullableDecl Object actual,
@NullableDecl String typeDescriptionOverride) {
this.metadata = metadata.updateForSubject(this);
this.actual = actual;
Expand Down Expand Up @@ -267,9 +251,7 @@ private static long integralValue(Object o) {
}

/** Fails if the subject is not the same instance as the given object. */
public final void isSameInstanceAs(
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object expected) {
public final void isSameInstanceAs(@NullableDecl Object expected) {
if (actual != expected) {
failEqualityCheck(
SAME_INSTANCE,
Expand All @@ -285,9 +267,7 @@ public final void isSameInstanceAs(
}

/** Fails if the subject is the same instance as the given object. */
public final void isNotSameInstanceAs(
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object unexpected) {
public final void isNotSameInstanceAs(@NullableDecl Object unexpected) {
if (actual == unexpected) {
/*
* We use actualCustomStringRepresentation() because it might be overridden to be better than
Expand Down Expand Up @@ -354,11 +334,7 @@ public void isIn(Iterable<?> iterable) {

/** Fails unless the subject is equal to any of the given elements. */
public void isAnyOf(
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object first,
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object second,
@NullableDecl Object... rest) {
@NullableDecl Object first, @NullableDecl Object second, @NullableDecl Object... rest) {
isIn(accumulate(first, second, rest));
}

Expand All @@ -371,16 +347,12 @@ public void isNotIn(Iterable<?> iterable) {

/** Fails if the subject is equal to any of the given elements. */
public void isNoneOf(
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object first,
@NullableDecl @CompatibleWith("DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT")
Object second,
@NullableDecl Object... rest) {
@NullableDecl Object first, @NullableDecl Object second, @NullableDecl Object... rest) {
isNotIn(accumulate(first, second, rest));
}

/** Returns the actual value under test. */
final DeprecatedActualTypeUseRawTypesInsteadOfParameterizedSubjectT actual() {
final Object actual() {
return actual;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/google/common/truth/Truth.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static BigDecimalSubject assertThat(@NullableDecl BigDecimal actual) {
return assert_().that(actual);
}

public static Subject<DefaultSubject, Object> assertThat(@NullableDecl Object actual) {
public static Subject assertThat(@NullableDecl Object actual) {
return assert_().that(actual);
}

Expand Down
22 changes: 11 additions & 11 deletions core/src/test/java/com/google/common/truth/IntegerSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ public void testNumericPrimitiveTypes_isNotEqual_shouldFail_charToInt() {
assertFailureValue("but was; string representation of actual value", "*");
}

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

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

Expand All @@ -236,17 +236,17 @@ public void testNumericPrimitiveTypes() {
ImmutableSet<Object> fortyTwosNoChar = ImmutableSet.<Object>of(byte42, short42, int42, long42);
for (final Object actual : fortyTwosNoChar) {
for (final Object expected : fortyTwosNoChar) {
ExpectFailure.SimpleSubjectBuilderCallback<DefaultSubject, Object> actualFirst =
new ExpectFailure.SimpleSubjectBuilderCallback<DefaultSubject, Object>() {
ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> actualFirst =
new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DefaultSubject, Object> expect) {
public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) {
expect.that(actual).isNotEqualTo(expected);
}
};
ExpectFailure.SimpleSubjectBuilderCallback<DefaultSubject, Object> expectedFirst =
new ExpectFailure.SimpleSubjectBuilderCallback<DefaultSubject, Object>() {
ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> expectedFirst =
new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() {
@Override
public void invokeAssertion(SimpleSubjectBuilder<DefaultSubject, Object> expect) {
public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) {
expect.that(expected).isNotEqualTo(actual);
}
};
Expand Down
2 changes: 0 additions & 2 deletions core/src/test/java/com/google/common/truth/gwt/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.common.truth.BooleanSubject;
import com.google.common.truth.ClassSubject;
import com.google.common.truth.ComparableSubject;
import com.google.common.truth.DefaultSubject;
import com.google.common.truth.DoubleSubject;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.FloatSubject;
Expand Down Expand Up @@ -55,7 +54,6 @@ public class Inventory {
BooleanSubject booleanSubject;
ClassSubject classSubject;
ComparableSubject comparableSubject;
DefaultSubject defaultSubject;
DoubleSubject doubleSubject;
FailureStrategy failureStrategy;
FloatSubject floatSubject;
Expand Down

0 comments on commit 3740ee6

Please sign in to comment.