diff options
author | ankitjavalkar | 2015-04-20 21:42:02 +0530 |
---|---|---|
committer | ankitjavalkar | 2015-04-26 19:46:01 +0530 |
commit | 3ffdba6e587422a0f2955879d12e0b2aeac342e1 (patch) | |
tree | 7ea9136dc4694b1d534dc2df7f1b0353ed4ee075 /testapp/exam/evaluate_python.py | |
parent | cbeffec80d30fe2a9048644f5b0345f797479c92 (diff) | |
download | online_test-3ffdba6e587422a0f2955879d12e0b2aeac342e1.tar.gz online_test-3ffdba6e587422a0f2955879d12e0b2aeac342e1.tar.bz2 online_test-3ffdba6e587422a0f2955879d12e0b2aeac342e1.zip |
Code review - code refactoring as per suggestion
- Add subclasses for different languages
- Create seperate modules for different languages
- Dynamic selection of subclasses based on language used
- Add testcases
Diffstat (limited to 'testapp/exam/evaluate_python.py')
-rw-r--r-- | testapp/exam/evaluate_python.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/testapp/exam/evaluate_python.py b/testapp/exam/evaluate_python.py new file mode 100644 index 0000000..78d6fdf --- /dev/null +++ b/testapp/exam/evaluate_python.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +import sys +import traceback +import os +from os.path import join +import importlib + +# local imports +from code_server import TestCode + +class EvaluatePython(TestCode): + """Tests the Python code obtained from Code Server""" + # def evaluate_python_code(self): + def evaluate_code(self): + success = False + + try: + tb = None + test_code = self._create_test_case() + submitted = compile(self.user_answer, '<string>', mode='exec') + g = {} + exec submitted in g + _tests = compile(test_code, '<string>', mode='exec') + exec _tests in g + except AssertionError: + type, value, tb = sys.exc_info() + info = traceback.extract_tb(tb) + fname, lineno, func, text = info[-1] + text = str(test_code).splitlines()[lineno-1] + err = "{0} {1} in: {2}".format(type.__name__, str(value), text) + else: + success = True + err = 'Correct answer' + + del tb + return success, err + + def _create_test_case(self): + """ + Create assert based test cases in python + """ + test_code = "" + for test_case in self.test_parameter: + pos_args = ", ".join(str(i) for i in test_case.get('pos_args')) if test_case.get('pos_args') \ + else "" + kw_args = ", ".join(str(k+"="+a) for k, a in test_case.get('kw_args').iteritems()) \ + if test_case.get('kw_args') else "" + args = pos_args + ", " + kw_args if pos_args and kw_args else pos_args or kw_args + tcode = "assert {0}({1}) == {2}" \ + .format(test_case.get('func_name'), args, test_case.get('expected_answer')) + test_code += tcode + "\n" + return test_code
\ No newline at end of file |