diff --git a/tests/parse/test_value.py b/tests/parse/test_value.py index 745a137..58dab06 100644 --- a/tests/parse/test_value.py +++ b/tests/parse/test_value.py @@ -25,7 +25,7 @@ # Dummy Parser for testing value parsing # Instead of actually parsing values just return a static value to show what # the parser would normally do -class ParserValueMock(Parser): +class ParserValueMockValid(Parser): def parse_bool(self, stream, context): stream.pop() return ParserMockAction.PARSE_BOOL @@ -39,6 +39,19 @@ return ParserMockAction.PARSE_REFERENCE +# Dummy Parser for testing error propagation +# Uses parser mock values as errors +class ParserValueMockError(Parser): + def parse_bool(self, stream, context): + raise ParseErrorException(ParserMockAction.PARSE_BOOL, None, None, context) + + def parse_text(self, stream, context): + raise ParseErrorException(ParserMockAction.PARSE_TEXT, None, None, context) + + def parse_reference(self, stream, context): + raise ParseErrorException(ParserMockAction.PARSE_REFERENCE, None, None, context) + + # Generates a strategy for a valid word and parser action def token_and_action(word, action): return just(([static_token_by_value(word)], action)) @@ -62,12 +75,22 @@ return ([token], ParserMockAction.PARSE_REFERENCE) +# Draws tokens for a valid value +@composite +def draw_token_value_valid(draw): + strategies = [ + draw_token_value_literal(), + draw_token_value_reference(), + ] + return draw(one_of(strategies)) + + # Tests parsing a literal value # We expect the following behaviour: # - parse_value parses a Bool if it sees True or False # - parse_value parses a Text if it sees StartText # template_parse_valid provides general parsing properties -@template_parse_valid(ParserValueMock().parse_value, draw_token_value_literal()) +@template_parse_valid(ParserValueMockValid().parse_value, draw_token_value_literal()) def test_parse_value_literal(): pass @@ -76,7 +99,7 @@ # We expect the following behaviour: # - parse_value parses a Reference if it sees an unknown value # template_parse_valid provides general parsing properties -@template_parse_valid(ParserValueMock().parse_value, draw_token_value_reference()) +@template_parse_valid(ParserValueMockValid().parse_value, draw_token_value_reference()) def test_parse_value_reference(): pass @@ -102,3 +125,14 @@ context = ParseContext(ParseTask.PARSE_VALUE, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) return ([], error) + + +# Tests parse_value error propagation +# We expect the following behaviour: +# - Errors from parsing are propagated and have the correct context +@template_parse_invalid(ParserValueMockError().parse_value) +def test_parse_value_error_propagation(draw, parent_context): + (tokens, action) = draw(draw_token_value_valid()) + context = ParseContext(ParseTask.PARSE_VALUE, tokens[0], parent_context) + error = ParseErrorException(action, None, None, context) + return (tokens, error)