diff options
author | Prabhu Ramachandran | 2017-11-10 17:50:48 +0530 |
---|---|---|
committer | GitHub | 2017-11-10 17:50:48 +0530 |
commit | 96f8e0af5b39338741c758de918e32e02b95f0c8 (patch) | |
tree | 80fb17501e7995ed3abb34ac3b0d62dd8decc560 /yaksh/error_messages.py | |
parent | cfcb2ed39c724639fe17338e29e327d08ae641b2 (diff) | |
parent | 95f862caee8ca6077ee8f9a8fc88d9ca44db1cdf (diff) | |
download | online_test-96f8e0af5b39338741c758de918e32e02b95f0c8.tar.gz online_test-96f8e0af5b39338741c758de918e32e02b95f0c8.tar.bz2 online_test-96f8e0af5b39338741c758de918e32e02b95f0c8.zip |
Merge pull request #380 from maheshgudi/beautify_assertions
Prettify assertion error output
Diffstat (limited to 'yaksh/error_messages.py')
-rw-r--r-- | yaksh/error_messages.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/yaksh/error_messages.py b/yaksh/error_messages.py new file mode 100644 index 0000000..7ea8618 --- /dev/null +++ b/yaksh/error_messages.py @@ -0,0 +1,62 @@ +try: + from itertools import zip_longest +except ImportError: + from itertools import izip_longest as zip_longest + +def prettify_exceptions(exception, message, traceback=None, testcase=None): + err = {"type": "assertion", + "exception": exception, + "traceback": traceback, + "message": message + } + if exception == 'RuntimeError' or exception == 'RecursionError': + err["traceback"] = None + + if exception == 'AssertionError': + value = ("Expected answer from the" + + " test case did not match the output") + err["message"] = value + err["traceback"] = None + if testcase: + err["test_case"] = testcase + return err + +def _get_incorrect_user_lines(exp_lines, user_lines): + err_line_numbers = [] + for line_no, (expected_line, user_line) in \ + enumerate(zip_longest(exp_lines, user_lines)): + if not user_line or not expected_line or \ + user_line.strip() != expected_line.strip(): + err_line_numbers.append(line_no) + return err_line_numbers + +def compare_outputs(expected_output, user_output, given_input=None): + given_lines = user_output.splitlines() + exp_lines = expected_output.splitlines() + msg = {"type": "stdio", + "given_input": given_input, + "expected_output": exp_lines, + "user_output": given_lines + } + ng = len(given_lines) + ne = len(exp_lines) + err_line_numbers = _get_incorrect_user_lines(exp_lines, given_lines) + msg["error_line_numbers"] = err_line_numbers + if ng != ne: + msg["error_msg"] = ("Incorrect Answer: " + + "We had expected {} number of lines. "\ + .format(ne) + + "We got {} number of lines.".format(ng) + ) + return False, msg + else: + if err_line_numbers: + msg["error_msg"] = ("Incorrect Answer: " + + "Line number(s) {0} did not match." + .format(", ".join(map( + str,[x+1 for x in err_line_numbers] + )))) + return False, msg + else: + msg["error_msg"] = "Correct Answer" + return True, msg |