Skip to content

Commit

Permalink
Show arrays/iterables diff when containsExactly fails because of diff…
Browse files Browse the repository at this point in the history
…erent sizes

Fixes #1136
  • Loading branch information
joel-costigliola committed Dec 31, 2017
1 parent 6983158 commit 2b845e8
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 533 deletions.
89 changes: 20 additions & 69 deletions src/main/java/org/assertj/core/error/ShouldContainExactly.java
Expand Up @@ -26,21 +26,6 @@
*/
public class ShouldContainExactly extends BasicErrorMessageFactory {

/**
* Creates a new <code>{@link ShouldContainExactly}</code>.
*
* @param actual the actual value in the failed assertion.
* @param expected values expected to be contained in {@code actual}.
* @param notFound values in {@code expected} not found in {@code actual}.
* @param notExpected values in {@code actual} that were not in {@code expected}.
* @param comparisonStrategy the {@link ComparisonStrategy} used to evaluate assertion.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldContainExactly(Object actual, Object expected, Object notFound,
Object notExpected, ComparisonStrategy comparisonStrategy) {
return new ShouldContainExactly(actual, expected, notFound, notExpected, comparisonStrategy);
}

/**
* Creates a new <code>{@link ShouldContainExactly}</code>.
*
Expand All @@ -52,12 +37,15 @@ public static ErrorMessageFactory shouldContainExactly(Object actual, Object exp
* @return the created {@code ErrorMessageFactory}.
*
*/
public static ErrorMessageFactory shouldContainExactly(Object actual, Object expected, Object notFound,
Iterable<?> notExpected, ComparisonStrategy comparisonStrategy) {
public static ErrorMessageFactory shouldContainExactly(Object actual, Iterable<?> expected,
Iterable<?> notFound, Iterable<?> notExpected,
ComparisonStrategy comparisonStrategy) {
if (isNullOrEmpty(notExpected)) {
return new ShouldContainExactly(actual, expected, notFound, comparisonStrategy);
}

if (isNullOrEmpty(notFound)) {
return new ShouldContainExactly(actual, expected, comparisonStrategy, notExpected);
}
return new ShouldContainExactly(actual, expected, notFound, notExpected, comparisonStrategy);
}

Expand All @@ -70,60 +58,11 @@ public static ErrorMessageFactory shouldContainExactly(Object actual, Object exp
* @param notExpected values in {@code actual} that were not in {@code expected}.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldContainExactly(Object actual, Object expected, Object notFound,
Object notExpected) {
return new ShouldContainExactly(actual, expected, notFound, notExpected, StandardComparisonStrategy.instance());
}

/**
* Creates a new <code>{@link ShouldContainExactly}</code>.
*
* @param actual the actual value in the failed assertion.
* @param expected values expected to be contained in {@code actual}.
* @param notFound values in {@code expected} not found in {@code actual}.
* @param notExpected values in {@code actual} that were not in {@code expected}.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldContainExactly(Object actual, Object expected, Object notFound,
Iterable<?> notExpected) {

public static ErrorMessageFactory shouldContainExactly(Object actual, Iterable<?> expected,
Iterable<?> notFound, Iterable<?> notExpected) {
return shouldContainExactly(actual, expected, notFound, notExpected, StandardComparisonStrategy.instance());
}

public static ErrorMessageFactory shouldHaveSameSize(Object actual, Object expected, int actualSize,
int expectedSize, ComparisonStrategy comparisonStrategy) {
return StandardComparisonStrategy.instance().equals(comparisonStrategy) ?
new ShouldContainExactly(actual, expected, actualSize, expectedSize) :
new ShouldContainExactly(actual, expected, actualSize, expectedSize, comparisonStrategy);
}

private ShouldContainExactly(Object actual, Object expected, int actualSize, int expectedSize,
ComparisonStrategy comparisonStrategy) {
super("%n" +
"Actual and expected should have same size but actual size was:%n" +
" <%s>%n" +
"while expected size was:%n" +
" <%s>%n" +
"Actual was:%n" +
" <%s>%n" +
"Expected was:%n" +
" <%s>%n%s",
actualSize, expectedSize, actual, expected, comparisonStrategy);
}

private ShouldContainExactly(Object actual, Object expected, int actualSize, int expectedSize) {
super("%n" +
"Actual and expected should have same size but actual size was:%n" +
" <%s>%n" +
"while expected size was:%n" +
" <%s>%n" +
"Actual was:%n" +
" <%s>%n" +
"Expected was:%n" +
" <%s>%n",
actualSize, expectedSize, actual, expected);
}

private ShouldContainExactly(Object actual, Object expected, Object notFound, Object notExpected,
ComparisonStrategy comparisonStrategy) {
super("%n" +
Expand All @@ -149,6 +88,18 @@ private ShouldContainExactly(Object actual, Object expected, Object notFound, Co
actual, expected, notFound, comparisonStrategy);
}

private ShouldContainExactly(Object actual, Object expected, ComparisonStrategy comparisonStrategy,
Object unexpected) {
super("%n" +
"Expecting:%n" +
" <%s>%n" +
"to contain exactly (and in same order):%n" +
" <%s>%n" +
"but some elements were not expected:%n" +
" <%s>%n%s",
actual, expected, unexpected, comparisonStrategy);
}

/**
* Creates a new <code>{@link ShouldContainExactly}</code> for the case where actual and expected have the same
* elements in different order according to the given {@link ComparisonStrategy}.
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/assertj/core/internal/Arrays.java
Expand Up @@ -37,7 +37,6 @@
import static org.assertj.core.error.ShouldContainAtIndex.shouldContainAtIndex;
import static org.assertj.core.error.ShouldContainExactly.elementsDifferAtIndex;
import static org.assertj.core.error.ShouldContainExactly.shouldContainExactly;
import static org.assertj.core.error.ShouldContainExactly.shouldHaveSameSize;
import static org.assertj.core.error.ShouldContainExactlyInAnyOrder.shouldContainExactlyInAnyOrder;
import static org.assertj.core.error.ShouldContainNull.shouldContainNull;
import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;
Expand Down Expand Up @@ -241,10 +240,6 @@ void assertContainsExactly(AssertionInfo info, Failures failures, Object actual,
if (commonChecks(info, actual, values)) return;
assertIsArray(info, actual);
assertIsArray(info, values);
int actualSize = sizeOf(actual);
int expectedSize = sizeOf(values);
if (actualSize != expectedSize)
throw failures.failure(info, shouldHaveSameSize(actual, values, actualSize, expectedSize, comparisonStrategy));

List<Object> actualAsList = asList(actual);
IterableDiff diff = diff(actualAsList, asList(values), comparisonStrategy);
Expand All @@ -260,7 +255,8 @@ void assertContainsExactly(AssertionInfo info, Failures failures, Object actual,
return;
}
throw failures.failure(info,
shouldContainExactly(actual, values, diff.missing, diff.unexpected, comparisonStrategy));
shouldContainExactly(actual, asList(values), diff.missing, diff.unexpected,
comparisonStrategy));
}

void assertContainsExactlyInAnyOrder(AssertionInfo info, Failures failures, Object actual, Object values) {
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/assertj/core/internal/Iterables.java
Expand Up @@ -31,7 +31,6 @@
import static org.assertj.core.error.ShouldContainAnyOf.shouldContainAnyOf;
import static org.assertj.core.error.ShouldContainExactly.elementsDifferAtIndex;
import static org.assertj.core.error.ShouldContainExactly.shouldContainExactly;
import static org.assertj.core.error.ShouldContainExactly.shouldHaveSameSize;
import static org.assertj.core.error.ShouldContainExactlyInAnyOrder.shouldContainExactlyInAnyOrder;
import static org.assertj.core.error.ShouldContainNull.shouldContainNull;
import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;
Expand Down Expand Up @@ -954,10 +953,6 @@ public void assertContainsAll(AssertionInfo info, Iterable<?> actual, Iterable<?
public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object[] values) {
checkIsNotNull(values);
assertNotNull(info, actual);
int actualSize = sizeOf(actual);
if (values.length != actualSize)
throw failures.failure(info, shouldHaveSameSize(actual, values, actualSize, values.length, comparisonStrategy));
assertHasSameSizeAs(info, actual, values); // include check that actual is not null

List<Object> actualAsList = newArrayList(actual);
IterableDiff diff = diff(actualAsList, asList(values), comparisonStrategy);
Expand All @@ -973,7 +968,8 @@ public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object
return;
}
throw failures.failure(info,
shouldContainExactly(actual, values, diff.missing, diff.unexpected, comparisonStrategy));
shouldContainExactly(actual, asList(values), diff.missing, diff.unexpected,
comparisonStrategy));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/assertj/core/internal/Maps.java
Expand Up @@ -34,6 +34,7 @@
import static org.assertj.core.internal.Arrays.assertIsArray;
import static org.assertj.core.internal.CommonValidations.checkSizes;
import static org.assertj.core.internal.CommonValidations.hasSameSizeAsCheck;
import static org.assertj.core.util.Arrays.asList;
import static org.assertj.core.util.Objects.areEqual;
import static org.assertj.core.util.Preconditions.checkArgument;
import static org.assertj.core.util.Preconditions.checkNotNull;
Expand Down Expand Up @@ -563,7 +564,7 @@ public <K, V> void assertContainsExactly(AssertionInfo info, Map<K, V> actual,
return;
}

throw failures.failure(info, shouldContainExactly(actual, entries, notFound, notExpected));
throw failures.failure(info, shouldContainExactly(actual, asList(entries), notFound, notExpected));
}

private <K, V> void compareActualMapAndExpectedKeys(Map<K, V> actual, K[] keys, Set<K> notExpected, Set<K> notFound) {
Expand Down

0 comments on commit 2b845e8

Please sign in to comment.