Skip to content
Permalink
Browse files

Add DescribedPredicate.allElements to be consistent with the existing…

… DescribedPredicate.anyElementThat

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information...
codecholeric committed Jul 28, 2019
1 parent 01f3379 commit b8890f447c3621cb16cb21e636b2a5397722c9ec
@@ -148,6 +148,10 @@ public boolean apply(Object input) {
return new AnyElementPredicate<>(predicate);
}

public static <T> DescribedPredicate<Iterable<T>> allElements(final DescribedPredicate<T> predicate) {
return new AllElementsPredicate<>(predicate);
}

private static class AsPredicate<T> extends DescribedPredicate<T> {
private final DescribedPredicate<T> current;

@@ -326,4 +330,23 @@ public boolean apply(Iterable<T> iterable) {
return false;
}
}

private static class AllElementsPredicate<T> extends DescribedPredicate<Iterable<T>> {
private final DescribedPredicate<T> predicate;

AllElementsPredicate(DescribedPredicate<T> predicate) {
super("all elements " + predicate.getDescription());
this.predicate = predicate;
}

@Override
public boolean apply(Iterable<T> iterable) {
for (T javaClass : iterable) {
if (!predicate.apply(javaClass)) {
return false;
}
}
return true;
}
}
}
@@ -1,13 +1,17 @@
package com.tngtech.archunit.base;

import com.google.common.collect.ImmutableList;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.tngtech.archunit.base.DescribedPredicate.allElements;
import static com.tngtech.archunit.base.DescribedPredicate.alwaysFalse;
import static com.tngtech.archunit.base.DescribedPredicate.alwaysTrue;
import static com.tngtech.archunit.base.DescribedPredicate.anyElementThat;
import static com.tngtech.archunit.base.DescribedPredicate.describe;
import static com.tngtech.archunit.base.DescribedPredicate.doNot;
import static com.tngtech.archunit.base.DescribedPredicate.doesNot;
import static com.tngtech.archunit.base.DescribedPredicate.equalTo;
@@ -16,7 +20,6 @@
import static com.tngtech.archunit.base.DescribedPredicate.lessThan;
import static com.tngtech.archunit.base.DescribedPredicate.lessThanOrEqualTo;
import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.base.DescribedPredicate.describe;
import static com.tngtech.archunit.testutil.Assertions.assertThat;
import static com.tngtech.java.junit.dataprovider.DataProviders.$;
import static com.tngtech.java.junit.dataprovider.DataProviders.$$;
@@ -178,6 +181,28 @@ public void onResultOf_works() {
assertThat(equalTo(5).onResultOf(constant(6))).rejects(new Object());
}

@Test
public void anyElementThat_works() {
assertThat(anyElementThat(equalTo(5)))
.hasDescription("any element that equal to '5'")
.accepts(ImmutableList.of(5))
.accepts(ImmutableList.of(-1, 0, 5, 6))
.accepts(ImmutableList.of(-1, 0, 5, 5, 6))
.rejects(ImmutableList.of(-1, 0, 6))
.rejects(ImmutableList.<Integer>of());
}

@Test
public void allElements_works() {
assertThat(allElements(equalTo(5)))
.hasDescription("all elements equal to '5'")
.accepts(ImmutableList.of(5))
.accepts(ImmutableList.of(5, 5, 5))
.rejects(ImmutableList.of(5, 5, 6))
.rejects(ImmutableList.of(-1, 0, 5, 6))
.accepts(ImmutableList.<Integer>of());
}

private Function<Object, Integer> constant(final int integer) {
return new Function<Object, Integer>() {
@Override

0 comments on commit b8890f4

Please sign in to comment.
You can’t perform that action at this time.