Skip to content

Commit

Permalink
Add test cases and mutations to API of Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 20, 2023
1 parent 1297501 commit 111cd9c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/main/java/edu/hm/hafner/coverage/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void addTestCase(final TestCase testCase) {
replaceValue(new TestCount(testCases.size()));
}

@Override
public List<TestCase> getTestCases() {
return testCases;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/hm/hafner/coverage/FileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ public void addMutation(final Mutation mutation) {
mutations.add(mutation);
}

@Override
public List<Mutation> getMutations() {
return Collections.unmodifiableList(mutations);
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/edu/hm/hafner/coverage/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,20 @@ public Optional<MethodNode> findMethod(final String searchName, final String sea
.findAny();
}

public List<Mutation> getMutations() {
return getChildren().stream()
.map(Node::getMutations)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

public List<TestCase> getTestCases() {
return getChildren().stream()
.map(Node::getTestCases)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}

/**
* Returns the file names that are contained within the subtree of this node.
*
Expand All @@ -468,11 +482,19 @@ public Set<String> getFiles() {
}

public List<FileNode> getAllFileNodes() {
return getAll(Metric.FILE).stream().map(FileNode.class::cast).collect(Collectors.toList());
return getAll(Metric.FILE, FileNode.class::cast);
}

public List<ClassNode> getAllClassNodes() {
return getAll(Metric.CLASS, ClassNode.class::cast);
}

public List<MethodNode> getAllMethodNodes() {
return getAll(Metric.METHOD).stream().map(MethodNode.class::cast).collect(Collectors.toList());
return getAll(Metric.METHOD, MethodNode.class::cast);
}

private <T extends Node> List<T> getAll(final Metric metric1, final Function<Node, T> cast) {
return getAll(metric1).stream().map(cast).collect(Collectors.toList());
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/edu/hm/hafner/coverage/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ void shouldReturnParentOfNodeAndItsName() {

assertThat(child.getParent()).isEqualTo(parent);

//boundary-interior demonstration (Path "Don't enter loop" is impossible in this case)
assertThat(child.getParentName()).isEqualTo("Parent"); // boundary -> Enter only once and cover all branches
assertThat(subSubPackage.getParentName()).isEqualTo(
"Child.SubPackage"); // interior -> Enter twice and cover all branches

assertThat(child.getParentName()).isEqualTo("Parent");
assertThat(subSubPackage.getParentName()).isEqualTo("Child.SubPackage");
}

@Test
Expand Down Expand Up @@ -727,16 +724,19 @@ void shouldMergeMultipleNodesWithDifferentMetricInList() {
@Test
void shouldGetAllNodesOfTypeInTree() {
Node tree = createTreeWithoutCoverage();
FileNode coveredFile = tree.findFile("Covered.java").orElseThrow();
FileNode missedFile = tree.findFile("Missed.java").orElseThrow();
MethodNode coveredMethod = new MethodNode("coveredMethod", "signature");
MethodNode missedMethod = new MethodNode("missedMethod", "signature");

tree.findClass(COVERED_CLASS).orElseThrow().addChild(coveredMethod);
tree.findClass(MISSED_CLASS).orElseThrow().addChild(missedMethod);

assertThat(tree.getAllMethodNodes()).containsExactlyInAnyOrder(coveredMethod, missedMethod);
assertThat(tree.getAllFileNodes()).containsExactlyInAnyOrder(coveredFile, missedFile);
assertThat(tree.getAllFileNodes()).containsExactlyInAnyOrder(
tree.findFile("Covered.java").orElseThrow(),
tree.findFile("Missed.java").orElseThrow());
assertThat(tree.getAllClassNodes()).containsExactlyInAnyOrder(
tree.findClass("CoveredClass.class").orElseThrow(),
tree.findClass("MissedClass.class").orElseThrow());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ void shouldReadWithNameOnly() {
assertThat(getFirstClass(tree)).hasName("CloudFormation Lint");

assertThat(tree.aggregateValues()).contains(new TestCount(141));
assertThat(tree.getAll(Metric.CLASS).stream()
.map(ClassNode.class::cast)
.map(ClassNode::getTestCases)
.flatMap(Collection::stream)
.filter(test -> test.getStatus() == TestResult.SKIPPED)
.count()).isEqualTo(19);
assertThat(tree.getTestCases()).hasSize(141)
.filteredOn(test -> test.getStatus() == TestResult.SKIPPED).hasSize(19);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void shouldReadAllMutationProperties() {
.hasKillingTest("edu.hm.hafner.coverage.CoverageNodeTest.[engine:junit-jupiter]/[class:edu.hm.hafner.coverage.CoverageNodeTest]/[method:shouldAddChildren()]")
.hasDescription("removed call to edu/hm/hafner/coverage/CoverageNode::setParent")
));

assertThat(tree.getMutations()).hasSize(1);
}

@Test
Expand Down Expand Up @@ -78,6 +80,7 @@ void shouldMapLineCoveragesForPainting() {
assertThat(tree.findFile(LOOKAHEAD_STREAM)).isPresent().get().satisfies(this::verifyLookaheadStream);
assertThat(tree.findFile(FILTERED_LOG)).isPresent().get().satisfies(this::verifyFilteredLog);
assertThat(tree.findFile(ENSURE)).isPresent().get().satisfies(this::verifyEnsure);
assertThat(tree.getMutations()).hasSize(234);
}

private void verifyEnsure(final FileNode file) {
Expand Down

0 comments on commit 111cd9c

Please sign in to comment.