Skip to content

Commit

Permalink
skill-demo2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpolitz committed Nov 29, 2022
1 parent d2e8c88 commit 0245bbc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
44 changes: 40 additions & 4 deletions TestListExamples.java
Original file line number Diff line number Diff line change
@@ -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<String> left = Arrays.asList("a", "b", "c");
List<String> right = Arrays.asList("a", "d");
List<String> merged = ListExamples.merge(left, right);
List<String> expected = Arrays.asList("a", "a", "b", "c", "d");
assertEquals(expected, merged);
}

@Test(timeout = 500)
public void testMergeLeftEnd() {
List<String> left = Arrays.asList("a", "b", "z");
List<String> right = Arrays.asList("a", "d");
List<String> merged = ListExamples.merge(left, right);
List<String> expected = Arrays.asList("a", "a", "b", "d", "z");
assertEquals(expected, merged);
}

@Test(timeout = 500)
public void testFilterSingle() {
List<String> input = Arrays.asList("Moon", "MOO", "moo");
List<String> expect = Arrays.asList("Moon");
List<String> filtered = ListExamples.filter(input, new IsMoon());
assertEquals(expect, filtered);
}

@Test(timeout = 500)
public void testFilterMulti() {
List<String> input = Arrays.asList("Moon", "MOO", "moon", "MOON");
List<String> expect = Arrays.asList("Moon", "moon", "MOON");
List<String> filtered = ListExamples.filter(input, new IsMoon());
assertEquals(expect, filtered);
}
// Write your grading tests here!
}
49 changes: 46 additions & 3 deletions grade.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0245bbc

Please sign in to comment.