code stringlengths 23 201k | docstring stringlengths 17 96.2k | func_name stringlengths 0 235 | language stringclasses 1
value | repo stringlengths 8 72 | path stringlengths 11 317 | url stringlengths 57 377 | license stringclasses 7
values |
|---|---|---|---|---|---|---|---|
protected final StandardSubjectBuilder ignoreCheck() {
return StandardSubjectBuilder.forCustomFailureStrategy(failure -> {});
} | Begins a new call chain that ignores any failures. This is useful for subjects that normally
delegate with to other subjects by using {@link #check} but have already reported a failure. In
such cases it may still be necessary to return a {@code Subject} instance even though any
subsequent assertions are meaningless. For example, if a user chains together more {@link
ThrowableSubject#hasCauseThat} calls than the actual exception has causes, {@code hasCauseThat}
returns {@code ignoreCheck().that(... a dummy exception ...)}. | ignoreCheck | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
protected final void failWithActual(String key, @Nullable Object value) {
failWithActual(fact(key, value));
} | Fails, reporting a message with two "{@linkplain Fact facts}":
<ul>
<li><i>key</i>: <i>value</i>
<li>but was: <i>actual value</i>.
</ul>
<p>This is the simplest failure API. For more advanced needs, see {@linkplain
#failWithActual(Fact, Fact...) the other overload} and {@link #failWithoutActual(Fact, Fact...)
failWithoutActual}.
<p>Example usage: The check {@code contains(String)} calls {@code failWithActual("expected to
contain", string)}.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failWithActual | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
protected final void failWithActual(Fact first, Fact... rest) {
metadata.fail(sandwich(first, rest, butWas()));
} | Fails, reporting a message with the given facts, followed by an automatically added fact of the
form:
<ul>
<li>but was: <i>actual value</i>.
</ul>
<p>If you have only one fact to report (and it's a {@linkplain Fact#fact key-value fact}),
prefer {@linkplain #failWithActual(String, Object) the simpler overload}.
<p>Example usage: The check {@code isEmpty()} calls {@code failWithActual(simpleFact("expected
to be empty"))}.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failWithActual | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final void failWithActual(Iterable<Fact> facts) {
metadata.fail(append(ImmutableList.copyOf(facts), butWas()));
} | Internal variant of {@link #failWithActual(Fact, Fact...)} that accepts an {@link Iterable}.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failWithActual | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void fail(String check) {
fail(check, new Object[0]);
} | Legacy failure method. Prefer the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param check the check being asserted
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithActual(Fact, Fact...) failWithActual}{@code (}{@link Fact#simpleFact
simpleFact(...)}{@code )}. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well). | fail | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void fail(String verb, Object other) {
fail(verb, new Object[] {other});
} | Legacy failure method. Prefer the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param verb the check being asserted
@param other the value against which the value under test is compared
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithActual(String, Object)}. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method (and then inline the resulting
method call, as well). | fail | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void fail(String verb, @Nullable Object... messageParts) {
StringBuilder message = new StringBuilder("Not true that <");
message.append(actualCustomStringRepresentation()).append("> ").append(verb);
for (Object part : messageParts) {
message.append(" <").append(part).append(">");
}
failWithoutActual(simpleFact(message.toString()));
} | Legacy failure method. Prefer the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param verb the check being asserted
@param messageParts the expectations against which the value under test is compared
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithActual(Fact, Fact...)}. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. | fail | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final void failEqualityCheckForEqualsWithoutDescription(@Nullable Object expected) {
failEqualityCheck(EqualityCheck.EQUAL, expected, ComparisonResult.differentNoDescription());
} | Special version of {@link #failEqualityCheck} for use from {@link IterableSubject}, documented
further there.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failEqualityCheckForEqualsWithoutDescription | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private void failEqualityCheck(
EqualityCheck equalityCheck, @Nullable Object expected, ComparisonResult difference) {
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();
/*
* It's a little odd for expectedString to be formatActualOrExpected(expected) but actualString
* *not* to be formatActualOrExpected(actual), since we're going to compare the two. Instead,
* actualString is actualCustomStringRepresentation() -- as it is for other assertions, since
* users may have overridden that method. While actualCustomStringRepresentation() defaults to
* formatActualOrExpected(actual), it's only a default.
*
* What we really want here is probably to delete actualCustomStringRepresentation() and migrate
* users to formatActualOrExpected(actual).
*/
boolean sameToStrings = actualString.equals(expectedString);
boolean sameClassNames = actualClass.equals(expectedClass);
// TODO(cpovirk): Handle "same class name, different class loader."
// `equal` is always false for isEqualTo, but it varies for isSameInstanceAs:
boolean equal = difference.valuesAreEqual();
if (equalityCheck == EqualityCheck.EQUAL
&& (tryFailForTrailingWhitespaceOnly(expected) || tryFailForEmptyString(expected))) {
// tryFailForTrailingWhitespaceOnly or tryFailForEmptyString reported a failure, so we're done
return;
}
if (sameToStrings) {
if (sameClassNames) {
String doppelgangerDescription =
equal
? "(different but equal instance of same class with same string representation)"
: "(non-equal instance of same class with same string representation)";
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("but was", doppelgangerDescription));
} else {
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("an instance of", expectedClass),
fact("but was", "(non-equal value with same string representation)"),
fact("an instance of", actualClass));
}
} else {
if (equalityCheck == EqualityCheck.EQUAL && actual != null && expected != null) {
metadata.failEqualityCheck(difference.factsOrEmpty(), expectedString, actualString);
} else {
failEqualityCheckNoComparisonFailure(
difference,
fact(equalityCheck.keyForExpected, expectedString),
fact("but was", actualString));
}
}
} | Fails, potentially producing a {@code ComparisonFailure}.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failEqualityCheck | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private boolean tryFailForTrailingWhitespaceOnly(@Nullable Object expected) {
if (!(actual instanceof String) || !(expected instanceof String)) {
return false;
}
/*
* TODO(cpovirk): Consider applying this for non-String types. The danger there is that we don't
* know whether toString() (or actualCustomStringRepresentation/formatActualOrExpected) and
* equals() are consistent for those types.
*/
String actualString = (String) actual;
String expectedString = (String) expected;
String actualNoTrailing = whitespace().trimTrailingFrom(actualString);
String expectedNoTrailing = whitespace().trimTrailingFrom(expectedString);
String expectedTrailing =
escapeWhitespace(expectedString.substring(expectedNoTrailing.length()));
String actualTrailing = escapeWhitespace(actualString.substring(actualNoTrailing.length()));
if (!actualNoTrailing.equals(expectedNoTrailing)) {
return false;
}
if (actualString.startsWith(expectedString)) {
failWithoutActual(
fact("expected", expectedString),
fact("but contained extra trailing whitespace", actualTrailing));
} else if (expectedString.startsWith(actualString)) {
failWithoutActual(
fact("expected", expectedString),
fact("but was missing trailing whitespace", expectedTrailing));
} else {
failWithoutActual(
fact("expected", expectedString),
fact("with trailing whitespace", expectedTrailing),
fact("but trailing whitespace was", actualTrailing));
}
return true;
} | Checks whether the actual and expected values are strings that match except for trailing
whitespace. If so, reports a failure and returns true. | tryFailForTrailingWhitespaceOnly | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private static String escapeWhitespace(String in) {
StringBuilder out = new StringBuilder();
for (char c : in.toCharArray()) {
out.append(escapeWhitespace(c));
}
return out.toString();
} | Checks whether the actual and expected values are strings that match except for trailing
whitespace. If so, reports a failure and returns true. | escapeWhitespace | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private boolean tryFailForEmptyString(@Nullable Object expected) {
if (!(actual instanceof String) || !(expected instanceof String)) {
return false;
}
String actualString = (String) actual;
String expectedString = (String) expected;
if (actualString.isEmpty()) {
failWithoutActual(fact("expected", expectedString), simpleFact("but was an empty string"));
return true;
} else if (expectedString.isEmpty()) {
failWithoutActual(simpleFact("expected an empty string"), fact("but was", actualString));
return true;
}
// Neither string was empty
return false;
} | Checks whether the actual and expected values are empty strings. If so, reports a failure and
returns true. | tryFailForEmptyString | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private static char[] asUnicodeHexEscape(char c) {
// Equivalent to String.format("\\u%04x", (int) c);
char[] r = new char[6];
r[0] = '\\';
r[1] = 'u';
r[5] = hexDigitsLower[c & 0xF];
c = (char) (c >>> 4);
r[4] = hexDigitsLower[c & 0xF];
c = (char) (c >>> 4);
r[3] = hexDigitsLower[c & 0xF];
c = (char) (c >>> 4);
r[2] = hexDigitsLower[c & 0xF];
return r;
} | Checks whether the actual and expected values are empty strings. If so, reports a failure and
returns true. | asUnicodeHexEscape | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private void failEqualityCheckNoComparisonFailure(ComparisonResult difference, Fact... facts) {
// TODO(cpovirk): Is it possible for difference.factsOrEmpty() to be nonempty? If not, remove.
metadata.fail(concat(asList(facts), difference.factsOrEmpty()));
} | Checks whether the actual and expected values are empty strings. If so, reports a failure and
returns true. | failEqualityCheckNoComparisonFailure | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void failWithBadResults(
String verb, @Nullable Object expected, String failVerb, @Nullable Object actual) {
String message =
lenientFormat(
"Not true that <%s> %s <%s>. It %s <%s>",
actualCustomStringRepresentation(),
verb,
expected,
failVerb,
actual == null ? "null reference" : actual);
failWithoutActual(simpleFact(message));
} | Legacy failure method. Prefer the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param verb the check being asserted
@param expected the expectations against which the value under test is compared
@param failVerb the failure of the check being asserted
@param actual the actual value that the value under test was compared against
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithActual(Fact, Fact...)}. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. | failWithBadResults | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void failWithCustomSubject(
String verb, @Nullable Object expected, @Nullable Object actual) {
String message =
lenientFormat(
"Not true that <%s> %s <%s>",
actual == null ? "null reference" : actual, verb, expected);
failWithoutActual(simpleFact(message));
} | Legacy failure method that accepts an alternative representation of the actual value. Prefer
the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param verb the check being asserted
@param expected the expected value of the check
@param actual the custom representation of the value under test to be reported in the failure.
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithoutActual(Fact, Fact...)}. However, if you want to preserve your exact failure
message as a migration aid, you can inline this method. | failWithCustomSubject | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void failWithoutSubject(String check) {
failWithoutActual(simpleFact(lenientFormat("Not true that the subject %s", check)));
} | Legacy failure method. Prefer the {@code fail*Actual(...)} family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithoutActual(Fact, Fact...) failWithoutActual}{@code (}{@link Fact#simpleFact
simpleFact(...)}{@code )}. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method. | failWithoutSubject | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
protected final void failWithoutActual(Fact first, Fact... rest) {
metadata.fail(ImmutableList.copyOf(Lists.asList(first, rest)));
} | Fails, reporting a message with the given facts, <i>without automatically adding the actual
value.</i>
<p>Most failure messages should report the actual value, so most checks should call {@link
#failWithActual(Fact, Fact...) failWithActual} instead. However, {@code failWithoutActual} is
useful in some cases:
<ul>
<li>when the actual value is obvious from the rest of the message. For example, {@code
isNotEmpty()} calls {@code failWithoutActual(simpleFact("expected not to be empty")}.
<li>when the actual value shouldn't come last or should have a different key than the default
of "but was." For example, {@code isNotWithin(...).of(...)} calls {@code
failWithoutActual} so that it can put the expected and actual values together, followed
by the tolerance.
</ul>
<p>Example usage: The check {@code isEmpty()} calls {@code failWithActual(simpleFact("expected
to be empty"))}.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}. | failWithoutActual | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
final void failWithoutActual(String check) {
failWithoutSubject(check);
} | Legacy failure method that excludes the actual value. Prefer the {@code fail*Actual(...)}
family of methods.
<p><b>Note:</b> While Truth's {@code fail*()} methods usually throw {@link AssertionError},
they do not do so in all cases: When users use an alternative {@link FailureStrategy}, such as
{@link Expect}, the {@code fail*()} methods may instead record the failure somewhere and then
return. To accommodate this, {@link Subject} methods should typically {@code return} after
calling a {@code fail*()} method, rather than continue onward to potentially fail a second time
or throw an exception. For cases in which a method needs to return another {@link Subject} to
the user, see {@link #ignoreCheck()}.
@param check the check being asserted
@deprecated Prefer to construct {@link Fact}-style methods, typically by using {@link
#failWithoutActual(Fact, Fact...) failWithoutActual}{@code (}{@link Fact#simpleFact
simpleFact(...)}{@code )}. However, if you want to preserve your exact failure message as a
migration aid, you can inline this method (and then inline the resulting method call, as
well). | failWithoutActual | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@DoNotCall(
"Subject.equals() is not supported. Did you mean to call"
+ " assertThat(actual).isEqualTo(expected) instead of"
+ " assertThat(actual).equals(expected)?")
@Deprecated
@Override
public final boolean equals(@Nullable Object o) {
throw new UnsupportedOperationException(
"Subject.equals() is not supported. Did you mean to call"
+ " assertThat(actual).isEqualTo(expected) instead of"
+ " assertThat(actual).equals(expected)?");
} | @throws UnsupportedOperationException always
@deprecated {@link Object#equals(Object)} is not supported on Truth subjects. If you are
writing a test assertion (actual vs. expected), use {@link #isEqualTo(Object)} instead. | equals | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@DoNotCall("Subject.hashCode() is not supported.")
@Deprecated
@Override
public final int hashCode() {
throw new UnsupportedOperationException("Subject.hashCode() is not supported.");
} | @throws UnsupportedOperationException always
@deprecated {@link Object#hashCode()} is not supported on Truth subjects. | hashCode | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
@Deprecated
@Override
public
String toString() {
throw new UnsupportedOperationException(
"Subject.toString() is not supported. Did you mean to call assertThat(foo.toString())"
+ " instead of assertThat(foo).toString()?");
} | @throws UnsupportedOperationException always
@deprecated {@link Object#toString()} is not supported on Truth subjects. | toString | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final Fact butWas() {
return fact("but was", actualCustomStringRepresentation());
} | 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. | butWas | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final String typeDescription() {
return typeDescriptionOrGuess(getClass(), typeDescriptionOverride);
} | 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. | typeDescription | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final void arrayIsEmptyImpl() {
if (actual == null) {
failWithActual(simpleFact("expected an empty array"));
} else if (getLength(actual) > 0) {
failWithActual(simpleFact("expected to be empty"));
}
} | 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. | arrayIsEmptyImpl | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final void arrayIsNotEmptyImpl() {
if (actual == null) {
failWithActual(simpleFact("expected a nonempty array"));
} else if (getLength(actual) == 0) {
failWithoutActual(simpleFact("expected not to be empty"));
}
} | 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. | arrayIsNotEmptyImpl | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
final void arrayHasLengthImpl(int length) {
if (length < 0) {
failWithoutActual(
simpleFact("could not perform length check because expected length is negative"),
fact("expected length", length),
fact("array was", actualCustomStringRepresentationForPackageMembersToCall()));
} else if (actual == null) {
failWithActual(fact("expected an array with length", length));
} else {
check("length").that(getLength(actual)).isEqualTo(length);
}
} | 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. | arrayHasLengthImpl | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
private static String typeDescriptionOrGuess(
Class<? extends Subject> clazz, @Nullable String typeDescriptionOverride) {
if (typeDescriptionOverride != null) {
return typeDescriptionOverride;
}
/*
* j2cl doesn't store enough metadata to know whether "Foo$BarSubject" is a nested class, so it
* can't tell whether the simple name is "Foo$BarSubject" or just "BarSubject": b/71808768. It
* returns "Foo$BarSubject" to err on the side of preserving information. We want just
* "BarSubject," so we strip any likely enclosing type ourselves.
*/
String subjectClass = clazz.getSimpleName().replaceFirst(".*[$]", "");
String actualClass =
(subjectClass.endsWith("Subject") && !subjectClass.equals("Subject"))
? subjectClass.substring(0, subjectClass.length() - "Subject".length())
: "Object";
return UPPER_CAMEL.to(LOWER_CAMEL, actualClass);
} | 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. | typeDescriptionOrGuess | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
static Factory<Subject, Object> objects() {
return Subject::new;
} | 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. | objects | java | google/truth | core/src/main/java/com/google/common/truth/Subject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Subject.java | Apache-2.0 |
static <T extends @Nullable Object> List<T> accumulate(T first, T second, T @Nullable [] rest) {
// rest should never be deliberately null, so assume that the caller passed null
// in the third position but intended it to be the third element in the array of values.
// Javac makes the opposite inference, so handle that here.
List<T> items = new ArrayList<>(2 + ((rest == null) ? 1 : rest.length));
items.add(first);
items.add(second);
if (rest == null) {
/*
* This cast is probably not actually safe as used in IterableSubject.UsingCorrespondence. But
* that whole API is stuck being type-unsafe unless we re-generify IterableSubject:
* b/145689657#comment1.
*/
items.add(uncheckedCastNullableTToT(null));
} else {
items.addAll(asList(rest));
}
return items;
} | Utility methods used in {@code Subject} implementors.
@author Christian Gruber
@author Jens Nyman | accumulate | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static String countDuplicates(Iterable<?> items) {
/*
* TODO(cpovirk): Remove brackets after migrating all callers to the new message format. But
* will that look OK when we put the result next to a homogeneous type name? If not, maybe move
* the homogeneous type name to a separate Fact?
*/
return countDuplicatesToMultiset(items).toStringWithBrackets();
} | Utility methods used in {@code Subject} implementors.
@author Christian Gruber
@author Jens Nyman | countDuplicates | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static String entryString(Multiset.Entry<?> entry) {
int count = entry.getCount();
String item = String.valueOf(entry.getElement());
return (count > 1) ? item + " [" + count + " copies]" : item;
} | Utility methods used in {@code Subject} implementors.
@author Christian Gruber
@author Jens Nyman | entryString | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
private static <T extends @Nullable Object> NonHashingMultiset<T> countDuplicatesToMultiset(
Iterable<T> items) {
// We use avoid hashing in case the elements don't have a proper
// .hashCode() method (e.g., MessageSet from old versions of protobuf).
NonHashingMultiset<T> multiset = NonHashingMultiset.create();
for (T item : items) {
multiset.add(item);
}
return multiset;
} | Utility methods used in {@code Subject} implementors.
@author Christian Gruber
@author Jens Nyman | countDuplicatesToMultiset | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static String countDuplicatesAndAddTypeInfo(Iterable<?> itemsIterable) {
Collection<?> items = iterableToCollection(itemsIterable);
String homogeneousTypeName = getHomogeneousTypeName(items);
return homogeneousTypeName != null
? lenientFormat("%s (%s)", countDuplicates(items), homogeneousTypeName)
: countDuplicates(addTypeInfoToEveryItem(items));
} | Makes a String representation of {@code items} with collapsed duplicates and additional class
info.
<p>Example: {@code countDuplicatesAndAddTypeInfo([1, 2, 2, 3]) == "[1, 2 [3 copies]]
(java.lang.Integer)"} and {@code countDuplicatesAndAddTypeInfo([1, 2L]) == "[1
(java.lang.Integer), 2 (java.lang.Long)]"}. | countDuplicatesAndAddTypeInfo | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static DuplicateGroupedAndTyped countDuplicatesAndMaybeAddTypeInfoReturnObject(
Iterable<?> itemsIterable, boolean addTypeInfo) {
if (addTypeInfo) {
Collection<?> items = iterableToCollection(itemsIterable);
String homogeneousTypeName = getHomogeneousTypeName(items);
NonHashingMultiset<?> valuesWithCountsAndMaybeTypes =
homogeneousTypeName != null
? countDuplicatesToMultiset(items)
: countDuplicatesToMultiset(addTypeInfoToEveryItem(items));
return DuplicateGroupedAndTyped.create(valuesWithCountsAndMaybeTypes, homogeneousTypeName);
} else {
return DuplicateGroupedAndTyped.create(
countDuplicatesToMultiset(itemsIterable), /* homogeneousTypeToDisplay= */ null);
}
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | countDuplicatesAndMaybeAddTypeInfoReturnObject | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
int totalCopies() {
return contents.size();
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | totalCopies | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
boolean isEmpty() {
return contents.isEmpty();
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | isEmpty | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
Iterable<Multiset.Entry<?>> entrySet() {
return transform(contents.entrySet(), this::unwrapKey);
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | entrySet | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
String toStringWithBrackets() {
List<String> parts = new ArrayList<>();
for (Multiset.Entry<?> entry : entrySet()) {
parts.add(entryString(entry));
}
return parts.toString();
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | toStringWithBrackets | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@Override
public String toString() {
String withBrackets = toStringWithBrackets();
return withBrackets.substring(1, withBrackets.length() - 1);
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | toString | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
private Multiset.Entry<?> unwrapKey(Multiset.Entry<Wrapper<E>> input) {
return immutableEntry(input.getElement().get(), input.getCount());
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | unwrapKey | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@Override
protected boolean doEquivalent(Object a, Object b) {
return Objects.equals(a, b);
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | doEquivalent | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@Override
protected int doHash(Object o) {
return 0; // slow but hopefully not much worse than what we get with a flat list
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | doHash | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <E extends @Nullable Object> NonHashingMultiset<E> create() {
return new NonHashingMultiset<>();
} | Similar to {@link #countDuplicatesAndAddTypeInfo} and {@link #countDuplicates} but (a) only
adds type info if requested and (b) returns a richer object containing the data. | create | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@Nullable String getHomogeneousTypeToDisplay() {
return homogeneousTypeToDisplay;
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | getHomogeneousTypeToDisplay | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
int totalCopies() {
return valuesAndMaybeTypes.totalCopies();
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | totalCopies | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
boolean isEmpty() {
return valuesAndMaybeTypes.isEmpty();
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | isEmpty | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
Iterable<Multiset.Entry<?>> entrySet() {
return valuesAndMaybeTypes.entrySet();
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | entrySet | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@Override
public String toString() {
return homogeneousTypeToDisplay != null
? valuesAndMaybeTypes + " (" + homogeneousTypeToDisplay + ")"
: valuesAndMaybeTypes.toString();
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | toString | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static DuplicateGroupedAndTyped create(
NonHashingMultiset<?> valuesAndMaybeTypes, @Nullable String homogeneousTypeToDisplay) {
return new DuplicateGroupedAndTyped(valuesAndMaybeTypes, homogeneousTypeToDisplay);
} | Missing or unexpected values from a collection assertion, with equal objects grouped together
and, in some cases, type information added. If the type information is present, it is either
present in {@code homogeneousTypeToDisplay} (if all objects have the same type) or appended to
each individual element (if some elements have different types).
<p>This allows collection assertions to the type information on a separate line from the
elements and even to output different elements on different lines. | create | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static List<@Nullable Object> retainMatchingToString(
Iterable<?> items, Iterable<?> itemsToCheck) {
ListMultimap<String, @Nullable Object> stringValueToItemsToCheck = ArrayListMultimap.create();
for (Object itemToCheck : itemsToCheck) {
stringValueToItemsToCheck.put(String.valueOf(itemToCheck), itemToCheck);
}
List<@Nullable Object> result = new ArrayList<>();
for (Object item : items) {
for (Object itemToCheck : stringValueToItemsToCheck.get(String.valueOf(item))) {
if (!Objects.equals(itemToCheck, item)) {
result.add(item);
break;
}
}
}
return result;
} | Returns a new collection containing all elements in {@code items} for which there exists at
least one element in {@code itemsToCheck} that has the same {@code toString()} value without
being equal.
<p>Example: {@code retainMatchingToString([1L, 2L, 2L], [2, 3]) == [2L, 2L]} | retainMatchingToString | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static boolean hasMatchingToStringPair(Iterable<?> items1, Iterable<?> items2) {
// Bail early for empty iterables to avoid calling hashCode() on the elements unnecessarily.
return !isEmpty(items1)
&& !isEmpty(items2)
&& !retainMatchingToString(items1, items2).isEmpty();
} | Returns true if there is a pair of an item from {@code items1} and one in {@code items2} that
has the same {@code toString()} value without being equal.
<p>Example: {@code hasMatchingToStringPair([1L, 2L], [1]) == true} | hasMatchingToStringPair | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static String objectToTypeName(@Nullable Object item) {
// TODO(cpovirk): Merge this with the code in Subject.failEqualityCheck().
if (item == null) {
// The name "null type" comes from the interface javax.lang.model.type.NullType.
return "null type";
} else if (item instanceof Map.Entry) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) item;
// Fix for interesting bug when entry.getValue() returns itself b/170390717
String valueTypeName =
entry.getValue() == entry ? "Map.Entry" : objectToTypeName(entry.getValue());
return lenientFormat("Map.Entry<%s, %s>", objectToTypeName(entry.getKey()), valueTypeName);
} else {
return item.getClass().getName();
}
} | Returns true if there is a pair of an item from {@code items1} and one in {@code items2} that
has the same {@code toString()} value without being equal.
<p>Example: {@code hasMatchingToStringPair([1L, 2L], [1]) == true} | objectToTypeName | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
private static @Nullable String getHomogeneousTypeName(Iterable<?> items) {
String homogeneousTypeName = null;
for (Object item : items) {
if (item == null) {
/*
* TODO(cpovirk): Why? We could have multiple nulls, which would be homogeneous. More
* likely, we could have exactly one null, which is still homogeneous. Arguably it's weird
* to call a single element "homogeneous" at all, but that's not specific to null.
*/
return null;
} else if (homogeneousTypeName == null) {
// This is the first item
homogeneousTypeName = objectToTypeName(item);
} else if (!objectToTypeName(item).equals(homogeneousTypeName)) {
// items is a heterogeneous collection
return null;
}
}
return homogeneousTypeName;
} | Returns the name of the single type of all given items or {@code null} if no such type exists. | getHomogeneousTypeName | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
private static List<String> addTypeInfoToEveryItem(Iterable<?> items) {
List<String> itemsWithTypeInfo = new ArrayList<>();
for (Object item : items) {
itemsWithTypeInfo.add(lenientFormat("%s (%s)", item, objectToTypeName(item)));
}
return itemsWithTypeInfo;
} | Returns the name of the single type of all given items or {@code null} if no such type exists. | addTypeInfoToEveryItem | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <T extends @Nullable Object> Collection<T> iterableToCollection(
@Nullable Iterable<T> iterable) {
// TODO(cpovirk): For null inputs, produce a better exception message (ideally in callers).
checkNotNull(iterable);
if (iterable instanceof Collection) {
// Should be safe to assume that any Iterable implementing Collection isn't a one-shot
// iterable, right? I sure hope so.
return (Collection<T>) iterable;
} else {
return Lists.newArrayList(iterable);
}
} | Returns the name of the single type of all given items or {@code null} if no such type exists. | iterableToCollection | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <T extends @Nullable Object> List<T> iterableToList(Iterable<T> iterable) {
if (iterable instanceof List) {
return (List<T>) iterable;
} else {
return Lists.newArrayList(iterable);
}
} | Returns the name of the single type of all given items or {@code null} if no such type exists. | iterableToList | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <T extends @Nullable Object> Iterable<T> annotateEmptyStrings(Iterable<T> items) {
if (Iterables.contains(items, "")) {
List<T> annotatedItems = new ArrayList<>();
for (T item : items) {
if (Objects.equals(item, "")) {
// This is a safe cast because know that at least one instance of T (this item) is a
// String.
@SuppressWarnings("unchecked")
T newItem = (T) HUMAN_UNDERSTANDABLE_EMPTY_STRING;
annotatedItems.add(newItem);
} else {
annotatedItems.add(item);
}
}
return annotatedItems;
} else {
return items;
}
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | annotateEmptyStrings | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@SafeVarargs
static <E> ImmutableList<E> concat(Iterable<? extends E>... inputs) {
return ImmutableList.copyOf(Iterables.concat(inputs));
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | concat | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <E> ImmutableList<E> append(E[] array, E e) {
return new ImmutableList.Builder<E>().add(array).add(e).build();
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | append | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <E> ImmutableList<E> append(ImmutableList<? extends E> list, E e) {
return new ImmutableList.Builder<E>().addAll(list).add(e).build();
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | append | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
static <E> ImmutableList<E> sandwich(E first, E[] array, E last) {
return new ImmutableList.Builder<E>().add(first).add(array).add(last).build();
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | sandwich | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
@SuppressWarnings("nullness") // TODO: b/316358623 - Remove suppression after fixing checker
static <E extends @Nullable Object> List<E> asList(E... a) {
return Arrays.asList(a);
} | Returns an iterable with all empty strings replaced by a non-empty human understandable
indicator for an empty string.
<p>Returns the given iterable if it contains no empty strings. | asList | java | google/truth | core/src/main/java/com/google/common/truth/SubjectUtils.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/SubjectUtils.java | Apache-2.0 |
public void isEmpty() {
if (!checkNotNull(actual).isEmpty()) {
failWithActual(simpleFact("expected to be empty"));
}
} | Checks that the actual table is empty. | isEmpty | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void isNotEmpty() {
if (checkNotNull(actual).isEmpty()) {
failWithoutActual(simpleFact("expected not to be empty"));
}
} | Checks that the actual table is not empty. | isNotEmpty | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void hasSize(int expectedSize) {
checkArgument(expectedSize >= 0, "expectedSize(%s) must be >= 0", expectedSize);
check("size()").that(checkNotNull(actual).size()).isEqualTo(expectedSize);
} | Checks that the actual table has the given size. | hasSize | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void contains(@Nullable Object rowKey, @Nullable Object columnKey) {
if (!checkNotNull(actual).contains(rowKey, columnKey)) {
/*
* TODO(cpovirk): Consider including information about whether any cell with the given row
* *or* column was present.
*/
failWithActual(
simpleFact("expected to contain mapping for row-column key pair"),
fact("row key", rowKey),
fact("column key", columnKey));
}
} | Checks that the actual table contains a mapping for the given row key and column key. | contains | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void doesNotContain(@Nullable Object rowKey, @Nullable Object columnKey) {
if (checkNotNull(actual).contains(rowKey, columnKey)) {
failWithoutActual(
simpleFact("expected not to contain mapping for row-column key pair"),
fact("row key", rowKey),
fact("column key", columnKey),
fact("but contained value", actual.get(rowKey, columnKey)),
fact("full contents", actual));
}
} | Checks that the actual table does not contain a mapping for the given row key and column key. | doesNotContain | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void containsCell(
@Nullable Object rowKey, @Nullable Object colKey, @Nullable Object value) {
containsCell(
Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell(
rowKey, colKey, value));
} | Checks that the actual table contains the given cell. | containsCell | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void containsCell(Cell<?, ?, ?> cell) {
checkNotNull(cell);
checkNoNeedToDisplayBothValues("cellSet()").that(checkNotNull(actual).cellSet()).contains(cell);
} | Checks that the actual table contains the given cell. | containsCell | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void doesNotContainCell(
@Nullable Object rowKey, @Nullable Object colKey, @Nullable Object value) {
doesNotContainCell(
Tables.<@Nullable Object, @Nullable Object, @Nullable Object>immutableCell(
rowKey, colKey, value));
} | Checks that the actual table does not contain the given cell. | doesNotContainCell | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public void doesNotContainCell(Cell<?, ?, ?> cell) {
checkNotNull(cell);
checkNoNeedToDisplayBothValues("cellSet()")
.that(checkNotNull(actual).cellSet())
.doesNotContain(cell);
} | Checks that the actual table does not contain the given cell. | doesNotContainCell | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
static Factory<TableSubject, Table<?, ?, ?>> tables() {
return TableSubject::new;
} | Checks that the actual table contains the given value. | tables | java | google/truth | core/src/main/java/com/google/common/truth/TableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/TableSubject.java | Apache-2.0 |
public final StringSubject hasMessageThat() {
// We provide a more helpful error message if hasCauseThat() methods are chained too deep, as in
// assertThat(new Exception()).hasCauseThat().hasMessageThat()....
// This message also triggers for the simpler case of assertThat(null).hasMessageThat()....
if (actual == null) {
failForNullThrowable("Attempt to assert about the message of a null Throwable");
return ignoreCheck().that("");
}
StandardSubjectBuilder check = check("getMessage()");
if (actual instanceof ErrorWithFacts && ((ErrorWithFacts) actual).facts().size() > 1) {
check =
check.withMessage(
"(Note from Truth: When possible, instead of asserting on the full message, assert"
+ " about individual facts by using ExpectFailure.assertThat.)");
}
return check.that(actual.getMessage());
} | Returns a {@code StringSubject} to make assertions about the throwable's message. | hasMessageThat | java | google/truth | core/src/main/java/com/google/common/truth/ThrowableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/ThrowableSubject.java | Apache-2.0 |
@SuppressWarnings("ShouldNotSubclass")
public final ThrowableSubject hasCauseThat() {
// We provide a more helpful error message if hasCauseThat() methods are chained too deep, as in
// assertThat(new Exception()).hasCauseThat().hasCauseThat()....
// This message also triggers for the simpler case of assertThat(null).hasCauseThat()....
if (actual == null) {
failForNullThrowable("Attempt to assert about the cause of a null Throwable");
return ignoreCheck()
.that(
new Throwable() {
@Override
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
public Throwable fillInStackTrace() {
setStackTrace(new StackTraceElement[0]); // for old versions of Android
return this;
}
});
}
return check("getCause()").that(actual.getCause());
} | Returns a new {@code ThrowableSubject} that supports assertions on this throwable's direct
cause. This method can be invoked repeatedly (e.g. {@code
assertThat(e).hasCauseThat().hasCauseThat()....} to assert on a particular indirect cause. | hasCauseThat | java | google/truth | core/src/main/java/com/google/common/truth/ThrowableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/ThrowableSubject.java | Apache-2.0 |
@Override
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
public Throwable fillInStackTrace() {
setStackTrace(new StackTraceElement[0]); // for old versions of Android
return this;
} | Returns a new {@code ThrowableSubject} that supports assertions on this throwable's direct
cause. This method can be invoked repeatedly (e.g. {@code
assertThat(e).hasCauseThat().hasCauseThat()....} to assert on a particular indirect cause. | fillInStackTrace | java | google/truth | core/src/main/java/com/google/common/truth/ThrowableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/ThrowableSubject.java | Apache-2.0 |
static Factory<ThrowableSubject, Throwable> throwables() {
return ThrowableSubject::new;
} | Returns a new {@code ThrowableSubject} that supports assertions on this throwable's direct
cause. This method can be invoked repeatedly (e.g. {@code
assertThat(e).hasCauseThat().hasCauseThat()....} to assert on a particular indirect cause. | throwables | java | google/truth | core/src/main/java/com/google/common/truth/ThrowableSubject.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/ThrowableSubject.java | Apache-2.0 |
@SuppressWarnings("MemberName") // The underscore is a weird but intentional choice.
public static StandardSubjectBuilder assert_() {
return ASSERT;
} | Begins a call chain with the fluent Truth API. If the check made by the chain fails, it will
throw {@link AssertionError}. | assert_ | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static StandardSubjectBuilder assertWithMessage(@Nullable String messageToPrepend) {
return assert_().withMessage(messageToPrepend);
} | Begins an assertion that, if it fails, will prepend the given message to the failure message.
<p>This method is a shortcut for {@code assert_().withMessage(...)}.
<p>To set a message when using a custom subject, use {@code assertWithMessage(...).}{@link
StandardSubjectBuilder#about about(...)}, as discussed in <a
href="https://truth.dev/faq#java8">this FAQ entry</a>. | assertWithMessage | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static StandardSubjectBuilder assertWithMessage(String format, @Nullable Object... args) {
return assert_().withMessage(format, args);
} | Begins an assertion that, if it fails, will prepend the given message to the failure message.
<p><b>Note:</b> the arguments will be substituted into the format template using {@link
com.google.common.base.Strings#lenientFormat Strings.lenientFormat}. Note this only supports
the {@code %s} specifier.
<p>This method is a shortcut for {@code assert_().withMessage(...)}.
<p>To set a message when using a custom subject, use {@code assertWithMessage(...).}{@link
StandardSubjectBuilder#about about(...)}, as discussed in <a
href="https://truth.dev/faq#java8">this FAQ entry</a>.
@throws IllegalArgumentException if the number of placeholders in the format string does not
equal the number of given arguments | assertWithMessage | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static <S extends Subject, T> SimpleSubjectBuilder<S, T> assertAbout(
Subject.Factory<S, T> factory) {
return assert_().about(factory);
} | Given a factory for some {@code Subject} class, returns a builder whose {@code that(actual)}
method creates instances of that class. | assertAbout | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static <CustomSubjectBuilderT extends CustomSubjectBuilder>
CustomSubjectBuilderT assertAbout(
CustomSubjectBuilder.Factory<CustomSubjectBuilderT> factory) {
return assert_().about(factory);
} | A generic, advanced method of extension of Truth to new types, which is documented on {@link
CustomSubjectBuilder}. Extension creators should prefer {@link Subject.Factory} if possible. | assertAbout | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static <ComparableT extends Comparable<?>> ComparableSubject<ComparableT> assertThat(
@Nullable ComparableT actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Comparable}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static BigDecimalSubject assertThat(@Nullable BigDecimal actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link BigDecimal}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static Subject assertThat(@Nullable Object actual) {
return assert_().that(actual);
} | Begins an assertion about an {@link Object}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
@GwtIncompatible("ClassSubject.java")
@J2ktIncompatible
public static ClassSubject assertThat(@Nullable Class<?> actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Class}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static ThrowableSubject assertThat(@Nullable Throwable actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Throwable}.
<p>Truth does not provide its own support for calling a method and automatically catching an
expected exception, only for asserting on the exception after it has been caught. To catch the
exception, we suggest {@link org.junit.Assert#assertThrows(Class,
org.junit.function.ThrowingRunnable) assertThrows} (JUnit), <a
href="https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-fails-with.html">{@code
assertFailsWith}</a> ({@code kotlin.test}), or similar functionality from your testing library
of choice.
<pre>
InvocationTargetException expected =
assertThrows(InvocationTargetException.class, () -> method.invoke(null));
assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
</pre> | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static LongSubject assertThat(@Nullable Long actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Long}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static DoubleSubject assertThat(@Nullable Double actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Double}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static FloatSubject assertThat(@Nullable Float actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Float}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static IntegerSubject assertThat(@Nullable Integer actual) {
return assert_().that(actual);
} | Begins an assertion about an {@link Integer}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static BooleanSubject assertThat(@Nullable Boolean actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link Boolean}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static StringSubject assertThat(@Nullable String actual) {
return assert_().that(actual);
} | Begins an assertion about a {@link String}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static IterableSubject assertThat(@Nullable Iterable<?> actual) {
return assert_().that(actual);
} | Begins an assertion about an {@link Iterable}. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
@SuppressWarnings("AvoidObjectArrays")
public static <T extends @Nullable Object> ObjectArraySubject<T> assertThat(
T @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about an {@link Object} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static PrimitiveBooleanArraySubject assertThat(boolean @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about a {@code boolean} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static PrimitiveShortArraySubject assertThat(short @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about a {@code short} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static PrimitiveIntArraySubject assertThat(int @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about an {@code int} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static PrimitiveLongArraySubject assertThat(long @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about a {@code long} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
public static PrimitiveByteArraySubject assertThat(byte @Nullable [] actual) {
return assert_().that(actual);
} | Begins an assertion about a {@code byte} array. | assertThat | java | google/truth | core/src/main/java/com/google/common/truth/Truth.java | https://github.com/google/truth/blob/master/core/src/main/java/com/google/common/truth/Truth.java | Apache-2.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.