/** Fails unless the subject is equal to any element in the given iterable. */ public void isIn(Iterable<?> iterable) { if (!Iterables.contains(iterable, actual())) { failWithActual("expected any of", iterable); } }
/** Fails if the subject is not an instance of the given class. */ public void isInstanceOf(Class<?> clazz) { if (clazz == null) { throw new NullPointerException("clazz"); } if (actual() == null) { failWithActual("expected instance of", clazz.getName()); return; } if (!Platform.isInstanceOfType(actual(), clazz)) { if (classMetadataUnsupported()) { throw new UnsupportedOperationException( actualCustomStringRepresentation() + ", an instance of " + actual().getClass().getName() + ", may or may not be an instance of " + clazz.getName() + ". Under -XdisableClassMetadata, we do not have enough information to tell."); } failWithoutActual( fact("expected instance of", clazz.getName()), fact("but was instance of", actual().getClass().getName()), fact("with value", actualCustomStringRepresentation())); } }
if (step.subject.actual() instanceof Throwable) {
/** Fails if the subject is the same instance as the given object. */ public void isNotSameAs(@NullableDecl @CompatibleWith("T") Object unexpected) { if (actual() == unexpected) { /* * We use actualCustomStringRepresentation() because it might be overridden to be better than * actual.toString()/unexpected.toString(). */ failWithoutActual( fact("expected not to be specific instance", actualCustomStringRepresentation())); } }
/** Fails if the subject is equal to any element in the given iterable. */ public void isNotIn(Iterable<?> iterable) { if (Iterables.contains(iterable, actual())) { failWithActual("expected not to be any of", iterable); } }
String actualString = actualCustomStringRepresentation(); String expectedString = formatActualOrExpected(expected); String actualClass = actual() == null ? "(null reference)" : actual().getClass().getName(); String expectedClass = expected == null ? "(null reference)" : expected.getClass().getName(); if (equalityCheck == EqualityCheck.EQUAL && actual() != null && expected != null) { metadata.failEqualityCheck( nameAsFacts(), difference.factsOrEmpty(), expectedString, actualString);
/** * Returns the first {@link Throwable} in the chain of actual values. Typically, we'll have a root * cause only if the assertion chain contains a {@link ThrowableSubject}. */ @NullableDecl private Throwable rootCause() { for (Step step : steps) { if (!step.isCheckCall() && step.subject.actual() instanceof Throwable) { return (Throwable) step.subject.actual(); } } return null; } }
/** Fails if the subject is an instance of the given class. */ public void isNotInstanceOf(Class<?> clazz) { if (clazz == null) { throw new NullPointerException("clazz"); } if (classMetadataUnsupported()) { throw new UnsupportedOperationException( "isNotInstanceOf is not supported under -XdisableClassMetadata"); } if (actual() == null) { return; // null is not an instance of clazz. } if (Platform.isInstanceOfType(actual(), clazz)) { failWithActual("expected not to be an instance of", clazz.getName()); /* * TODO(cpovirk): Consider including actual().getClass() if it's not clazz itself but only a * subtype. */ } }
if (actual() == null && expected == null) { return ComparisonResult.equal(); } else if (actual() == null || expected == null) { return ComparisonResult.differentNoDescription(); } else if (actual() instanceof byte[] && expected instanceof byte[]) { return checkByteArrayEquals((byte[]) expected, (byte[]) actual()); } else if (actual().getClass().isArray() && expected.getClass().isArray()) { return checkArrayEqualsRecursive(expected, actual, ""); } else if (isIntegralBoxedPrimitive(actual()) && isIntegralBoxedPrimitive(expected)) { return ComparisonResult.fromEqualsResult(integralValue(actual()) == integralValue(expected)); } else if (actual() instanceof Double && expected instanceof Double) { return ComparisonResult.fromEqualsResult( Double.compare((Double) actual(), (Double) expected) == 0); } else if (actual() instanceof Float && expected instanceof Float) { return ComparisonResult.fromEqualsResult( Float.compare((Float) actual(), (Float) expected) == 0); } else { return ComparisonResult.fromEqualsResult(actual() == expected || actual().equals(expected));
/** * Supplies the direct string representation of the actual value to other methods which may prefix * or otherwise position it in an error message. This should only be overridden to provide an * improved string representation of the value under test, as it would appear in any given error * message, and should not be used for additional prefixing. * * <p>Subjects should override this with care. * * <p>By default, this returns {@code String.ValueOf(getActualValue())}. */ /* * TODO(cpovirk): Consider whether this API pulls its weight. If users want to format the actual * value, maybe they should do so themselves? Of course, they won't have a chance to use a custom * format for inherited implementations like isEqualTo(). But if they want to format the actual * value specially, then it seems likely that they'll want to format the expected value specially, * too. And that applies just as well to APIs like isIn(). Maybe we'll want an API that supports * formatting those values, too (like formatActualOrExpected below)? See also the related * b/70930431. But note that we are likely to use this from FailureMetadata, at least in the short * term, for better or for worse. */ @ForOverride protected String actualCustomStringRepresentation() { return formatActualOrExpected(actual()); }
/** Fails if the subject is not the same instance as the given object. */ public void isSameAs(@NullableDecl @CompatibleWith("T") Object expected) { if (actual() != expected) { failEqualityCheck( SAME_INSTANCE, expected, /* * Pass through *whether* the values are equal so that failEqualityCheck() can print that * information. But remove the description of the difference, which is always about * content, since people calling isSameAs() are explicitly not interested in content, only * object identity. */ compareForEquality(expected).withoutDescription()); } }