2018-09-30 21:57:27 +00:00
|
|
|
package test.utils;
|
|
|
|
|
2018-10-04 20:58:04 +00:00
|
|
|
import com.google.common.collect.Iterables;
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
import java.lang.Class;
|
|
|
|
import java.util.ArrayList;
|
2018-09-30 21:57:27 +00:00
|
|
|
import java.util.HashSet;
|
2018-10-04 20:58:04 +00:00
|
|
|
import java.util.List;
|
2018-09-30 21:57:27 +00:00
|
|
|
import java.util.Set;
|
2018-10-04 20:58:04 +00:00
|
|
|
|
|
|
|
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
2018-09-30 21:57:27 +00:00
|
|
|
|
|
|
|
public class AssertExtensions {
|
2018-10-04 20:58:04 +00:00
|
|
|
|
|
|
|
public static <T> void assertItemsEqual(Iterable<T> expected, Iterable<T> actual, EqualityComparer<T> comparer) {
|
|
|
|
assertItemsEqual(expected, actual, comparer, (String)null);
|
|
|
|
}
|
2018-09-30 21:57:27 +00:00
|
|
|
|
2018-10-04 20:58:04 +00:00
|
|
|
public static <T> void assertItemsEqual(Iterable<T> expected, Iterable<T> actual, EqualityComparer<T> comparer, String message) {
|
|
|
|
List<EquatableWrapper<T>> expectedSet = new ArrayList<EquatableWrapper<T>>();
|
2018-09-30 21:57:27 +00:00
|
|
|
for(T item: expected)
|
|
|
|
expectedSet.add(new EquatableWrapper<T>(item, comparer));
|
|
|
|
|
2018-10-04 20:58:04 +00:00
|
|
|
List<EquatableWrapper<T>> actualSet = new ArrayList<EquatableWrapper<T>>();
|
2018-09-30 21:57:27 +00:00
|
|
|
for(T item: actual)
|
|
|
|
actualSet.add(new EquatableWrapper<T>(item, comparer));
|
|
|
|
|
2018-10-04 20:58:04 +00:00
|
|
|
assertItemsEqual(expectedSet, actualSet, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> void assertItemsEqual(Iterable<T> expected, Iterable<T> actual) {
|
|
|
|
assertItemsEqual(expected, actual, (String)null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> void assertItemsEqual(Iterable<T> expected, Iterable<T> actual, String message) {
|
|
|
|
List<T> list = new ArrayList<T>();
|
|
|
|
T[] expectedArray = getArray(expected);
|
|
|
|
assertThat(message, actual, containsInAnyOrder(expectedArray));
|
2018-09-30 21:57:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 20:58:04 +00:00
|
|
|
private static <T> T[] getArray(Iterable<T> iterable) {
|
|
|
|
// XXX: What a horrific way to create an array from an iterable.
|
|
|
|
// Isn't there a better solution?
|
|
|
|
List<T> list = new ArrayList<T>();
|
|
|
|
for(T item : iterable)
|
|
|
|
list.add(item);
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
T[] result = (T[])new Object[list.size()];
|
|
|
|
for(int i = 0; i < list.size(); i++)
|
|
|
|
result[i] = list.get(i);
|
|
|
|
return result;
|
|
|
|
}
|
2018-09-30 21:57:27 +00:00
|
|
|
}
|