diff --git a/src/main/java/com/spertus/jacquard/junittester/JUnitTester.java b/src/main/java/com/spertus/jacquard/junittester/JUnitTester.java index 0d51a47..48b0b94 100644 --- a/src/main/java/com/spertus/jacquard/junittester/JUnitTester.java +++ b/src/main/java/com/spertus/jacquard/junittester/JUnitTester.java @@ -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; @@ -69,7 +70,7 @@ public List 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 results) { Preconditions.checkArgument(!results.isEmpty()); @@ -77,30 +78,32 @@ static Result mergeResults(List results) { 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 processResults(List results) { - return results.stream() - .collect(Collectors.groupingBy(Result::getName)) + Map>> 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()); } diff --git a/src/test/java/com/spertus/jacquard/JUnitTesterTest.java b/src/test/java/com/spertus/jacquard/JUnitTesterTest.java index 39f03da..d97b36c 100644 --- a/src/test/java/com/spertus/jacquard/JUnitTesterTest.java +++ b/src/test/java/com/spertus/jacquard/JUnitTesterTest.java @@ -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"; @@ -63,11 +68,12 @@ public void testPackageIncludingSubpackages() { JUnitTester tester = new JUnitTester("com.spertus.jacquard.junittester", true); List 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 @@ -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 results = tester.run(); assertEquals(1, results.size()); } + @Test + public void testGroupingWithDifferentVisibility() { + JUnitTester tester = new JUnitTester(GroupTest2.class); + List results = tester.run(); + assertEquals(2, results.size()); + Map> 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); diff --git a/src/test/java/com/spertus/jacquard/junittester/group/GroupTest.java b/src/test/java/com/spertus/jacquard/junittester/group/GroupTest1.java similarity index 96% rename from src/test/java/com/spertus/jacquard/junittester/group/GroupTest.java rename to src/test/java/com/spertus/jacquard/junittester/group/GroupTest1.java index 66c8eb7..a5d5199 100644 --- a/src/test/java/com/spertus/jacquard/junittester/group/GroupTest.java +++ b/src/test/java/com/spertus/jacquard/junittester/group/GroupTest1.java @@ -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", diff --git a/src/test/java/com/spertus/jacquard/junittester/group/GroupTest2.java b/src/test/java/com/spertus/jacquard/junittester/group/GroupTest2.java new file mode 100644 index 0000000..e3f7801 --- /dev/null +++ b/src/test/java/com/spertus/jacquard/junittester/group/GroupTest2.java @@ -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 + } +}