Include index at which the unexpected item was found in the list.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=90811709
diff --git a/core/src/main/java/com/google/common/truth/Subject.java b/core/src/main/java/com/google/common/truth/Subject.java
index 0e4a9e2..82d2e3f 100644
--- a/core/src/main/java/com/google/common/truth/Subject.java
+++ b/core/src/main/java/com/google/common/truth/Subject.java
@@ -20,6 +20,8 @@
import static com.google.common.truth.SubjectUtils.accumulate;
import com.google.common.base.Objects;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import java.util.List;
@@ -168,10 +170,10 @@
* Fails if the subject is equal to any element in the given iterable.
*/
public void isNotIn(Iterable<?> iterable) {
- if (Iterables.contains(iterable, getSubject())) {
- // TODO(kak): We might want to beef up the error message to include the index at which the
- // unexpected element actually occurred.
- fail("is not equal to any element in", iterable);
+ int index = Iterables.indexOf(iterable, Predicates.<Object>equalTo(getSubject()));
+ if (index != -1 ) {
+ failWithRawMessage("Not true that %s is not in %s. It was found at index %s",
+ getDisplaySubject(), iterable, index);
}
}
@@ -179,12 +181,7 @@
* Fails if the subject is equal to any of the given elements.
*/
public void isNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object... rest) {
- List<Object> list = accumulate(first, second, rest);
- if (list.contains(getSubject())) {
- // TODO(kak): We might want to beef up the error message to include the index at which the
- // unexpected element actually occurred.
- fail("is not equal to any of", list);
- }
+ isNotIn(accumulate(first, second, rest));
}
protected T getSubject() {
diff --git a/core/src/test/java/com/google/common/truth/SubjectTest.java b/core/src/test/java/com/google/common/truth/SubjectTest.java
index 43452ac..10cfe26 100644
--- a/core/src/test/java/com/google/common/truth/SubjectTest.java
+++ b/core/src/test/java/com/google/common/truth/SubjectTest.java
@@ -495,7 +495,8 @@
assertThat("b").isNotIn(oneShotIterable("a", "b", "c"));
fail("Should have thrown.");
} catch (AssertionError e) {
- assertThat(e).hasMessage("Not true that <\"b\"> is not equal to any element in <[a, b, c]>");
+ assertThat(e)
+ .hasMessage("Not true that <\"b\"> is not in [a, b, c]. It was found at index 1");
}
}
@@ -509,7 +510,7 @@
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
- .hasMessage("Not true that <\"null\"> is not equal to any element in <[a, b, null]>");
+ .hasMessage("Not true that <\"null\"> is not in [a, b, null]. It was found at index 2");
}
}
@@ -526,7 +527,8 @@
assertThat("b").isNoneOf("a", "b", "c");
fail("Should have thrown.");
} catch (AssertionError e) {
- assertThat(e).hasMessage("Not true that <\"b\"> is not equal to any of <[a, b, c]>");
+ assertThat(e)
+ .hasMessage("Not true that <\"b\"> is not in [a, b, c]. It was found at index 1");
}
}
@@ -539,7 +541,8 @@
assertThat((String) null).isNoneOf("a", "b", (String) null);
fail("Should have thrown.");
} catch (AssertionError e) {
- assertThat(e).hasMessage("Not true that <\"null\"> is not equal to any of <[a, b, null]>");
+ assertThat(e)
+ .hasMessage("Not true that <\"null\"> is not in [a, b, null]. It was found at index 2");
}
}