diff --git a/src/parse.py b/src/parse.py index 2cf8689..367bdea 100644 --- a/src/parse.py +++ b/src/parse.py @@ -89,6 +89,20 @@ FOUND_TERMINATOR = enum.auto() # pragma: no mutate +# Message identifiers for ParseErrors +ParseErrorMessageIDs = { + ParseError.TEST_ERROR: "ParseErrorTestError", + ParseError.NO_TOKEN: "ParseErrorNoToken", + ParseError.WRONG_TOKEN: "ParseErrorWrongToken", + ParseError.FOUND_STARTTEXT: "ParseErrorFoundStartText", + ParseError.FOUND_STARTNOTE: "ParseErrorFoundStartNote", + ParseError.NOT_BOOL: "ParseErrorNotBool", + ParseError.FOUND_ENDNOTE: "ParseErrorFoundEndNote", + ParseError.RESERVED_NAME: "ParseErrorReservedName", + ParseError.FOUND_TERMINATOR: "ParseErrorFoundTerminator", +} + + # Exception thrown when a parse error is encountered class ParseErrorException(BaseException): def __init__(self, error, token, expected, context): @@ -327,10 +341,11 @@ def format_exception(exception): has_expected = exception.expected is not None has_token = exception.token is not None + error = Message(ParseErrorMessageIDs[exception.error], []) if has_expected: args = [exception.expected] else: - args = [exception.error] + args = [error] if has_token: line = exception.token.location.line offset = exception.token.location.offset diff --git a/tests/parse/test_error.py b/tests/parse/test_error.py index ff487c0..7e26d15 100644 --- a/tests/parse/test_error.py +++ b/tests/parse/test_error.py @@ -45,6 +45,20 @@ # +# Mapping of error to message identifiers +error_message_ids = { + ParseError.TEST_ERROR: "ParseErrorTestError", + ParseError.NO_TOKEN: "ParseErrorNoToken", + ParseError.WRONG_TOKEN: "ParseErrorWrongToken", + ParseError.FOUND_STARTTEXT: "ParseErrorFoundStartText", + ParseError.FOUND_STARTNOTE: "ParseErrorFoundStartNote", + ParseError.NOT_BOOL: "ParseErrorNotBool", + ParseError.FOUND_ENDNOTE: "ParseErrorFoundEndNote", + ParseError.RESERVED_NAME: "ParseErrorReservedName", + ParseError.FOUND_TERMINATOR: "ParseErrorFoundTerminator", +} + + # Draws a strategy, with 25% of draws being None @composite def draw_maybe(draw, strategy): @@ -163,12 +177,15 @@ # - Message("ParserErrorAt", [error, line, offset]) # - Message("ParserErrorExpected", [expected]) # - Message("ParserErrorExpectedAt", [expected, line, offset]) -@given(draw_parse_error_exception()) -def test_parse_error_format_exception(exception): +# error is a message representing the ParseError, equivalent to: +# - Message(error_message_ids[exception.error], []) +# line is a source file's line number +# offset is a source file's line offset +def _test_parse_error_format_exception(exception): has_expected = exception.expected is not None has_location = exception.token is not None # Variables used for message parameters - err = exception.error + err = Message(error_message_ids[exception.error], []) expect = exception.expected if has_location: line = exception.token.location.line @@ -195,6 +212,19 @@ assert expected == value +# Tests formatting with a random ParseErrorException +@given(draw_parse_error_exception()) +def test_parse_error_format_exception(exception): + _test_parse_error_format_exception(exception) + + +# Tests formatting with each ParseError +@given(draw_parse_error()) +def test_parse_error_format_parse_error(error): + exception = ParseErrorException(error, None, None, static_parse_context()) + _test_parse_error_format_exception(exception) + + # Tests formatting a full error # We expect the following behaviour: # - An array of Messages are returned