-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JUnit Platform] Support discovery selectors with FilePosition
There was no good way to discover and execute a single test when using a file based test system like Cucumber. Recently JUnit added support for file position in the `FileSelector` and `ClasspathResourceSelector`. See: junit-team/junit5#2146 This implements a filter that prunes all scenarios that do not match the `FilePosition` from the discovery selector. This feature itself is not new. It was already implemented for the `UriSelector`.
- Loading branch information
1 parent
92ee5cf
commit 560676b
Showing
6 changed files
with
139 additions
and
45 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
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
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
62 changes: 62 additions & 0 deletions
62
...platform-engine/src/main/java/io/cucumber/junit/platform/engine/TestDescriptorOnLine.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,62 @@ | ||
package io.cucumber.junit.platform.engine; | ||
|
||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.discovery.ClasspathResourceSelector; | ||
import org.junit.platform.engine.discovery.FileSelector; | ||
import org.junit.platform.engine.discovery.UriSelector; | ||
import org.junit.platform.engine.support.descriptor.ClasspathResourceSource; | ||
import org.junit.platform.engine.support.descriptor.FileSource; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
|
||
import static org.junit.platform.engine.support.descriptor.FilePosition.fromQuery; | ||
|
||
@SuppressWarnings("Convert2MethodRef") | ||
class TestDescriptorOnLine { | ||
|
||
static Predicate<TestDescriptor> testDescriptorOnLine(int line) { | ||
return descriptor -> descriptor.getSource() | ||
.flatMap(testSource -> { | ||
if (testSource instanceof FileSource) { | ||
FileSource fileSystemSource = (FileSource) testSource; | ||
return fileSystemSource.getPosition(); | ||
} | ||
if (testSource instanceof ClasspathResourceSource) { | ||
ClasspathResourceSource classpathResourceSource = (ClasspathResourceSource) testSource; | ||
return classpathResourceSource.getPosition(); | ||
} | ||
return Optional.empty(); | ||
}) | ||
.map(filePosition -> filePosition.getLine()) | ||
.map(testSourceLine -> line == testSourceLine) | ||
.orElse(false); | ||
} | ||
|
||
private static boolean anyTestDescriptor(TestDescriptor testDescriptor) { | ||
return true; | ||
} | ||
|
||
static Predicate<TestDescriptor> from(UriSelector selector) { | ||
String query = selector.getUri().getQuery(); | ||
return fromQuery(query) | ||
.map(filePosition -> filePosition.getLine()) | ||
.map(TestDescriptorOnLine::testDescriptorOnLine) | ||
.orElse(TestDescriptorOnLine::anyTestDescriptor); | ||
} | ||
|
||
static Predicate<TestDescriptor> from(ClasspathResourceSelector selector) { | ||
return selector.getPosition() | ||
.map(filePosition -> filePosition.getLine()) | ||
.map(TestDescriptorOnLine::testDescriptorOnLine) | ||
.orElse(TestDescriptorOnLine::anyTestDescriptor); | ||
} | ||
|
||
static Predicate<TestDescriptor> from(FileSelector selector) { | ||
return selector.getPosition() | ||
.map(filePosition -> filePosition.getLine()) | ||
.map(TestDescriptorOnLine::testDescriptorOnLine) | ||
.orElse(TestDescriptorOnLine::anyTestDescriptor); | ||
} | ||
|
||
} |
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
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