Skip to content

Commit

Permalink
[tests] assertContains provides diff between actual and expected values.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 2, 2020
1 parent 75629d7 commit 2bd7e71
Showing 1 changed file with 28 additions and 4 deletions.
Expand Up @@ -171,12 +171,36 @@ public static void assertContainsCollection(Iterable<?> actual, Iterable<?> expe
}

if (!unexpectedElements.isEmpty()) {
fail("Unexpected elements:\n" + unexpectedElements.toString() + "\nActual elements are:\n" +
Iterables.toString(actual) + "\nExpected elements are:\n" + Iterables.toString(expected));
throw new AssertionFailedError(
"Unexpected elements:\n" + unexpectedElements.toString(),
toString(expected),
toString(actual));
} else if (!le.isEmpty()) {
fail("Expecting the following elements:\n" + le.toString() + "\nActual elements are:\n" +
Iterables.toString(actual) + "\nExpected elements are:\n" + Iterables.toString(expected));
throw new AssertionFailedError("Expecting the following elements:\n" + le.toString(),
toString(expected),
toString(actual));
}
}

private static String toString(Iterable<?> iterable) {
final StringBuilder buf = new StringBuilder();
if (iterable != null) {
final List<String> elements = new ArrayList<>();
for (final Object obj : iterable) {
if (obj == null) {
elements.add("null");
} else {
elements.add("<" + obj.toString() + ">");
}
}
final String[] tab = new String[elements.size()];
elements.toArray(tab);
Arrays.sort(tab);
for (final String obj : tab) {
buf.append(obj).append("\n");
}
}
return buf.toString();
}

/** Test if the actual collection/iterable contains at least all the expected objects.
Expand Down

0 comments on commit 2bd7e71

Please sign in to comment.