Skip to content

Commit

Permalink
Rename Filter.filter() to Filter.apply()
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 28, 2016
1 parent 5f6f33a commit d756837
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 57 deletions.
Expand Up @@ -40,10 +40,10 @@ static <T> Filter<T> alwaysIncluded() {
}

@Override
public FilterResult filter(T element) {
public FilterResult apply(T element) {
// @formatter:off
return filters.stream()
.map(filter -> filter.filter(element))
.map(filter -> filter.apply(element))
.filter(FilterResult::excluded)
.findFirst()
.orElse(FilterResult.included("Element was included by all filters."));
Expand Down
28 changes: 20 additions & 8 deletions junit-engine-api/src/main/java/org/junit/gen5/engine/Filter.java
Expand Up @@ -22,7 +22,11 @@
import org.junit.gen5.commons.util.Preconditions;

/**
* Filters particular tests during/after test discovery.
* A {@link Filter} can be applied to determine if an object should be
* <em>included</em> or <em>excluded</em> in a result set.
*
* <p>For example, tests may be filtered during or after test discovery
* based on certain criteria.
*
* <p>Clients should not implement this interface directly but rather one of
* its subinterfaces.
Expand All @@ -35,8 +39,8 @@
public interface Filter<T> {

/**
* Combines an array of {@link Filter filters} into a new composite filter
* that will include elements if and only if all of the filters in the
* Combine the supplied array of {@link Filter filters} into a new composite
* filter that will include elements if and only if all of the filters in the
* specified array include it.
*
* <p>If the array is empty, the returned filter will include all elements
Expand All @@ -62,9 +66,9 @@ static <T> Filter<T> composeFilters(Filter<T>... filters) {
}

/**
* Combines a collection of {@link Filter filters} into a new composite filter
* that will include elements if and only if all of the filters in the specified
* collection include it.
* Combine the supplied collection of {@link Filter filters} into a new
* composite filter that will include elements if and only if all of the
* filters in the specified collection include it.
*
* <p>If the collection is empty, the returned filter will include all
* elements it is asked to filter.
Expand All @@ -87,10 +91,18 @@ static <T> Filter<T> composeFilters(Collection<? extends Filter<T>> filters) {
return new CompositeFilter<>(filters);
}

FilterResult filter(T object);
/**
* Apply this filter to the supplied object.
*/
FilterResult apply(T object);

/**
* Return a {@link Predicate} that returns {@code true} if this filter
* <em>includes</em> the object supplied to the predicate's
* {@link Predicate#test test} method.
*/
default Predicate<T> toPredicate() {
return object -> filter(object).included();
return object -> apply(object).included();
}

}
Expand Up @@ -79,14 +79,14 @@ private FilterResult(boolean included, String reason) {
}

/**
* @return {@code true} if the filtered object should be included in the test plan
* @return {@code true} if the filtered object should be included
*/
public boolean included() {
return included;
}

/**
* @return {@code true} if the filtered object should be excluded from the test plan
* @return {@code true} if the filtered object should be excluded
*/
public boolean excluded() {
return !included();
Expand Down
Expand Up @@ -28,7 +28,7 @@ class ClassNameFilter implements ClassFilter {
}

@Override
public FilterResult filter(Class<?> testClass) {
public FilterResult apply(Class<?> testClass) {
return includedIf(pattern.matcher(testClass.getName()).matches(), //
() -> "Test class matches name pattern: " + pattern, //
() -> "Test class does not match name pattern: " + pattern);
Expand Down
Expand Up @@ -34,10 +34,10 @@ public ExclusionReasonConsumingFilter(Filter<T> filter, BiConsumer<T, Optional<S
}

@Override
public FilterResult filter(T object) {
FilterResult result = filter.filter(object);
public FilterResult apply(T object) {
FilterResult result = this.filter.apply(object);
if (result.excluded()) {
reasonConsumer.accept(object, result.getReason());
this.reasonConsumer.accept(object, result.getReason());
}
return result;
}
Expand Down
Expand Up @@ -44,7 +44,7 @@ private EngineIdFilter(String engineId) {
}

@Override
public FilterResult filter(String engineId) {
public FilterResult apply(String engineId) {
return includedIf(this.engineId.equals(engineId), //
() -> "Engine ID matches", //
() -> "Engine ID does not match");
Expand Down
Expand Up @@ -18,10 +18,10 @@
import org.junit.gen5.engine.TestEngine;

/**
* A {@code PostDiscoveryFilter} filters particular tests after test discovery.
* A {@code PostDiscoveryFilter} is applied to tests after test discovery.
*
* <p>These filters must not be applied by a {@link TestEngine} during test
* discovery.
* <p>A {@code PostDiscoveryFilter} must not be applied by a {@link TestEngine}
* during test discovery.
*
* @since 5.0
* @see TestDiscoveryRequest
Expand Down
Expand Up @@ -88,7 +88,7 @@ private Root discoverRoot(TestDiscoveryRequest discoveryRequest, String phase) {
final String engineId = testEngine.getId();

if (discoveryRequest.getEngineIdFilters().stream().map(
engineIdFilter -> engineIdFilter.filter(engineId)).anyMatch(FilterResult::excluded)) {
engineIdFilter -> engineIdFilter.apply(engineId)).anyMatch(FilterResult::excluded)) {
LOG.fine(() -> String.format(
"Test discovery for engine '%s' was skipped due to a filter in phase '%s'.", engineId, phase));
continue;
Expand Down
Expand Up @@ -79,7 +79,7 @@ void prune() {
}

private boolean isExcludedTest(TestDescriptor descriptor, Filter<TestDescriptor> postDiscoveryFilter) {
return descriptor.isTest() && postDiscoveryFilter.filter(descriptor).excluded();
return descriptor.isTest() && postDiscoveryFilter.apply(descriptor).excluded();
}

private void acceptInAllTestEngines(TestDescriptor.Visitor visitor) {
Expand Down
Expand Up @@ -36,8 +36,8 @@ class FilterCompositionTests {
void composingNoFiltersCreatesFilterThatIncludesEverything() {
Filter<Object> composedFilter = Filter.composeFilters();

assertTrue(composedFilter.filter(String.class).included());
assertTrue(composedFilter.filter(Object.class).included());
assertTrue(composedFilter.apply(String.class).included());
assertTrue(composedFilter.apply(Object.class).included());
}

@Test
Expand All @@ -54,8 +54,8 @@ void composingMultipleFiltersIsAConjunctionOfFilters() {

Filter<Class<?>> composed = Filter.composeFilters(firstFilter, secondFilter);

assertFalse(composed.filter(String.class).included());
assertTrue(composed.filter(StringJoiner.class).included());
assertFalse(composed.apply(String.class).included());
assertTrue(composed.apply(StringJoiner.class).included());
}

@Test
Expand All @@ -65,7 +65,7 @@ void aFilterComposedOfMultipleFiltersHasReadableDescription() {

Filter<Object> composed = Filter.composeFilters(firstFilter, secondFilter);

assertFalse(composed.filter(String.class).included());
assertFalse(composed.apply(String.class).included());
assertEquals("(1st) and (2nd)", composed.toString());
}

Expand Down
Expand Up @@ -30,8 +30,8 @@ void classNameMatches() {
ClassFilter filter = ClassFilter.byNamePattern(regex);

assertEquals("Filter class names with regular expression: " + regex, filter.toString());
assertTrue(filter.filter(String.class).included());
assertFalse(filter.filter(Collection.class).included());
assertTrue(filter.apply(String.class).included());
assertFalse(filter.apply(Collection.class).included());
}

}
Expand Up @@ -138,9 +138,9 @@ class TestCase {
assertThat(filters).hasSize(1);

PostDiscoveryFilter filter = filters.get(0);
assertTrue(filter.filter(testDescriptorWithTag("foo")).included());
assertTrue(filter.filter(testDescriptorWithTag("bar")).included());
assertTrue(filter.filter(testDescriptorWithTag("baz")).excluded());
assertTrue(filter.apply(testDescriptorWithTag("foo")).included());
assertTrue(filter.apply(testDescriptorWithTag("bar")).included());
assertTrue(filter.apply(testDescriptorWithTag("baz")).excluded());
}

@Test
Expand All @@ -155,9 +155,9 @@ class TestCase {
assertThat(filters).hasSize(1);

PostDiscoveryFilter filter = filters.get(0);
assertTrue(filter.filter(testDescriptorWithTag("foo")).excluded());
assertTrue(filter.filter(testDescriptorWithTag("bar")).excluded());
assertTrue(filter.filter(testDescriptorWithTag("baz")).included());
assertTrue(filter.apply(testDescriptorWithTag("foo")).excluded());
assertTrue(filter.apply(testDescriptorWithTag("bar")).excluded());
assertTrue(filter.apply(testDescriptorWithTag("baz")).included());
}

@Test
Expand All @@ -172,8 +172,8 @@ class TestCase {
assertThat(filters).hasSize(1);

EngineIdFilter filter = filters.get(0);
assertTrue(filter.filter("foo").included());
assertTrue(filter.filter("bar").excluded());
assertTrue(filter.apply("foo").included());
assertTrue(filter.apply("bar").excluded());
}

@Test
Expand All @@ -192,8 +192,8 @@ class Bar {
assertThat(filters).hasSize(1);

ClassFilter filter = filters.get(0);
assertTrue(filter.filter(Foo.class).included());
assertTrue(filter.filter(Bar.class).excluded());
assertTrue(filter.apply(Foo.class).included());
assertTrue(filter.apply(Bar.class).excluded());
}

@Test
Expand Down
Expand Up @@ -34,7 +34,7 @@ public FilterStub(Function<T, FilterResult> function, Supplier<String> toString)
}

@Override
public FilterResult filter(T object) {
public FilterResult apply(T object) {
return function.apply(object);
}

Expand Down
Expand Up @@ -37,44 +37,44 @@ class TagFilterTests {
void requireSingleTag() throws Exception {
PostDiscoveryFilter requireSingleTag = TagFilter.requireTags("tag1");

Assertions.assertTrue(requireSingleTag.filter(testWithTag1).included());
Assertions.assertTrue(requireSingleTag.filter(testWithBothTags).included());
Assertions.assertTrue(requireSingleTag.apply(testWithTag1).included());
Assertions.assertTrue(requireSingleTag.apply(testWithBothTags).included());

Assertions.assertTrue(requireSingleTag.filter(testWithTag2).excluded());
Assertions.assertTrue(requireSingleTag.filter(testWithNoTags).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithTag2).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithNoTags).excluded());
}

@Test
void requireAtLeastOneOfTwoTags() throws Exception {
PostDiscoveryFilter requireSingleTag = TagFilter.requireTags("tag1", "tag2");

Assertions.assertTrue(requireSingleTag.filter(testWithBothTags).included());
Assertions.assertTrue(requireSingleTag.filter(testWithTag1).included());
Assertions.assertTrue(requireSingleTag.filter(testWithTag2).included());
Assertions.assertTrue(requireSingleTag.apply(testWithBothTags).included());
Assertions.assertTrue(requireSingleTag.apply(testWithTag1).included());
Assertions.assertTrue(requireSingleTag.apply(testWithTag2).included());

Assertions.assertTrue(requireSingleTag.filter(testWithNoTags).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithNoTags).excluded());
}

@Test
void excludeSingleTag() throws Exception {
PostDiscoveryFilter requireSingleTag = TagFilter.excludeTags("tag1");

Assertions.assertTrue(requireSingleTag.filter(testWithTag1).excluded());
Assertions.assertTrue(requireSingleTag.filter(testWithBothTags).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithTag1).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithBothTags).excluded());

Assertions.assertTrue(requireSingleTag.filter(testWithTag2).included());
Assertions.assertTrue(requireSingleTag.filter(testWithNoTags).included());
Assertions.assertTrue(requireSingleTag.apply(testWithTag2).included());
Assertions.assertTrue(requireSingleTag.apply(testWithNoTags).included());
}

@Test
void excludeSeveralTags() throws Exception {
PostDiscoveryFilter requireSingleTag = TagFilter.excludeTags("tag1", "tag2");

Assertions.assertTrue(requireSingleTag.filter(testWithTag1).excluded());
Assertions.assertTrue(requireSingleTag.filter(testWithBothTags).excluded());
Assertions.assertTrue(requireSingleTag.filter(testWithTag2).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithTag1).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithBothTags).excluded());
Assertions.assertTrue(requireSingleTag.apply(testWithTag2).excluded());

Assertions.assertTrue(requireSingleTag.filter(testWithNoTags).included());
Assertions.assertTrue(requireSingleTag.apply(testWithNoTags).included());
}

@Tag("tag1")
Expand Down
Expand Up @@ -57,7 +57,7 @@ private boolean includeClass(ClassTestDescriptor classTestDescriptor, List<Class

// @formatter:off
return (classFilters.stream()
.map(filter -> filter.filter(testClass))
.map(filter -> filter.apply(testClass))
.noneMatch(FilterResult::excluded));
// @formatter:on
}
Expand Down

1 comment on commit d756837

@marcphilipp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.