diff --git a/src/parse.py b/src/parse.py index 6e8b39d..5a0347e 100644 --- a/src/parse.py +++ b/src/parse.py @@ -336,3 +336,13 @@ offset = exception.token.location.offset args = args + [line, offset] return Message(id, args) + + +# Formats a ParseErrorException and its contexts +def format_full_error(exception): + formatted = [format_exception(exception)] + context = exception.context + while context is not None: + formatted.append(format_context(context)) + context = context.parent + return formatted diff --git a/tests/parse/test_error.py b/tests/parse/test_error.py index 1195627..63e90b5 100644 --- a/tests/parse/test_error.py +++ b/tests/parse/test_error.py @@ -35,6 +35,7 @@ ParseTask, format_context, format_exception, + format_full_error, ) from tests.templates import template_test_structure from tests.test_token import draw_token_random, static_token @@ -189,3 +190,20 @@ expected = messages[has_expected][has_location] value = format_exception(exception) assert expected == value + + +# Tests formatting a full error +# We expect the following behaviour: +# - An array of Messages are returned +# - The first message is the exception formatted +# - The second messages is the exception's context formatted +# - Subsequent messages are of any context decedents formatted +@given(draw_parse_error_exception()) +def test_parse_error_format_full_error(exception): + expected = [format_exception(exception)] + context = exception.context + while context is not None: + expected.append(format_context(context)) + context = context.parent + value = format_full_error(exception) + assert expected == value