Skip to content

Commit

Permalink
Add DescribedPredicate.allElements to be consistent with the existing…
Browse files Browse the repository at this point in the history
… DescribedPredicate.anyElementThat

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Jul 29, 2019
1 parent 01f3379 commit b8890f4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static <T> DescribedPredicate<Iterable<T>> anyElementThat(final Described
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;

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.$$;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b8890f4

Please sign in to comment.