Skip to content

Commit

Permalink
refactor: improve test code
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasfara committed Nov 4, 2023
1 parent eef8ea6 commit 2baabe8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class TestIterableWithPolicy {

private TestIterableWithPolicy() {}

private static <T> IterableWithPolicy<T> getIterableWithPolicy(T[] elements, Predicate<T> filter) {
return new IterableWithPolicyImpl<>(elements, filter);
}

private static <T> IterableWithPolicy<T> getIterableWithPolicy(T[] elements) {
return new IterableWithPolicyImpl<>(elements);
}

public static void main(String[] args) {
Integer[] test1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Predicate<Integer> filterEven = new Predicate<Integer>() {
Expand All @@ -28,8 +36,8 @@ public boolean test(Integer elem) {
}
};

IterableWithPolicy<Integer> evenIterable = new IterableWithPolicyImpl<>(test1, filterEven);
IterableWithPolicy<Integer> oddIterable = new IterableWithPolicyImpl<>(test1, filterOdd);
IterableWithPolicy<Integer> evenIterable = getIterableWithPolicy(test1, filterEven);
IterableWithPolicy<Integer> oddIterable = getIterableWithPolicy(test1, filterOdd);

assertContentEqualsInOrder(evenIterable, Arrays.asList(2, 4, 6, 8));
assertContentEqualsInOrder(oddIterable, Arrays.asList(1, 3, 5, 7, 9));
Expand All @@ -45,13 +53,13 @@ public boolean test(Integer elem) {
}
};

IterableWithPolicy<Integer> emptyIterable = new IterableWithPolicyImpl<>(test1, filterOutAll);
IterableWithPolicy<Integer> allIterable = new IterableWithPolicyImpl<>(test1, takeAll);
IterableWithPolicy<Integer> emptyIterable = getIterableWithPolicy(test1, filterOutAll);
IterableWithPolicy<Integer> allIterable = getIterableWithPolicy(test1, takeAll);

assertContentEqualsInOrder(emptyIterable, new ArrayList<>());
assertContentEqualsInOrder(allIterable, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));

IterableWithPolicy<Integer> switchPolicy = new IterableWithPolicyImpl<>(test1);
IterableWithPolicy<Integer> switchPolicy = getIterableWithPolicy(test1);

// By default, if no Predicate is given, the iterator should return all the elements
assertContentEqualsInOrder(switchPolicy, Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
Expand Down Expand Up @@ -87,13 +95,13 @@ public boolean test(Product elem) {
}
};

IterableWithPolicy<Product> availableProducts = new IterableWithPolicyImpl<>(productsTest, filterAllAvailable);
IterableWithPolicy<Product> availableProducts = getIterableWithPolicy(productsTest, filterAllAvailable);
assertContentEqualsInOrder(availableProducts, Arrays.asList(prod2, prod3, prod4, prod5));

IterableWithPolicy<Product> expensiveProducts = new IterableWithPolicyImpl<>(productsTest, filterGraterThanFifty);
IterableWithPolicy<Product> expensiveProducts = getIterableWithPolicy(productsTest, filterGraterThanFifty);
assertContentEqualsInOrder(expensiveProducts, Arrays.asList(prod2, prod5));

IterableWithPolicy<Product> onlyProductOne = new IterableWithPolicyImpl<>(productsTest, takeOnlyProductOne);
IterableWithPolicy<Product> onlyProductOne = getIterableWithPolicy(productsTest, takeOnlyProductOne);
assertContentEqualsInOrder(onlyProductOne, Arrays.asList(prod1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,6 @@ public static void assertTrue(final boolean condition) {
}
}

/**
* Exits with an error if the two collections do not contain the same elements (except for the order).
*
* @param expected the expected collection
* @param actual the actual collection
*/
public static void assertContentEqualsInAnyOrder(final Collection<?> expected, final Collection<?> actual) {
if (checkContentEqualsInAnyOrder(expected, actual)) {
confirmOK(expected, actual);
} else {
onNotEquals(expected, actual);
}
}

private static boolean checkContentEqualsInAnyOrder(final Collection<?> expected, final Collection<?> actual) {
Objects.requireNonNull(expected);
if (actual == null || expected.size() != actual.size()) {
return false;
}
final Collection<?> expectedCopy = new ArrayList<>(expected);
for (final var elem : actual) {
if (!expectedCopy.remove(elem)) {
return false;
}
}
return expectedCopy.isEmpty();
}

/**
* Exits with an error if the two collections do not contain the same elements (except for the order).
*
Expand Down

0 comments on commit 2baabe8

Please sign in to comment.