diff --git a/TestListExamples.java b/TestListExamples.java index f488609..27f1033 100644 --- a/TestListExamples.java +++ b/TestListExamples.java @@ -1,10 +1,46 @@ import static org.junit.Assert.*; import org.junit.*; +import java.util.Arrays; +import java.util.List; + +class IsMoon implements StringChecker { + public boolean checkString(String s) { + return s.equalsIgnoreCase("moon"); + } +} public class TestListExamples { - @Test(timeout = 8000) - public void testTimeout() { - while(true) {} + @Test(timeout = 500) + public void testMergeRightEnd() { + List left = Arrays.asList("a", "b", "c"); + List right = Arrays.asList("a", "d"); + List merged = ListExamples.merge(left, right); + List expected = Arrays.asList("a", "a", "b", "c", "d"); + assertEquals(expected, merged); + } + + @Test(timeout = 500) + public void testMergeLeftEnd() { + List left = Arrays.asList("a", "b", "z"); + List right = Arrays.asList("a", "d"); + List merged = ListExamples.merge(left, right); + List expected = Arrays.asList("a", "a", "b", "d", "z"); + assertEquals(expected, merged); + } + + @Test(timeout = 500) + public void testFilterSingle() { + List input = Arrays.asList("Moon", "MOO", "moo"); + List expect = Arrays.asList("Moon"); + List filtered = ListExamples.filter(input, new IsMoon()); + assertEquals(expect, filtered); + } + + @Test(timeout = 500) + public void testFilterMulti() { + List input = Arrays.asList("Moon", "MOO", "moon", "MOON"); + List expect = Arrays.asList("Moon", "moon", "MOON"); + List filtered = ListExamples.filter(input, new IsMoon()); + assertEquals(expect, filtered); } - // Write your grading tests here! } diff --git a/grade.sh b/grade.sh index 37de2aa..17eaf54 100644 --- a/grade.sh +++ b/grade.sh @@ -1,13 +1,56 @@ -# Create your grading script here - CPATH='.:lib/hamcrest-core-1.3.jar:lib/junit-4.13.2.jar' rm -rf student-submission git clone $1 student-submission echo 'Finished cloning' +if [[ -f student-submission/ListExamples.java ]] +then + echo 'ListExamples.java found' +else + echo 'ListExamples.java not found' + echo 'Score: 0/4' +fi + cp student-submission/ListExamples.java ./ + javac -cp $CPATH *.java -java -cp $CPATH org.junit.runner.JUnitCore TestListExamples +java -cp $CPATH org.junit.runner.JUnitCore TestListExamples > junit-output.txt + +# The strategy used here relies on the last few lines of JUnit output, which +# looks like: + +# FAILURES!!! +# Tests run: 4, Failures: 2 + +# We check for "FAILURES!!!" and then do a bit of parsing of the last line to +# get the count +FAILURES=`grep -c FAILURES!!! junit-output.txt` + +if [[ $FAILURES -eq 0 ]] +then + echo 'All tests passed' + echo '4/4' +else + # The ${VAR:N:M} syntax gets a substring of length M starting at index N + # Note that since this is a precise character count into the "Tests run:..." + # string, we'd need to update it if, say, we had a double-digit number of + # tests. But it's nice and simple for the purposes of this script. + + # See, for example: + # https://stackoverflow.com/questions/16484972/how-to-extract-a-substring-in-bash + # https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion + + RESULT_LINE=`grep "Tests run:" junit-output.txt` + COUNT=${RESULT_LINE:25:1} + + echo "JUnit output was:" + cat junit-output.txt + echo "" + echo "--------------" + echo "| Score: $COUNT/4 |" + echo "--------------" + echo "" +fi