Skip to content

Commit

Permalink
Override, deprecate, and check for non-Iterable elements in IterableS…
Browse files Browse the repository at this point in the history
…ubject.isNoneOf() and isNotIn(). They are almost certainly not what people want.

RELNOTES=throw UOE from IterableSubject.isNoneOf() and isNotIn()

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=191919969
  • Loading branch information
kluever authored and ronshapiro committed Apr 11, 2018
1 parent 4330ec6 commit 46c8d3d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
30 changes: 30 additions & 0 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,36 @@ private void pairwiseCheck(String verb, PairwiseChecker checker) {
}
}

/** @deprecated You probably meant to call {@link #containsNoneOf} instead. */
@Override
@Deprecated
public void isNoneOf(@Nullable Object first, @Nullable Object second, @Nullable Object... rest) {
super.isNoneOf(first, second, rest);
}

/** @deprecated You probably meant to call {@link #containsNoneIn} instead. */
@Override
@Deprecated
public void isNotIn(Iterable<?> iterable) {
if (Iterables.contains(iterable, actual())) {
failWithFact("expected not to be any of", iterable);
}
List<Object> nonIterables = new ArrayList<>();
for (Object element : iterable) {
if (!(element instanceof Iterable<?>)) {
nonIterables.add(element);
}
}
if (!nonIterables.isEmpty()) {
failWithRawMessage(
"The actual value is an Iterable, and you've written a test that compares it to some "
+ "objects that are not Iterables. Did you instead mean to check whether its "
+ "*contents* match any of the *contents* of the given values? If so, call "
+ "containsNoneOf(...)/containsNoneIn(...) instead. Non-iterables: %s",
nonIterables);
}
}

/**
* Starts a method chain for a check in which the actual elements (i.e. the elements of the {@link
* Iterable} under test) are compared to expected elements using the given {@link Correspondence}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,11 +1146,17 @@ public void isIn() {
@Test
public void isNotIn() {
ImmutableList<String> actual = ImmutableList.of("a");
ImmutableList<String> expectedB = ImmutableList.of("b");
ImmutableList<String> expectedC = ImmutableList.of("c");
ImmutableList<ImmutableList<String>> expected = ImmutableList.of(expectedB, expectedC);

assertThat(actual).isNotIn(expected);
assertThat(actual).isNotIn(ImmutableList.of(ImmutableList.of("b"), ImmutableList.of("c")));

expectFailureWhenTestingThat(actual).isNotIn(ImmutableList.of("a", "b"));
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"The actual value is an Iterable, and you've written a test that compares it to some "
+ "objects that are not Iterables. Did you instead mean to check whether its "
+ "*contents* match any of the *contents* of the given values? If so, call "
+ "containsNoneOf(...)/containsNoneIn(...) instead. Non-iterables: [a, b]");
}

@Test
Expand All @@ -1163,12 +1169,20 @@ public void isAnyOf() {
}

@Test
@SuppressWarnings("IncompatibleArgumentType")
public void isNoneOf() {
ImmutableList<String> actual = ImmutableList.of("a");
ImmutableList<String> expectedB = ImmutableList.of("b");
ImmutableList<String> expectedC = ImmutableList.of("c");

assertThat(actual).isNoneOf(expectedB, expectedC);
assertThat(actual).isNoneOf(ImmutableList.of("b"), ImmutableList.of("c"));

expectFailureWhenTestingThat(actual).isNoneOf("a", "b");
assertThat(expectFailure.getFailure())
.hasMessageThat()
.isEqualTo(
"The actual value is an Iterable, and you've written a test that compares it to some "
+ "objects that are not Iterables. Did you instead mean to check whether its "
+ "*contents* match any of the *contents* of the given values? If so, call "
+ "containsNoneOf(...)/containsNoneIn(...) instead. Non-iterables: [a, b]");
}

private IterableSubject expectFailureWhenTestingThat(Iterable<?> actual) {
Expand Down
6 changes: 4 additions & 2 deletions core/src/test/java/com/google/common/truth/SubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ public void allAssertThatOverloadsAcceptNull() throws Exception {
subject.isSameAs(null);
subject.isNotSameAs(new Object());

subject.isNotIn(ImmutableList.<Object>of());
subject.isNoneOf(new Object(), new Object());
if (!(subject instanceof IterableSubject)) { // b/36000148
subject.isNotIn(ImmutableList.<Object>of());
subject.isNoneOf(new Object(), new Object());
}

try {
subject.isIn(ImmutableList.of());
Expand Down

0 comments on commit 46c8d3d

Please sign in to comment.