final String actualCustomStringRepresentationForPackageMembersToCall() { return actualCustomStringRepresentation(); }
/** Like {@link #actualAsString()} but without angle brackets around the value. */ final String actualAsStringNoBrackets() { String formatted = actualCustomStringRepresentation(); if (customName != null) { return customName + (formatted.isEmpty() ? "" : " (" + formatted + ")"); } else { return formatted; } }
@Override public String toString() { return getClass().getName() + "(" + actualCustomStringRepresentation() + ")"; }
/** * Returns a string representation of the actual value. This will either be the toString() of the * value or a prefixed "name" along with the string representation. */ /* * TODO(cpovirk): If we delete named(), this method will be a thin wrapper around * actualCustomStringRepresentation(), one that merely adds angle brackets (which we might not * want it to do). But if we delete actualCustomStringRepresentation() in favor of a "format * actual or expected" method, as described in a comment on that method, then it becomes useful * again (though there's still a question of what to do with the angle brackets). */ protected final String actualAsString() { String formatted = actualCustomStringRepresentation(); if (customName != null) { // Covers some rare cases where a type might return "" from their custom formatter. // This is actually pretty terrible, as it comes from subjects overriding (formerly) // getDisplaySubject() in cases of .named() to make it not prefixing but replacing. // That goes against the stated contract of .named(). Once displayedAs() is in place, // we can rip this out and callers can use that instead. // TODO(cgruber) return customName + (formatted.isEmpty() ? "" : " (<" + formatted + ">)"); } else { return "<" + formatted + ">"; } }
/** * Returns a "but was: <actual value>" string. This method should be rarely needed, since Truth * inserts a "but was" fact by default for assertions. However, it's occasionally useful for calls * to {@code failWithoutActual} that want a "but was" fact but don't want it to come last, where * Truth inserts it by default. */ /* * TODO(cpovirk): Consider giving this protected access. * * It is likely better than what users would otherwise do -- `fact("but was", actual())`, which * ignores actualCustomStringRepresentation() (which is inaccessible outside the package). * * But I want to think more about this. In particular, if people use this to reimplement * isEqualTo(), I would be sad that they're missing out on its normal special handling. That's * probably not enough reason to avoid adding this, but we can hold it back for now. */ final Fact butWas() { return fact("but was", actualCustomStringRepresentation()); }
@Override protected String actualCustomStringRepresentation() { if (actual() != null) { // Check the value of iterable.toString() against the default Object.toString() implementation // so we can avoid things like "com.google.common.graph.Traverser$GraphTraverser$1@5e316c74" String objectToString = actual().getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(actual())); if (actual().toString().equals(objectToString)) { return Iterables.toString(actual()); } } return super.actualCustomStringRepresentation(); }
/** 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())); } }
private void standardIsNotEqualTo(@NullableDecl Object unexpected) { ComparisonResult difference = compareForEquality(unexpected); if (difference.valuesAreEqual()) { String unexpectedAsString = formatActualOrExpected(unexpected); if (actualCustomStringRepresentation().equals(unexpectedAsString)) { failWithoutActual(fact("expected not to be", unexpectedAsString)); } else { failWithoutActual( fact("expected not to be", unexpectedAsString), fact( "but was; string representation of actual value", actualCustomStringRepresentation())); } } }
/** 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())); } }
private final void failEqualityCheck( EqualityCheck equalityCheck, Object expected, ComparisonResult difference) { String actualString = actualCustomStringRepresentation(); String expectedString = formatActualOrExpected(expected); String actualClass = actual() == null ? "(null reference)" : actual().getClass().getName();