Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly merge test cases into the existing coverage tree #98

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/main/java/edu/hm/hafner/coverage/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,26 +629,34 @@ public final Node copyNode() {
public abstract Node copy();

/**
* Maps the test cases in all test classes of the specified {@link Node} to the corresponding class nodes of this
* tree. The mapping is done by the name of the test class. If the name of the test class can't be mapped to a
* target class, then the tests of this test class are ignored.
* Merges the test cases of the specified test classes into the corresponding production classes of this coverage
* tree. The mapping is done by evaluating the name of the test class. If the name of the test class cannot be
* mapped to a target class, then the tests of this test class are ignored.
*
* @param testClassNodes
* the test classes containing the test cases
*
* @return the test classes that have not been merged into this coverage tree
*/
public void mapTests(final List<ClassNode> testClassNodes) {
testClassNodes.forEach(this::mapTestClass);
public Set<ClassNode> mergeTests(final Collection<ClassNode> testClassNodes) {
return testClassNodes.stream()
.map(this::mapTestClass)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}

private void mapTestClass(final ClassNode testClassNode) {
findPackage(testClassNode.getPackageName())
.ifPresent(packageNode -> packageNode.getAllClassNodes().stream()
.filter(classNode -> isTargetOfTest(classNode, testClassNode))
.forEach(classNode -> classNode.addTestCases(testClassNode.getTestCases())));
}
private Optional<ClassNode> mapTestClass(final ClassNode testClassNode) {
Optional<ClassNode> targetClass = findPackage(testClassNode.getPackageName())
.map(Node::getAllClassNodes).stream()
.flatMap(Collection::stream)
.filter(classNode -> classNode.getName().endsWith(createTargetClassName(testClassNode)))
.findFirst();
if (targetClass.isPresent()) {
targetClass.get().addTestCases(testClassNode.getTestCases());

private boolean isTargetOfTest(final ClassNode classNode, final ClassNode testClassNode) {
return classNode.getName().endsWith(createTargetClassName(testClassNode));
return Optional.empty();
}
return Optional.of(testClassNode);
}

private String createTargetClassName(final ClassNode testClassNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ void shouldMapTestCasesToClasses() {
assertThat(tests.getTestCases()).hasSize(257);
assertThat(tests.getAllClassNodes()).extracting(Node::getName).containsExactly(TEST_CLASSES);

var testCases = tests.getTestCases();
coverage.mapTests(tests.getAllClassNodes());
var unmappedTests = coverage.mergeTests(tests.getAllClassNodes());
assertThat(unmappedTests).flatMap(Node::getTestCases).hasSize(10);
assertThat(coverage.getTestCases()).hasSize(247);

testCases.removeAll(coverage.getTestCases());
assertThat(testCases).hasSize(10)
.extracting(TestCase::getClassName)
assertThat(unmappedTests)
.extracting(Node::getName)
.containsOnly("ArchitectureTest", "PackageArchitectureTest");

assertThat(coverage.findFile("Metric.java")).hasValueSatisfying(
Expand Down
Loading