Skip to content

Commit

Permalink
Add @CheckReturnValue to Truth's package-info.java.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=113490153
  • Loading branch information
kluever authored and cpovirk committed Feb 2, 2016
1 parent 7168161 commit 89243a6
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 49 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Expand Up @@ -60,6 +60,11 @@
<version>0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<version>2.0.8</version>
</dependency>
</dependencies>
<build>
<resources>
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/java/com/google/common/truth/AbstractVerb.java
Expand Up @@ -18,7 +18,6 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.StringUtil.format;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

public abstract class AbstractVerb<T extends AbstractVerb<T>> {
Expand Down Expand Up @@ -56,7 +55,6 @@ public void fail(@Nullable String format, Object... args) {
* @return A custom verb which will show the descriptive message along with the normal failure
* text.
*/
@CheckReturnValue
public abstract T withFailureMessage(@Nullable String failureMessage);

@Nullable
Expand Down Expand Up @@ -93,7 +91,6 @@ public DelegatedVerb(FailureStrategy failureStrategy, SubjectFactory<S, T> facto
this.failureStrategy = checkNotNull(failureStrategy);
}

@CheckReturnValue
public S that(T target) {
return factory.getSubject(failureStrategy, target);
}
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/java/com/google/common/truth/DoubleSubject.java
Expand Up @@ -22,7 +22,6 @@
import static com.google.common.truth.MathUtil.notEqualWithinTolerance;
import static java.lang.Double.doubleToLongBits;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -94,7 +93,6 @@ public int hashCode() {
* allowed by the check, which must be a non-negative finite value, i.e. not
* {@link Double#NaN}, {@link Double#POSITIVE_INFINITY}, or negative, including {@code -0.0}
*/
@CheckReturnValue
public TolerantDoubleComparison isWithin(final double tolerance) {
return new TolerantDoubleComparison() {
@Override
Expand Down Expand Up @@ -130,7 +128,6 @@ public void of(double expected) {
* allowed by the check, which must be a non-negative finite value, i.e. not
* {@code Double.NaN}, {@code Double.POSITIVE_INFINITY}, or negative, including {@code -0.0}
*/
@CheckReturnValue
public TolerantDoubleComparison isNotWithin(final double tolerance) {
return new TolerantDoubleComparison() {
@Override
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/com/google/common/truth/FloatSubject.java
Expand Up @@ -22,15 +22,13 @@
import static com.google.common.truth.MathUtil.notEqualWithinTolerance;
import static java.lang.Float.floatToIntBits;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
* Propositions for {@link Float} subjects.
*
* @author Kurt Alfred Kluever
*/
@CheckReturnValue
public final class FloatSubject extends ComparableSubject<FloatSubject, Float> {
private static final int NEG_ZERO_BITS = floatToIntBits(-0.0f);

Expand Down
Expand Up @@ -30,6 +30,7 @@
import com.google.common.collect.Multiset.Entry;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -55,6 +56,7 @@ protected IterableSubject(FailureStrategy failureStrategy, @Nullable Iterable<?>
* representations of the subject.
*/
@Override
@CanIgnoreReturnValue
public IterableSubject named(String name) {
super.named(name);
return this;
Expand Down Expand Up @@ -174,6 +176,7 @@ private void containsAny(String failVerb, Iterable<?> expected) {
* <p>Callers may optionally chain an {@code inOrder()} call if its expected
* contents must be contained in the given order.
*/
@CanIgnoreReturnValue
public final Ordered containsAllOf(
@Nullable Object first, @Nullable Object second, @Nullable Object... rest) {
return containsAll("contains all of", accumulate(first, second, rest));
Expand All @@ -187,6 +190,7 @@ public final Ordered containsAllOf(
* <p>Callers may optionally chain an {@code inOrder()} call if its expected
* contents must be contained in the given order.
*/
@CanIgnoreReturnValue
public final Ordered containsAllIn(Iterable<?> expected) {
return containsAll("contains all elements in", expected);
}
Expand Down Expand Up @@ -242,6 +246,7 @@ private static void moveElements(List<?> input, Collection<Object> output, int m
* <p>Callers may optionally chain an {@code inOrder()} call if its expected
* contents must be contained in the given order.
*/
@CanIgnoreReturnValue
public final Ordered containsExactly(@Nullable Object... varargs) {
List<Object> expected = (varargs == null) ? Lists.newArrayList((Object) null) : asList(varargs);
return containsExactly(
Expand All @@ -260,6 +265,7 @@ public final Ordered containsExactly(@Nullable Object... varargs) {
* <p>Callers may optionally chain an {@code inOrder()} call if its expected
* contents must be contained in the given order.
*/
@CanIgnoreReturnValue
public final Ordered containsExactlyElementsIn(Iterable<?> expected) {
return containsExactly("contains exactly", expected, false);
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/com/google/common/truth/MapSubject.java
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;
import com.google.common.collect.Multiset;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -42,6 +43,7 @@ public class MapSubject extends Subject<MapSubject, Map<?, ?>> {
/**
* Fails if the subject is not equal to the given object.
*/
@Override
public void isEqualTo(@Nullable Object other) {
if (!Objects.equal(getSubject(), other)) {
if (other instanceof Map) {
Expand Down Expand Up @@ -140,6 +142,7 @@ public void doesNotContainEntry(@Nullable Object key, @Nullable Object value) {
/**
* Fails if the map is not empty.
*/
@CanIgnoreReturnValue
public Ordered containsExactly() {
return check().that(getSubject().entrySet()).containsExactly();
}
Expand All @@ -151,6 +154,7 @@ public Ordered containsExactly() {
* key/value pairs at compile time. Please make sure you provide varargs in key/value pairs!
*/
// TODO(b/25744307): Can we add an error-prone check that rest.length % 2 == 0?
@CanIgnoreReturnValue
public Ordered containsExactly(@Nullable Object k0, @Nullable Object v0, Object... rest) {
checkArgument(
rest.length % 2 == 0,
Expand All @@ -177,6 +181,7 @@ public Ordered containsExactly(@Nullable Object k0, @Nullable Object v0, Object.
/**
* Fails if the map does not contain exactly the given set of entries in the given map.
*/
@CanIgnoreReturnValue
public Ordered containsExactlyEntriesIn(Map<?, ?> expectedMap) {
return check().that(getSubject().entrySet()).containsExactlyElementsIn(expectedMap.entrySet());
}
Expand Down
Expand Up @@ -28,14 +28,14 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -131,7 +131,6 @@ public void doesNotContainEntry(@Nullable Object key, @Nullable Object value) {
* <p>This method performs no checks on its own and cannot cause test failures. Subsequent
* assertions must be chained onto this method call to test properties of the Multimap.
*/
@CheckReturnValue
public IterableSubject valuesForKey(@Nullable Object key) {
return new IterableValuesForKey(failureStrategy, this, key);
}
Expand Down Expand Up @@ -176,6 +175,7 @@ public void isEqualTo(@Nullable Object other) {
* that the two Multimaps iterate fully in the same order. That is, their key sets iterate
* in the same order, and the value collections for each key iterate in the same order.
*/
@CanIgnoreReturnValue
public Ordered containsExactlyEntriesIn(Multimap<?, ?> expectedMultimap) {
checkNotNull(expectedMultimap, "expectedMultimap");
return containsExactly("contains exactly", expectedMultimap);
Expand All @@ -185,6 +185,7 @@ public Ordered containsExactlyEntriesIn(Multimap<?, ?> expectedMultimap) {
* @deprecated Use {@link #containsExactlyEntriesIn} instead.
*/
@Deprecated
@CanIgnoreReturnValue
public Ordered containsExactly(Multimap<?, ?> expectedMultimap) {
return containsExactlyEntriesIn(expectedMultimap);
}
Expand Down
Expand Up @@ -32,15 +32,13 @@
import java.util.Arrays;
import java.util.List;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
* A Subject to handle testing propositions for {@code Object[]} and more generically {@code T[]}.
*
* @author Christian Gruber
*/
@CheckReturnValue
public class ObjectArraySubject<T> extends AbstractArraySubject<ObjectArraySubject<T>, T[]> {
private final String typeName;
private final int numberOfDimensions;
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand All @@ -39,7 +38,6 @@
*
* @author Christian Gruber (cgruber@israfil.net)
*/
@CheckReturnValue
public class PrimitiveDoubleArraySubject
extends AbstractArraySubject<PrimitiveDoubleArraySubject, double[]> {
PrimitiveDoubleArraySubject(FailureStrategy failureStrategy, @Nullable double[] o) {
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand All @@ -39,7 +38,6 @@
*
* @author Christian Gruber (cgruber@israfil.net)
*/
@CheckReturnValue
public class PrimitiveFloatArraySubject
extends AbstractArraySubject<PrimitiveFloatArraySubject, float[]> {
PrimitiveFloatArraySubject(FailureStrategy failureStrategy, @Nullable float[] o) {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/google/common/truth/Subject.java
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.Objects;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.List;

Expand Down Expand Up @@ -53,6 +54,7 @@ protected String internalCustomName() {
* representations of the subject.
*/
@SuppressWarnings("unchecked")
@CanIgnoreReturnValue
public S named(String name) {
// TODO: use check().withFailureMessage... here?
this.customName = checkNotNull(name, "Name passed to named() cannot be null.");
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/com/google/common/truth/TestVerb.java
Expand Up @@ -28,10 +28,8 @@
import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

@CheckReturnValue
public class TestVerb extends AbstractVerb<TestVerb> {
private static final Object[] EMPTY_ARGS = new Object[0];
@Nullable private final String format;
Expand Down
50 changes: 24 additions & 26 deletions core/src/main/java/com/google/common/truth/Truth.gwt.xml
@@ -1,29 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- semi-autogenerated module descriptor -->
<module>
<source path="">
<!-- Hack to keep collect from hiding collect.testing supersource: -->
<exclude name="**/testing/**"/>
</source>

<!--
We used to set this only for packages that had manual supersource.
That worked everywhere that I know of except for one place:
when running the GWT util.concurrent tests under Guava.
The problem is that GWT responds poorly to two .gwt.xml files in the same Java package:
http://goo.gl/CO7dLB
The summary is that it ignores one file in favor of the other.
util.concurrent, like nearly all our packages, has two .gwt.xml files: one for prod and one for tests.
util.concurrent, unlike our other packages, has, as of this writing, test supersource but no prod supersource.
GWT happens to use the prod .gwt.xml, so it looks for no supersource for tests, either.
This causes it to fail to find AtomicLongMapTest.
Our workaround is to tell GWT that util.concurrent and all other packages have prod supersource, even if they have none.
GWT is happy to ignore us when we specify a nonexistent path.
(I hope that this workaround does not cause its own problems in the future.)
-->
<super-source path="super"/>
<inherits name="com.google.common.annotations.Annotations"/>
<inherits name="com.google.common.base.Base"/>
<inherits name="com.google.common.collect.Collect"/>
<inherits name="com.google.common.primitives.Primitives"/>
<inherits name="com.google.gwt.core.Core"/>

<source path="">

<!-- Hack to keep collect from hiding collect.testing supersource: -->

<exclude name="**/testing/**"/>

</source>

<!-- We used to set this only for packages that had manual supersource. That worked everywhere that I know of except for one place: when running the GWT util.concurrent tests under Guava. The problem is that GWT responds poorly to two .gwt.xml files in the same Java package: http://goo.gl/CO7dLB The summary is that it ignores one file in favor of the other. util.concurrent, like nearly all our packages, has two .gwt.xml files: one for prod and one for tests. util.concurrent, unlike our other packages, has, as of this writing, test supersource but no prod supersource. GWT happens to use the prod .gwt.xml, so it looks for no supersource for tests, either. This causes it to fail to find AtomicLongMapTest. Our workaround is to tell GWT that util.concurrent and all other packages have prod supersource, even if they have none. GWT is happy to ignore us when we specify a nonexistent path. (I hope that this workaround does not cause its own problems in the future.) -->

<super-source path="super"/>

<inherits name="com.google.common.annotations.Annotations"/>

<inherits name="com.google.common.base.Base"/>

<inherits name="com.google.common.collect.Collect"/>

<inherits name="com.google.common.primitives.Primitives"/>

<inherits name="com.google.gwt.core.Core"/>

</module>
2 changes: 0 additions & 2 deletions core/src/main/java/com/google/common/truth/Truth.java
Expand Up @@ -27,7 +27,6 @@
import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -67,7 +66,6 @@
* @author David Saff
* @author Christian Gruber (cgruber@israfil.net)
*/
@CheckReturnValue
public final class Truth {
private Truth() {}

Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/com/google/common/truth/package-info.java
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Truth is an open source, fluent testing framework for Java that is designed to make your
* test assertions and failure messages more readable.
*
* <p>This package is a part of the open-source
* <a href="http://github.com/google/truth">Truth</a> library.
*/
@CheckReturnValue
@ParametersAreNonnullByDefault
package com.google.common.truth;

import javax.annotation.CheckReturnValue;
import javax.annotation.ParametersAreNonnullByDefault;
4 changes: 2 additions & 2 deletions core/src/test/java/com/google/common/truth/SubjectTest.java
Expand Up @@ -670,7 +670,7 @@ public void throwableHasInitedCause() {
@Test
public void equalsThrowsUSOE() {
try {
assertThat(5).equals(5);
boolean unused = assertThat(5).equals(5);
} catch (UnsupportedOperationException expected) {
assertThat(expected)
.hasMessage("If you meant to test object equality, use .isEqualTo(other) instead.");
Expand All @@ -682,7 +682,7 @@ public void equalsThrowsUSOE() {
@Test
public void hashCodeThrowsUSOE() {
try {
assertThat(5).hashCode();
int unused = assertThat(5).hashCode();
} catch (UnsupportedOperationException expected) {
assertThat(expected).hasMessage("Subject.hashCode() is not supported.");
return;
Expand Down

0 comments on commit 89243a6

Please sign in to comment.