Class: JavaTestHook

Inherits:
Mumukit::Templates::MultiFileHook
  • 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(*filenames) ⇒ Object



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

def command_line(*filenames)
  "runjunit #{filenames.join(' ')}"
end

#compile_file_content(request) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/test_hook.rb', line 41

def compile_file_content(request)
  test = <<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;
#{has_files?(request) ? '' : 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

  has_files?(request) ?
      files_of(request).merge('SubmissionTest.java' => test) :
      test
end

#format_errored_result(result) ⇒ Object



37
38
39
# File 'lib/test_hook.rb', line 37

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



29
30
31
32
33
34
35
# File 'lib/test_hook.rb', line 29

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
17
# File 'lib/test_hook.rb', line 14

def to_structured_result(result)
  clean_json = result.strip.gsub("\n", '\n')
  transform(JSON.parse(clean_json))
end

#transform(examples) ⇒ Object



19
20
21
22
23
# File 'lib/test_hook.rb', line 19

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

#transform_test_title(test_name) ⇒ Object



25
26
27
# File 'lib/test_hook.rb', line 25

def transform_test_title(test_name)
  test_name.gsub('_', ' ')
end