From 16e8d4b3c096e6034c0066adffc8f5a520f272c7 Mon Sep 17 00:00:00 2001 From: maheshgudi Date: Thu, 26 Oct 2017 18:34:36 +0530 Subject: Beautiful error outputs --- yaksh/compare_stdio.py | 3 ++- yaksh/python_assertion_evaluator.py | 40 +++++++++++++++++-------------------- 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 , 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 @@
Testcase No. {{ forloop.counter }}
- {% if not error.expected_output %} + {% if not error.type %}
 {{error|safe}} 
- {% else %} - {% if error.given_input %} - - - - - + {% elif error.type == 'assertion' %} + We tried the calling your function with the following test case:
+
{{error.test_case}}
+
For given Input value(s):{{error.given_input}}
+ But the following error took place: + + + + + + -
Exception Name{{error.exception}} +
Exception Message{{error.message}}
+ + {% elif error.type == 'stdio' %} + {% if error.given_input %} + + + + + + +
For given Input value(s):{{error.given_input}}
{% endif %} -- cgit