diff options
author | maheshgudi | 2017-10-26 18:34:36 +0530 |
---|---|---|
committer | maheshgudi | 2017-11-07 14:50:09 +0530 |
commit | 16e8d4b3c096e6034c0066adffc8f5a520f272c7 (patch) | |
tree | af551bc18cb679bd816a78b45b8087beabf8a5be | |
parent | 807a52e45a9b6c6ac64a7a5e4bd8248900fb4367 (diff) | |
download | online_test-16e8d4b3c096e6034c0066adffc8f5a520f272c7.tar.gz online_test-16e8d4b3c096e6034c0066adffc8f5a520f272c7.tar.bz2 online_test-16e8d4b3c096e6034c0066adffc8f5a520f272c7.zip |
Beautiful error outputs
-rw-r--r-- | yaksh/compare_stdio.py | 3 | ||||
-rw-r--r-- | yaksh/python_assertion_evaluator.py | 40 | ||||
-rw-r--r-- | yaksh/templates/exam.html | 32 |
3 files changed, 43 insertions, 32 deletions
diff --git a/yaksh/compare_stdio.py b/yaksh/compare_stdio.py index c4076de..9c13a98 100644 --- a/yaksh/compare_stdio.py +++ b/yaksh/compare_stdio.py @@ -18,7 +18,8 @@ def compare_outputs(expected_output, user_output, given_input=None): exp_lines = expected_output.splitlines() msg = {"given_input":given_input, "expected_output": exp_lines, - "user_output":given_lines + "user_output":given_lines, + "type": "stdio" } ng = len(given_lines) ne = len(exp_lines) diff --git a/yaksh/python_assertion_evaluator.py b/yaksh/python_assertion_evaluator.py index c8f2dd0..35a08ec 100644 --- a/yaksh/python_assertion_evaluator.py +++ b/yaksh/python_assertion_evaluator.py @@ -4,6 +4,7 @@ import traceback import os import re from os.path import join +from textwrap import dedent import importlib # Local imports @@ -73,31 +74,26 @@ class PythonAssertionEvaluator(BaseEvaluator): exec(_tests, self.exec_scope) except TimeoutException: raise + except AssertionError: + type, _, tb = sys.exc_info() + tb_info = traceback.extract_tb(tb) + filename, line, func, text = tb_info[-1] + value = "Expected answer from the test case didnt match the output" + err = {"type": "assertion", + "test_case": self.test_case, + "exception": type.__name__, + "message": value + } except Exception: type, value, tb = sys.exc_info() - info = traceback.extract_tb(tb) - fname, lineno, func, text = info[-1] - text = str(self.test_case) + tb_info = traceback.extract_tb(tb) + filename, line, func, text = tb_info[-1] + err = {"type": "assertion", + "test_case": self.test_case, + "exception": type.__name__, + "message": str(value) + } - # Get truncated traceback - err_tb_lines = traceback.format_exc().splitlines() - stripped_tb_lines = [] - for line in err_tb_lines: - line = re.sub(r'File\s+".*?",\s+line', - 'File <file>, line', - line - ) - stripped_tb_lines.append(line) - stripped_tb = '\n'.join(stripped_tb_lines[-10::]) - - err = "Expected Test Case:\n{0}\n" \ - "Error Traceback - {1} {2} in:\n {3}\n{4}".format( - self.test_case, - type.__name__, - str(value), - text, - stripped_tb - ) else: success = True err = None diff --git a/yaksh/templates/exam.html b/yaksh/templates/exam.html index 9596c1c..7b6d54e 100644 --- a/yaksh/templates/exam.html +++ b/yaksh/templates/exam.html @@ -88,17 +88,31 @@ <div class="panel-heading">Testcase No. {{ forloop.counter }}</div> <div class="panel-body"> <div class="well well-sm"> - {% if not error.expected_output %} + {% if not error.type %} <pre><code> {{error|safe}} </code></pre> - {% else %} - {% if error.given_input %} - <table class="table table-bordered"> - <col width="30%"> - <tr class = "active"> - <td> For given Input value(s):</td> - <td>{{error.given_input}}</td> + {% elif error.type == 'assertion' %} + We tried the calling your function with the following test case:<br/> + <pre><code><strong>{{error.test_case}}</strong></code></pre> + <table> + <tr> But the following error took place: </tr> + <tr> + <td>Exception Name</td> + <td><mark style="background-color:#ff9999;">{{error.exception}}</mark> + </td> + </tr> + <tr> + <td>Exception Message</td><td>{{error.message}}</td> </tr> - </table> + </table> + {% elif error.type == 'stdio' %} + {% if error.given_input %} + <table class="table table-bordered"> + <col width="30%"> + <tr class = "active"> + <td> For given Input value(s):</td> + <td>{{error.given_input}}</td> + </tr> + </table> {% endif %} <table class="table table-bordered" width="100%" id="output"> <col width="10%"> |