I have encountered an issue with displaying newlines (linebreaks) in the "expected" and "got" feedback shown to students. I am using a brand new installation of Moodle & Coderunner, but I have encountered a similar issue on an older installation. I have the following Java template grader:
public class Main { public static void main(String[] args) { String studentAnswer = studentProgram(); String correctAnswer = correctProgram(); if (studentAnswer.equals(correctAnswer)) { System.out.printf("{\"inputs\":\"No input\","); System.out.printf("\"expected\":\"\","); System.out.printf("\"got\":\"Passed!\","); System.out.printf("\"fraction\":\"1.00\"}"); } else { System.out.printf("{\"inputs\":\"No input\","); System.out.printf("\"expected\":\"%s\",", correctAnswer); System.out.printf("\"got\":\"%s\",", studentAnswer); System.out.printf("\"fraction\":\"0.00\"}"); } } public static String studentProgram() { {{ STUDENT_ANSWER }} } public static String correctProgram() { {{ QUESTION.answer }} } }
The answer contains
return "Hello, world!";
and it works fine for correct and incorrect student answers. Except, if the student were to input
return "Hello,\nworld!";
(or any text that contains a newline or "\t" or "\r"). Then it reports (for example):
Bad output from grader: {"inputs":"No input","expected":"Hello, world!","got":"Hello, world!","fraction":"0.00"}. Your program execution may have aborted (e.g. a timeout or memory limit exceeded).
Note: I could filter the return value in Java, but there are cases where it is, in fact, correct and should be displayed. I have looked at the CodeRunner source code, but I cannot fathom what is going wrong. I have seen examples that show multiple lines in the output, but they do not really help me to resolve my issue. Any idea of what I could/should do?