-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding allOf validation and test cases #438
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import static am.ik.yavi.core.NullAs.VALID; | ||
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_CONTAINS; | ||
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_UNIQUE; | ||
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_ALL_OF; | ||
|
||
public class CollectionConstraint<T, L extends Collection<E>, E> | ||
extends ContainerConstraintBase<T, L, CollectionConstraint<T, L, E>> { | ||
|
@@ -69,6 +70,22 @@ public CollectionConstraint<T, L, E> unique() { | |
return this; | ||
} | ||
|
||
public CollectionConstraint<T, L, E> allOf(Collection<? extends E> values) { | ||
this.predicates().add(ConstraintPredicate.withViolatedValue(collection -> { | ||
final Set<E> missingValues = new HashSet<>(values); | ||
|
||
if (collection instanceof Set) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use brackets |
||
missingValues.removeAll(collection); | ||
else | ||
missingValues.removeAll(new HashSet<>(collection)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to wrap it in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It becomes efficient. If you check removeAll implementation in public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
if (size() > c.size()) {
for (Object e : c)
modified |= remove(e);
} else {
for (Iterator<?> i = iterator(); i.hasNext(); ) {
if (c.contains(i.next())) {
i.remove();
modified = true;
}
}
}
return modified;
} |
||
|
||
return missingValues.isEmpty() ? Optional.empty() : Optional.of(new ViolatedValue(missingValues)); | ||
}, OBJECT_ALL_OF, () -> new Object[]{values.toString()}, VALID)); | ||
|
||
return this; | ||
} | ||
|
||
|
||
@Override | ||
protected ToIntFunction<L> size() { | ||
return Collection::size; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ enum Default implements ViolationMessage { | |
OBJECT_EQUAL_TO("object.equalTo", "\"{0}\" must be equal to {1}"), // | ||
OBJECT_ONE_OF("object.oneOf", "\"{0}\" must be one of the following values: {1}"), // | ||
OBJECT_NOT_ONE_OF("object.notOneOf", "\"{0}\" must not be one of the following values: {1}"), // | ||
OBJECT_ALL_OF("object.allOf", "\"{0}\" must be have of all of the following values: {1}, missing values: {2}"), // | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
CONTAINER_NOT_EMPTY("container.notEmpty", "\"{0}\" must not be empty"), // | ||
CONTAINER_LESS_THAN("container.lessThan", "The size of \"{0}\" must be less than {1}. The given size is {2}"), // | ||
CONTAINER_LESS_THAN_OR_EQUAL("container.lessThanOrEqual", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allOf
sounds misleading. It seems thatcontains
is more appropriate.AssertJ's Collection assert method
https://assertj.github.io/doc/#assertj-core-group-contains
contains
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@making using
contains
can be a misnomer. It does not explain whether any or all items need to be contained. How aboutcontainsAll
?