Skip to content

Commit

Permalink
Merge pull request #10 from espertus/issue9
Browse files Browse the repository at this point in the history
Group results by name and visibility
  • Loading branch information
espertus authored Mar 5, 2024
2 parents 9221b2f + 5294a7e commit 89e26f6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
27 changes: 15 additions & 12 deletions src/main/java/com/spertus/jacquard/junittester/JUnitTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

Expand Down Expand Up @@ -69,38 +70,40 @@ public List<Result> run() {
return processResults(listener.results);
}

// Note that the merged result has the default visibility set when the autograder was constructed.
// Merge results having the same name and visibility
@VisibleForTesting
static Result mergeResults(List<Result> results) {
Preconditions.checkArgument(!results.isEmpty());
if (results.size() == 1) {
return results.get(0);
}
String name = results.get(0).getName();
Visibility vis = results.get(0).getVisibility();
Preconditions.checkArgument(
results.stream().allMatch((r) -> r.getName().equals(name)));
results.stream()
.allMatch((r) -> r.getName().equals(name) && r.getVisibility().equals(vis)));

double score = results.stream().mapToDouble(Result::getScore).sum();
double maxScore = results.stream().mapToDouble(Result::getMaxScore).sum();
String message = results.stream()
.filter((r) -> r.getScore() < r.getMaxScore())
.map(r -> String.format(Locale.US,
"%.1f: %s",
r.getScore() - r.getMaxScore(),
r.getMessage()))
.map(r -> String.format(Locale.US,
"%.1f: %s",
r.getScore() - r.getMaxScore(),
r.getMessage()))
.collect(Collectors.joining("\n"));

return Result.makeResult(name, score, maxScore, message);
return Result.makeResult(name, score, maxScore, message, vis);
}

// Merge all the results having the same name. The merged result has the
// default visibility level specified when creating the autograder.
// Merge all the results having the same name and visibility.
private static List<Result> processResults(List<Result> results) {
return results.stream()
.collect(Collectors.groupingBy(Result::getName))
Map<String, Map<Visibility, List<Result>>> groupedResults = results.stream().collect(groupingBy(Result::getName, groupingBy(Result::getVisibility)));
return groupedResults
.values()
.stream()
.map(JUnitTester::mergeResults)
.flatMap(visibilityMap -> visibilityMap.values().stream())
.map(group -> mergeResults(group))
.collect(Collectors.toList());
}

Expand Down
30 changes: 25 additions & 5 deletions src/test/java/com/spertus/jacquard/JUnitTesterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import com.spertus.jacquard.common.*;
import com.spertus.jacquard.junittester.SampleTest;
import com.spertus.jacquard.junittester.JUnitTester;
import com.spertus.jacquard.junittester.group.GroupTest;
import com.spertus.jacquard.junittester.group.GroupTest1;
import com.spertus.jacquard.junittester.group.GroupTest2;
import com.spertus.jacquard.junittester.output.OutputTest;
import com.spertus.jacquard.junittester.visibility.VisibilityLevelsTest;
import org.junit.jupiter.api.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JUnitTesterTest {
private static final String PASSING_TEST_NAME = "passingTest";
Expand Down Expand Up @@ -63,11 +68,12 @@ public void testPackageIncludingSubpackages() {
JUnitTester tester = new JUnitTester("com.spertus.jacquard.junittester", true);
List<Result> results = tester.run();
// There should be:
// 1 result from GroupTest
// 1 from GroupTest1
// 2 from GroupTest2
// 2 from OutputTest
// 2 results from SampleTest
// 5 results from VisibilityTest
assertEquals(10, results.size());
assertEquals(12, results.size());
}

@Test
Expand All @@ -90,12 +96,26 @@ public void testVisibility() {
}

@Test
public void testGrouping() {
JUnitTester tester = new JUnitTester(GroupTest.class);
public void testGroupingWithSameVisibility() {
JUnitTester tester = new JUnitTester(GroupTest1.class);
List<Result> results = tester.run();
assertEquals(1, results.size());
}

@Test
public void testGroupingWithDifferentVisibility() {
JUnitTester tester = new JUnitTester(GroupTest2.class);
List<Result> results = tester.run();
assertEquals(2, results.size());
Map<Visibility, List<Result>> map = results.stream().collect(Collectors.groupingBy(Result::getVisibility));
assertEquals(2, map.size());
assertTrue(map.containsKey(Visibility.VISIBLE));
assertEquals(1, map.get(Visibility.VISIBLE).size());
assertEquals(1.0, map.get(Visibility.VISIBLE).get(0).getMaxScore());
assertEquals(1, map.get(Visibility.AFTER_PUBLISHED).size());
assertEquals(2.0, map.get(Visibility.AFTER_PUBLISHED).get(0).getMaxScore());
}

@Test
public void testOutput() {
JUnitTester tester = new JUnitTester(OutputTest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// These 2 tests should get merged together.
@Tag("IndirectTest")
public class GroupTest {
public class GroupTest1 {
@Test
@GradedTest(name = "Bus constructor",
description = "Verifies exception is thrown for null argument",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.spertus.jacquard.junittester.group;

import com.spertus.jacquard.common.Visibility;
import com.spertus.jacquard.junittester.GradedTest;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.fail;

// These 2 tests should get merged together.
@Tag("IndirectTest")
public class GroupTest2 {
@Test
@GradedTest(name = "visible test", visibility = Visibility.VISIBLE)
public void visibleTest() {
fail();
}

@Test
@GradedTest(name = "after published test", visibility = Visibility.AFTER_PUBLISHED)
public void afterPublishedTest1() {
// pass
}

@Test
@GradedTest(name = "after published test", visibility = Visibility.AFTER_PUBLISHED)
public void afterPublishedTest2() {
// pass
}
}

0 comments on commit 89e26f6

Please sign in to comment.