Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 11 additions & 128 deletions yaksh/evaluator_tests/test_java_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,8 @@ def setUp(self):
f.write('2'.encode('ascii'))
tmp_in_dir_path = tempfile.mkdtemp()
self.tc_data = dedent("""
class main
{
public static <E> void check(E expect, E result)
{
if(result.equals(expect))
{
System.out.println("Correct:Output expected "+expect+
"and got "+result);
}
else
{
System.out.println("Incorrect:Output expected "+expect+
"but got "+result);
System.exit(1);
}
}
public static void main(String arg[])
{
Test t = new Test();
int result, input, output;
input = 0; output = 0;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
input = 5; output = 25;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
input = 6; output = 36;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
}
}
Test t = new Test();
assert t.square_num(5) == 25: "Square of 5 is not 25";
""")

self.test_case_data = [
Expand Down Expand Up @@ -172,33 +137,12 @@ def test_file_based_assert(self):
# Given
self.file_paths = [(self.f_path, False)]
self.tc_data = dedent("""
class main
{
public static <E> void check(E expect, E result)
{
if(result.equals(expect))
{
System.out.println("Correct:Output expected "+expect+
" and got "+result);
}
else
{
System.out.println("Incorrect:Output expected "+expect+
" but got "+result);
System.exit(1);
}
}
public static void main(String arg[])
{
String result = "";
Test t = new Test();
try{
result = t.readFile();}
catch(Exception e){
Test t = new Test();
try{
String a = t.readFile();
assert a.equals("2");
} catch(Exception e) {
System.out.print(e);
}
check("2", result);
}
}
""")
self.test_case_data = [
Expand Down Expand Up @@ -237,40 +181,14 @@ class Test{
# When
grader = Grader(self.in_dir)
result = grader.evaluate(kwargs)

# Then
self.assertTrue(result.get("success"))

def test_incorrect_testcase(self):
# Given
self.tc_data = dedent("""
class main
{
public static <E> void check(E expect, E result)
{
if(result.equals(expect))
{
System.out.println("Correct:Output expected "+expect+
"and got "+result);
}
else
{
System.out.println("Incorrect:Output expected "+expect+
"but got "+result);
System.exit(1);
}
}
public static void main(String arg[])
{
Test t = new Test();
int result, input, output;
input = 0; output = 0;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result)
}
}
Test t = new Test()
result = t.square_num(5) == 25;
""")
user_answer = ("class Test {\n\tint square_num(int a) "
"{\n\treturn a;\n\t}\n}")
Expand Down Expand Up @@ -701,43 +619,8 @@ def test_assert_with_hook(self):
user_answer = ("class Test {\n\tint square_num(int a)"
" {\n\treturn a*a;\n\t}\n}")
assert_test_case = dedent("""
class main
{
public static <E> void check(E expect, E result)
{
if(result.equals(expect))
{
System.out.println("Correct:Output expected "+expect+
" and got "+result);
}
else
{
System.out.println("Incorrect:Output expected "+expect+
" but got "+result);
System.exit(1);
}
}
public static void main(String arg[])
{
Test t = new Test();
int result, input, output;
input = 0; output = 0;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
input = 5; output = 25;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
input = 6; output = 36;
result = t.square_num(input);
System.out.println("Input submitted to the function: "+
input);
check(output, result);
}
}
Test t = new Test();
assert t.square_num(6) == 36;
""")

hook_code = dedent("""\
Expand Down
Binary file modified yaksh/fixtures/demo_questions.zip
Binary file not shown.
18 changes: 15 additions & 3 deletions yaksh/java_code_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from os.path import isfile
import subprocess
from textwrap import dedent

# Local imports
from .base_evaluator import BaseEvaluator
Expand Down Expand Up @@ -53,17 +54,28 @@ def set_file_paths(self, directory, file_name):
output_path = "{0}{1}.class".format(directory, file_name)
return output_path

def create_main_file_content(self):
content = dedent('''
class main {{
public static void main( String args[] ) {{
{0};
}}
}}
'''.format(self.test_case))
return content

def compile_code(self):
if self.compiled_user_answer and self.compiled_test_code:
return None
else:
# create student code and moderator code file
self.submit_code_path = self.create_submit_code_file('Test.java')
self.submit_code_path = self.create_submit_code_file('code.java')
self.test_code_path = self.create_submit_code_file('main.java')
self.write_to_submit_code_file(
self.submit_code_path, self.user_answer
)
self.write_to_submit_code_file(self.test_code_path, self.test_case)
main_content = self.create_main_file_content()
self.write_to_submit_code_file(self.test_code_path, main_content)
clean_ref_code_path = self.test_code_path
if self.file_paths:
self.files = copy_files(self.file_paths)
Expand All @@ -86,7 +98,7 @@ def compile_code(self):
clean_ref_code_path,
user_code_directory
)
self.run_command_args = "java -cp {0} {1}".format(
self.run_command_args = "java -ea -cp {0} {1}".format(
user_code_directory,
ref_file_name
)
Expand Down