Utility matchers for testing.
This commit is contained in:
parent
6dd0d1483e
commit
0f8012134e
@ -0,0 +1,43 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.Recipe;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ContainsRecipesMatcher extends BaseMatcher<List<Recipe>> {
|
||||
|
||||
private final Recipe[] allExpected;
|
||||
|
||||
public ContainsRecipesMatcher(Recipe[] allExpected) {
|
||||
this.allExpected = allExpected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object actual) {
|
||||
if (actual instanceof List<?> list) {
|
||||
checkExpected:
|
||||
for (final Recipe expected : allExpected) {
|
||||
for (final Object item : list) {
|
||||
if (item instanceof Recipe o && Objects.equals(o.getId(), expected.getId())) {
|
||||
continue checkExpected;
|
||||
}
|
||||
}
|
||||
// Did not find the expected in the list
|
||||
return false;
|
||||
}
|
||||
// Found all expected in list
|
||||
return true;
|
||||
}
|
||||
// actual is not a List
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Expected ").appendValue(List.of(this.allExpected));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class IsUserMatcher extends BaseMatcher<User> {
|
||||
|
||||
private final User expected;
|
||||
|
||||
public IsUserMatcher(User expected) {
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object actual) {
|
||||
return actual instanceof User o && Objects.equals(o.getId(), this.expected.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Expected ").appendValue(this.expected);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package app.mealsmadeeasy.api.matchers;
|
||||
|
||||
import app.mealsmadeeasy.api.recipe.Recipe;
|
||||
import app.mealsmadeeasy.api.user.User;
|
||||
|
||||
public final class Matchers {
|
||||
|
||||
public static ContainsRecipesMatcher containsRecipes(Recipe... expected) {
|
||||
return new ContainsRecipesMatcher(expected);
|
||||
}
|
||||
|
||||
public static IsUserMatcher isUser(User expected) {
|
||||
return new IsUserMatcher(expected);
|
||||
}
|
||||
|
||||
private Matchers() {}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user