diff options
Diffstat (limited to 'core/src/test/java/com/google/common/truth/SubjectTest.java')
-rw-r--r-- | core/src/test/java/com/google/common/truth/SubjectTest.java | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/core/src/test/java/com/google/common/truth/SubjectTest.java b/core/src/test/java/com/google/common/truth/SubjectTest.java index 32919ce4..9544f52f 100644 --- a/core/src/test/java/com/google/common/truth/SubjectTest.java +++ b/core/src/test/java/com/google/common/truth/SubjectTest.java @@ -18,9 +18,11 @@ package com.google.common.truth; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.truth.ExpectFailure.assertThat; import static com.google.common.truth.Fact.simpleFact; +import static com.google.common.truth.SubjectTest.ForbidsEqualityChecksSubject.objectsForbiddingEqualityCheck; import static com.google.common.truth.TestPlatform.isGwt; import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.TruthJUnit.assume; import static org.junit.Assert.fail; import com.google.common.annotations.GwtIncompatible; @@ -35,7 +37,6 @@ import com.google.common.collect.ImmutableTable; import com.google.common.collect.Iterators; import com.google.common.primitives.UnsignedInteger; import com.google.common.testing.NullPointerTester; -import com.google.common.truth.Subject.Factory; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; @@ -58,7 +59,16 @@ public class SubjectTest extends BaseSubjectTestCase { @Test @GwtIncompatible("NullPointerTester") @SuppressWarnings("GoogleInternalApi") + /* + * TODO(cpovirk): Reenable these tests publicly. Currently, we depend on guava-android, whose + * NullPointerTester doesn't yet recognize type-use @Nullable annotations. And we can't mix the + * -jre version of guava-testlib with the -android version of guava because the NullPointerTester + * feature we need requires a -jre-only API. + */ + @org.junit.Ignore public void nullPointerTester() { + assume().that(isAndroid()).isFalse(); // type-annotation @Nullable is not available + NullPointerTester npTester = new NullPointerTester(); npTester.setDefault(Fact.class, simpleFact("fact")); @@ -88,7 +98,10 @@ public class SubjectTest extends BaseSubjectTestCase { @Test @GwtIncompatible("NullPointerTester") + @org.junit.Ignore // TODO(cpovirk): Reenable publicly. (See nullPointerTester().) public void allAssertThatOverloadsAcceptNull() throws Exception { + assume().that(isAndroid()).isFalse(); // type-annotation @Nullable is not available + NullPointerTester npTester = new NullPointerTester(); npTester.setDefault(Fact.class, simpleFact("fact")); for (Method method : Truth.class.getDeclaredMethods()) { @@ -447,6 +460,10 @@ public class SubjectTest extends BaseSubjectTestCase { assertThat(a).isNotEqualTo(b); } + @SuppressWarnings({ + "BoxedPrimitiveConstructor", + "deprecation" + }) // intentional check on non-identity objects @Test public void isNotEqualToFailureWithObjects() { Object o = new Integer(1); @@ -481,16 +498,19 @@ public class SubjectTest extends BaseSubjectTestCase { expectFailure.whenTesting().that(o).isNotEqualTo(o); } + @SuppressWarnings("IsInstanceString") // test is an intentional trivially true check @Test public void isInstanceOfExactType() { assertThat("a").isInstanceOf(String.class); } + @SuppressWarnings("IsInstanceInteger") // test is an intentional trivially true check @Test public void isInstanceOfSuperclass() { assertThat(3).isInstanceOf(Number.class); } + @SuppressWarnings("IsInstanceString") // test is an intentional trivially true check @Test public void isInstanceOfImplementedInterface() { if (isGwt()) { @@ -540,6 +560,17 @@ public class SubjectTest extends BaseSubjectTestCase { expectFailure.whenTesting().that((Object) null).isInstanceOf(CharSequence.class); } + // false positive; actually an intentional trivially *false* check + @SuppressWarnings("IsInstanceInteger") + @Test + public void isInstanceOfPrimitiveType() { + try { + assertThat(1).isInstanceOf(int.class); + fail(); + } catch (IllegalArgumentException expected) { + } + } + @Test public void isNotInstanceOfUnrelatedClass() { assertThat("a").isNotInstanceOf(Long.class); @@ -586,6 +617,15 @@ public class SubjectTest extends BaseSubjectTestCase { } @Test + public void isNotInstanceOfPrimitiveType() { + try { + assertThat(1).isNotInstanceOf(int.class); + fail(); + } catch (IllegalArgumentException expected) { + } + } + + @Test public void isIn() { assertThat("b").isIn(oneShotIterable("a", "b", "c")); } @@ -707,7 +747,8 @@ public class SubjectTest extends BaseSubjectTestCase { } @Test - @SuppressWarnings({"EqualsIncompatibleType", "DoNotCall"}) + // test of a mistaken call + @SuppressWarnings({"EqualsIncompatibleType", "DoNotCall", "deprecation"}) public void equalsThrowsUSOE() { try { boolean unused = assertThat(5).equals(5); @@ -724,7 +765,8 @@ public class SubjectTest extends BaseSubjectTestCase { } @Test - @SuppressWarnings("DoNotCall") + // test of a mistaken call + @SuppressWarnings({"DoNotCall", "deprecation"}) public void hashCodeThrowsUSOE() { try { int unused = assertThat(5).hashCode(); @@ -740,8 +782,8 @@ public class SubjectTest extends BaseSubjectTestCase { assertThat((Object) null).ignoreCheck().that("foo").isNull(); } - private static <T> Iterable<T> oneShotIterable(final T... values) { - final Iterator<T> iterator = Iterators.forArray(values); + private static <T> Iterable<T> oneShotIterable(T... values) { + Iterator<T> iterator = Iterators.forArray(values); return new Iterable<T>() { @Override public Iterator<T> iterator() { @@ -756,6 +798,7 @@ public class SubjectTest extends BaseSubjectTestCase { } @Test + @SuppressWarnings("TruthIncompatibleType") // test of a mistaken call public void disambiguationWithSameToString() { expectFailure.whenTesting().that(new StringBuilder("foo")).isEqualTo(new StringBuilder("foo")); assertFailureKeys("expected", "but was"); @@ -784,7 +827,11 @@ public class SubjectTest extends BaseSubjectTestCase { } } - private static final class ForbidsEqualityChecksSubject extends Subject { + static final class ForbidsEqualityChecksSubject extends Subject { + static Factory<ForbidsEqualityChecksSubject, Object> objectsForbiddingEqualityCheck() { + return ForbidsEqualityChecksSubject::new; + } + ForbidsEqualityChecksSubject(FailureMetadata metadata, @Nullable Object actual) { super(metadata, actual); } @@ -802,13 +849,7 @@ public class SubjectTest extends BaseSubjectTestCase { } } - private static Subject.Factory<ForbidsEqualityChecksSubject, Object> - objectsForbiddingEqualityCheck() { - return new Factory<ForbidsEqualityChecksSubject, Object>() { - @Override - public ForbidsEqualityChecksSubject createSubject(FailureMetadata metadata, Object actual) { - return new ForbidsEqualityChecksSubject(metadata, actual); - } - }; + private static boolean isAndroid() { + return System.getProperty("java.runtime.name").contains("Android"); } } |