Class: JavaTestHook

Inherits:
Mumukit::Templates::FileHook
  • Object
show all
Defined in:
lib/test_hook.rb

Constant Summary collapse

COMPILATION_ERROR_FLAG =
'!!TEST FINISHED WITH COMPILATION ERROR!!'

Instance Method Summary collapse

Instance Method Details

#command_line(filename) ⇒ Object



10
11
12
# File 'lib/test_hook.rb', line 10

def command_line(filename)
  "runjunit #{filename}"
end

#compile_file_content(request) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/test_hook.rb', line 34

def compile_file_content(request)
  <<EOF
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import java.util.stream.Collectors.*;
import java.time.*;
import org.junit.runner.RunWith;
import org.junit.*;
import org.junit.runner.*;
import org.junit.runner.notification.*;
import org.junit.runners.*;
import org.junit.runners.model.InitializationError;
import org.apache.commons.text.StringEscapeUtils;
#{request.content}
#{request.extra}

public class SubmissionTest {
#{request.test}
@AfterClass
public static void afterAll(){
    System.out.println("!!!JAVA-MUMUKI-OUTPUT!!!");
}

public static void main(String[] args) {
  JUnitCore core = new JUnitCore();
  core.addListener(new MuListener());
  core.run(SubmissionTest.class);
}
}
class MuListener extends RunListener {
private Map<String, Collection<String>> tests = new HashMap<>();
@Override
public void testStarted(Description description) throws Exception {
  String methodName = description.getMethodName();
  tests.put(methodName, Arrays.asList(methodName, "passed"));
}

@Override
public void testFailure(Failure failure) {
  String methodName = failure.getDescription().getMethodName();
  tests.put(methodName, Arrays.asList(methodName, "failed", failure.getMessage()));
}

@Override
public void testRunFinished(Result r) {
  String result = prettyFormatResults(tests.values());
  System.out.println(StringEscapeUtils.unescapeJson(result));
}

public String prettyFormatString(String string) {
  return ("\\\""+string+"\\\"");
}
public String prettyFormatExample(Collection<String> example) {
  return "["+example.stream().map(element -> prettyFormatString(element)).collect(Collectors.joining(",")) +"]";
}
public String prettyFormatResults(Collection<Collection<String>> results) {
  return "["+results.stream().map(element -> prettyFormatExample(element)).collect(Collectors.joining(","))+"]";
}
}
EOF
end

#format_errored_result(result) ⇒ Object



30
31
32
# File 'lib/test_hook.rb', line 30

def format_errored_result(result)
  Mumukit::ContentType::Markdown.highlighted_code :java, result.gsub(COMPILATION_ERROR_FLAG, '').gsub(/\/tmp\/tmp.*\/SubmissionTest/, "/tmp/SubmissionTest")
end

#post_process_file(file, result, status) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/test_hook.rb', line 22

def post_process_file(file, result, status)
  if result.include? COMPILATION_ERROR_FLAG
    [format_errored_result(result), :errored]
  else
    super
  end
end

#tempfile_extensionObject



6
7
8
# File 'lib/test_hook.rb', line 6

def tempfile_extension
  '.java'
end

#to_structured_result(result) ⇒ Object



14
15
16
# File 'lib/test_hook.rb', line 14

def to_structured_result(result)
  transform(JSON.parse(result))
end

#transform(examples) ⇒ Object



18
19
20
# File 'lib/test_hook.rb', line 18

def transform(examples)
  examples.map { |e| [e[0], e[1].to_sym, e[2].try {|result| Mumukit::ContentType::Markdown.code result}] }
end