Refactored ContainsItemsMatcher to allow different expected and actual types.
This commit is contained in:
parent
73e5f95e92
commit
1804b1556f
@ -4,14 +4,14 @@ import app.mealsmadeeasy.api.matchers.ContainsItemsMatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class ContainsImagesMatcher extends ContainsItemsMatcher<Image, Long> {
|
||||
public final class ContainsImagesMatcher extends ContainsItemsMatcher<Image, Image, Long> {
|
||||
|
||||
public static ContainsImagesMatcher containsImages(Image... expected) {
|
||||
return new ContainsImagesMatcher(expected);
|
||||
}
|
||||
|
||||
private ContainsImagesMatcher(Image... allExpected) {
|
||||
super(List.of(allExpected), o -> o instanceof Image, Image::getId);
|
||||
super(List.of(allExpected), o -> o instanceof Image, Image::getId, Image::getId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,31 +9,35 @@ import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class ContainsItemsMatcher<T, ID> extends BaseMatcher<Iterable<T>> {
|
||||
public class ContainsItemsMatcher<T, E, ID> extends BaseMatcher<Iterable<T>> {
|
||||
|
||||
private final List<T> allExpected;
|
||||
private final List<E> allExpected;
|
||||
private final Predicate<Object> isT;
|
||||
private final Function<T, ID> idFunction;
|
||||
private final Function<T, ID> givenToIdFunction;
|
||||
private final Function<E, ID> expectedToIdFunction;
|
||||
private final BiPredicate<ID, ID> equalsFunction;
|
||||
|
||||
public ContainsItemsMatcher(
|
||||
List<T> allExpected,
|
||||
List<E> allExpected,
|
||||
Predicate<Object> isT,
|
||||
Function<T, ID> idFunction,
|
||||
Function<T, ID> givenToIdFunction,
|
||||
Function<E, ID> expectedToIdFunction,
|
||||
BiPredicate<ID, ID> equalsFunction
|
||||
) {
|
||||
this.allExpected = allExpected;
|
||||
this.isT = isT;
|
||||
this.idFunction = idFunction;
|
||||
this.givenToIdFunction = givenToIdFunction;
|
||||
this.expectedToIdFunction = expectedToIdFunction;
|
||||
this.equalsFunction = equalsFunction;
|
||||
}
|
||||
|
||||
public ContainsItemsMatcher(
|
||||
List<T> allExpected,
|
||||
List<E> allExpected,
|
||||
Predicate<Object> isT,
|
||||
Function<T, ID> idFunction
|
||||
Function<T, ID> givenToIdFunction,
|
||||
Function<E, ID> expectedToIdFunction
|
||||
) {
|
||||
this(allExpected, isT, idFunction, Objects::equals);
|
||||
this(allExpected, isT, givenToIdFunction, expectedToIdFunction, Objects::equals);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -41,12 +45,12 @@ public class ContainsItemsMatcher<T, ID> extends BaseMatcher<Iterable<T>> {
|
||||
public boolean matches(Object o) {
|
||||
if (o instanceof Iterable<?> iterable) {
|
||||
checkExpected:
|
||||
for (final T expected : this.allExpected) {
|
||||
for (final E expected : this.allExpected) {
|
||||
for (final Object item : iterable) {
|
||||
if (
|
||||
this.isT.test(item) && this.equalsFunction.test(
|
||||
this.idFunction.apply((T) item),
|
||||
this.idFunction.apply(expected)
|
||||
this.givenToIdFunction.apply((T) item),
|
||||
this.expectedToIdFunction.apply(expected)
|
||||
)
|
||||
) {
|
||||
continue checkExpected;
|
||||
|
@ -0,0 +1,23 @@
|
||||
package app.mealsmadeeasy.api.recipe;
|
||||
|
||||
import app.mealsmadeeasy.api.matchers.ContainsItemsMatcher;
|
||||
import app.mealsmadeeasy.api.recipe.view.RecipeInfoView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ContainsRecipeInfoViewsForRecipesMatcher extends ContainsItemsMatcher<RecipeInfoView, Recipe, Long> {
|
||||
|
||||
public static ContainsRecipeInfoViewsForRecipesMatcher containsRecipeInfoViewsForRecipes(Recipe... expected) {
|
||||
return new ContainsRecipeInfoViewsForRecipesMatcher(List.of(expected));
|
||||
}
|
||||
|
||||
private ContainsRecipeInfoViewsForRecipesMatcher(List<Recipe> expected) {
|
||||
super(
|
||||
expected,
|
||||
o -> o instanceof RecipeInfoView,
|
||||
RecipeInfoView::getId,
|
||||
Recipe::getId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -8,7 +8,7 @@ import app.mealsmadeeasy.api.recipe.star.RecipeStarId;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ContainsRecipeStarsMatcher extends ContainsItemsMatcher<RecipeStar, RecipeStarId> {
|
||||
public class ContainsRecipeStarsMatcher extends ContainsItemsMatcher<RecipeStar, RecipeStar, RecipeStarId> {
|
||||
|
||||
public static ContainsRecipeStarsMatcher containsStars(RecipeStar... expected) {
|
||||
return new ContainsRecipeStarsMatcher(expected);
|
||||
@ -19,6 +19,7 @@ public class ContainsRecipeStarsMatcher extends ContainsItemsMatcher<RecipeStar,
|
||||
List.of(allExpected),
|
||||
o -> o instanceof RecipeStar,
|
||||
recipeStar -> ((RecipeStarEntity) recipeStar).getId(),
|
||||
recipeStar -> ((RecipeStarEntity) recipeStar).getId(),
|
||||
(id0, id1) -> Objects.equals(id0.getRecipeId(), id1.getRecipeId())
|
||||
&& Objects.equals(id0.getOwnerUsername(), id1.getOwnerUsername())
|
||||
);
|
||||
|
@ -4,14 +4,14 @@ import app.mealsmadeeasy.api.matchers.ContainsItemsMatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class ContainsRecipesMatcher extends ContainsItemsMatcher<Recipe, Long> {
|
||||
public final class ContainsRecipesMatcher extends ContainsItemsMatcher<Recipe, Recipe, Long> {
|
||||
|
||||
public static ContainsRecipesMatcher containsRecipes(Recipe... expected) {
|
||||
return new ContainsRecipesMatcher(expected);
|
||||
}
|
||||
|
||||
private ContainsRecipesMatcher(Recipe[] allExpected) {
|
||||
super(List.of(allExpected), o -> o instanceof Recipe, Recipe::getId);
|
||||
super(List.of(allExpected), o -> o instanceof Recipe, Recipe::getId, Recipe::getId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,14 +4,14 @@ import app.mealsmadeeasy.api.matchers.ContainsItemsMatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ContainsUsersMatcher extends ContainsItemsMatcher<User, Long> {
|
||||
public class ContainsUsersMatcher extends ContainsItemsMatcher<User, User, Long> {
|
||||
|
||||
public static ContainsUsersMatcher containsUsers(User... allExpected) {
|
||||
return new ContainsUsersMatcher(allExpected);
|
||||
}
|
||||
|
||||
private ContainsUsersMatcher(User... allExpected) {
|
||||
super(List.of(allExpected), o -> o instanceof User, User::getId);
|
||||
super(List.of(allExpected), o -> o instanceof User, User::getId, User::getId);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user