1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
|
import unittest
import os
from exam import evaluate_c, evaluate_cpp, evaluate_bash, evaluate_python
class TestPythonEvaluation(unittest.TestCase):
def setUp(self):
self.language = "Python"
self.test_parameter = [{"func_name": "add",
"expected_answer": "5",
"test_id": u'null',
"pos_args": ["3", "2"],
"kw_args": {}
}]
def test_correct_answer(self):
user_answer = "def add(a, b):\n\treturn a + b"""
get_class = evaluate_python.EvaluatePython(self.test_parameter, self.language, user_answer, ref_code_path=None, in_dir=None)
result = get_class.run_code()
self.assertTrue(result.get("success"))
self.assertEqual(result.get("error"), "Correct answer")
def test_incorrect_answer(self):
user_answer = "def add(a, b):\n\treturn a - b"""
test_parameter = [{"func_name": "add",
"expected_answer": "5",
"test_id": u'null',
"pos_args": ["3", "2"],
"kw_args": {}
}]
get_class = evaluate_python.EvaluatePython(self.test_parameter, self.language, user_answer, ref_code_path=None, in_dir=None)
result = get_class.run_code()
self.assertFalse(result.get("success"))
self.assertEqual(result.get("error"), "AssertionError in: assert add(3, 2) == 5")
def test_infinite_loop(self):
user_answer = "def add(a, b):\n\twhile True:\n\t\tpass"""
test_parameter = [{"func_name": "add",
"expected_answer": "5",
"test_id": u'null',
"pos_args": ["3", "2"],
"kw_args": {}
}]
get_class = evaluate_python.EvaluatePython(self.test_parameter, self.language, user_answer, ref_code_path=None, in_dir=None)
result = get_class.run_code()
self.assertFalse(result.get("success"))
self.assertTrue("Code took more than" in result.get("error"))
###############################################################################
class TestCEvaluation(unittest.TestCase):
def setUp(self):
self.language = "C"
self.ref_code_path = os.getcwd() + "/c_cpp_files/main.cpp"
self.in_dir = "/tmp"
self.test_parameter = []
def test_correct_answer(self):
user_answer = "int add(int a, int b)\n{return a+b;}"
get_class = evaluate_c.EvaluateC(self.test_parameter, self.language, user_answer, self.ref_code_path, self.in_dir)
result = get_class.run_code()
self.assertTrue(result.get("success"))
self.assertEqual(result.get("error"))
def test_incorrect_answer(self):
user_answer = "int add(int a, int b)\n{return a+b}"
get_class = evaluate_c.EvaluateC(self.test_parameter, self.language, user_answer, self.ref_code_path, self.in_dir)
result = get_class.run_code()
self.assertFalse(result.get("success"))
self.assertTrue("compilation error" in result.get("error").lower())
###############################################################################
class TestCPPEvaluation(unittest.TestCase):
def setUp(self):
self.language = "CPP"
self.ref_code_path = os.getcwd() + "/c_cpp_files/main.cpp"
self.in_dir = "/tmp"
self.test_parameter = []
def test_correct_answer(self):
user_answer = "int add(int a, int b)\n{return a+b;}"
get_class = evaluate_cpp.EvaluateCpp(self.test_parameter, self.language, user_answer, self.ref_code_path, self.in_dir)
result = get_class.run_code()
self.assertTrue(result.get("success"))
self.assertEqual(result.get("error"))
def test_incorrect_answer(self):
user_answer = "int add(int a, int b)\n{return a+b}"
get_class = evaluate_cpp.EvaluateCpp(self.test_parameter, self.language, user_answer, self.ref_code_path, self.in_dir)
result = get_class.run_code()
self.assertFalse(result.get("success"))
self.assertTrue("compilation error" in result.get("error").lower())
if __name__ == '__main__':
unittest.main()
|