Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#40: Introduce composite ClassFilter
------------------------------------------------------------------------ On behalf of the community, the JUnit Lambda Team thanks Klarna AB (http://www.klarna.com) for supporting the JUnit crowdfunding campaign! ------------------------------------------------------------------------
- Loading branch information
1 parent
15132f7
commit 1213ccf
Showing
5 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
junit-engine-api/src/main/java/org/junit/gen5/engine/PredicateBasedClassFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.engine; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
class PredicateBasedClassFilter implements ClassFilter { | ||
|
||
private final Predicate<? super Class<?>> predicate; | ||
private final Supplier<String> descriptionSupplier; | ||
|
||
PredicateBasedClassFilter(Predicate<? super Class<?>> predicate, Supplier<String> descriptionSupplier) { | ||
this.predicate = predicate; | ||
this.descriptionSupplier = descriptionSupplier; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return descriptionSupplier.get(); | ||
} | ||
|
||
@Override | ||
public boolean acceptClass(Class<?> testClass) { | ||
return predicate.test(testClass); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
junit-tests/src/test/java/org/junit/gen5/engine/ClassFiltersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.engine; | ||
|
||
import static org.junit.gen5.api.Assertions.*; | ||
|
||
import java.util.Collection; | ||
import java.util.StringJoiner; | ||
|
||
import org.junit.gen5.api.Test; | ||
|
||
class ClassFiltersTests { | ||
|
||
@Test | ||
void classNameMatches() { | ||
String regex = "^java\\.lang\\..*"; | ||
|
||
ClassFilter filter = ClassFilters.classNameMatches(regex); | ||
|
||
assertEquals("Filter class names with regular expression: " + regex, filter.getDescription()); | ||
assertTrue(filter.acceptClass(String.class)); | ||
assertFalse(filter.acceptClass(Collection.class)); | ||
} | ||
|
||
@Test | ||
void anyClass() { | ||
ClassFilter filter = ClassFilters.anyClass(); | ||
|
||
assertEquals("Any class", filter.getDescription()); | ||
assertTrue(filter.acceptClass(String.class)); | ||
assertTrue(filter.acceptClass(this.getClass())); | ||
} | ||
|
||
@Test | ||
void allOfWithoutFilter() { | ||
ClassFilter[] noFilters = {}; | ||
|
||
ClassFilter filter = ClassFilters.allOf(noFilters); | ||
|
||
assertEquals("Any class", filter.getDescription()); | ||
assertTrue(filter.acceptClass(String.class)); | ||
assertTrue(filter.acceptClass(Object.class)); | ||
} | ||
|
||
@Test | ||
void allOfWithSingleFilter() { | ||
ClassFilter singleFilter = ClassFilters.classNameMatches(".*ring.*"); | ||
|
||
ClassFilter filter = ClassFilters.allOf(singleFilter); | ||
|
||
assertSame(singleFilter, filter); | ||
} | ||
|
||
@Test | ||
void allOfWithMultipleFiltersIsConjunction() { | ||
ClassFilter firstFilter = ClassFilters.classNameMatches(".*ring.*"); | ||
ClassFilter secondFilter = ClassFilters.classNameMatches(".*Join.*"); | ||
|
||
ClassFilter filter = ClassFilters.allOf(firstFilter, secondFilter); | ||
|
||
assertFalse(filter.acceptClass(String.class)); | ||
assertTrue(filter.acceptClass(StringJoiner.class)); | ||
} | ||
|
||
@Test | ||
void allOfWithMultipleFiltersHasReadableDescription() { | ||
ClassFilter firstFilter = new PredicateBasedClassFilter(o -> false, () -> "1st"); | ||
ClassFilter secondFilter = new PredicateBasedClassFilter(o -> true, () -> "2nd"); | ||
|
||
ClassFilter filter = ClassFilters.allOf(firstFilter, secondFilter); | ||
|
||
assertEquals("(1st) and (2nd)", filter.getDescription()); | ||
} | ||
|
||
} |
30 changes: 0 additions & 30 deletions
30
junit-tests/src/test/java/org/junit/gen5/engine/ClassNameFilterTests.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters