From 63c9f9225318e4c146ab9b82511acd875cb32481 Mon Sep 17 00:00:00 2001 From: peteg Date: Mon, 10 Oct 2016 08:09:48 -0700 Subject: [PATCH] Stop using ImmutableList for storing elements in the implementation of Fuzzy Truth, because we want to support nulls. (Unfortunately, the previous test case only went through the most trivial code path and didn't trigger the bug.) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=135676702 --- .../google/common/truth/IterableSubject.java | 18 +++++++++--------- .../common/truth/IterableSubjectTest.java | 13 +++++-------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/google/common/truth/IterableSubject.java b/core/src/main/java/com/google/common/truth/IterableSubject.java index 6fb291249..352fecee0 100644 --- a/core/src/main/java/com/google/common/truth/IterableSubject.java +++ b/core/src/main/java/com/google/common/truth/IterableSubject.java @@ -619,8 +619,8 @@ public Ordered containsExactlyElementsIn(Iterable expected) { // We know they don't correspond in order, so we're going to have to do an any-order test. // Find a many:many mapping between the indexes of the elements which correspond, and check // it for completeness. - ImmutableList actualList = ImmutableList.copyOf(getCastSubject()); - ImmutableList expectedList = ImmutableList.copyOf(expected); + List actualList = Lists.newArrayList(getCastSubject()); + List expectedList = Lists.newArrayList(expected); ImmutableSetMultimap candidateMapping = findCandidateMapping(actualList, expectedList); failIfCandidateMappingHasMissingOrExtra(actualList, expectedList, candidateMapping); @@ -681,8 +681,8 @@ void failIfCandidateMappingHasMissingOrExtra( List actual, List expected, ImmutableMultimap mapping) { - ImmutableList extra = findNotIndexed(actual, mapping.keySet()); - ImmutableList missing = findNotIndexed(expected, mapping.inverse().keySet()); + List extra = findNotIndexed(actual, mapping.keySet()); + List missing = findNotIndexed(expected, mapping.inverse().keySet()); Optional missingOrExtraMessage = describeMissingOrExtra(extra, missing); if (missingOrExtraMessage.isPresent()) { failWithRawMessage( @@ -721,19 +721,19 @@ private Optional describeMissingOrExtra( * Returns all the elements of the given list other than those with the given indexes. Assumes * that all the given indexes really are valid indexes into the list. */ - private ImmutableList findNotIndexed(List list, Set indexes) { + private List findNotIndexed(List list, Set indexes) { if (indexes.size() == list.size()) { // If there are as many distinct valid indexes are there are elements in the list then every // index must be in there once. return ImmutableList.of(); } - ImmutableList.Builder notIndexed = ImmutableList.builder(); + List notIndexed = Lists.newArrayList(); for (int index = 0; index < list.size(); index++) { if (!indexes.contains(index)) { notIndexed.add(list.get(index)); } } - return notIndexed.build(); + return notIndexed; } /** @@ -786,8 +786,8 @@ void failIfOneToOneMappingHasMissingOrExtra( List actual, List expected, BiMap mapping) { - ImmutableList extra = findNotIndexed(actual, mapping.keySet()); - ImmutableList missing = findNotIndexed(expected, mapping.inverse().keySet()); + List extra = findNotIndexed(actual, mapping.keySet()); + List missing = findNotIndexed(expected, mapping.inverse().keySet()); Optional missingOrExtraMessage = describeMissingOrExtra(extra, missing); if (missingOrExtraMessage.isPresent()) { failWithRawMessage( diff --git a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java index 37c57a363..9b373e990 100644 --- a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java @@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; @@ -1258,12 +1257,11 @@ public void comparingElementsUsing_containsExactlyElementsIn_inOrder_failsOutOfO @Test public void comparingElementsUsing_containsExactlyElementsIn_null() { - List expected = Arrays.asList((Integer) null); - List actual = Arrays.asList((String) null); + List expected = Arrays.asList(128, null); + List actual = Arrays.asList(null, "0x80"); assertThat(actual) .comparingElementsUsing(STRING_PARSES_TO_INTEGER_CORRESPONDENCE) - .containsExactlyElementsIn(expected) - .inOrder(); + .containsExactlyElementsIn(expected); } @Test @@ -1309,11 +1307,10 @@ public void comparingElementsUsing_containsExactly_failsMissingAndExtraInOneToOn @Test public void comparingElementsUsing_containsExactly_nullValueInArray() { - List actual = Arrays.asList((String) null); + List actual = Arrays.asList(null, "0x80"); assertThat(actual) .comparingElementsUsing(STRING_PARSES_TO_INTEGER_CORRESPONDENCE) - .containsExactly((Integer) null) - .inOrder(); + .containsExactly(128, null); } @Test