diff --git a/tests/parse/test_text.py b/tests/parse/test_text.py index abe991e..9bbf587 100644 --- a/tests/parse/test_text.py +++ b/tests/parse/test_text.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia -from hypothesis import assume +from hypothesis import assume, given from hypothesis.strategies import ( composite, lists, @@ -16,10 +16,11 @@ Parser, ) from tests.parse.templates import ( - insert_random_within, - template_parse_invalid, - template_parse_valid, + draw_random_within, + template_test_invalid, + template_test_valid, ) +from tests.parse.test_error import static_parse_context from tests.test_token import ( static_token_by_value, draw_token_random, @@ -56,15 +57,23 @@ return (all_tokens, Text(value)) +# Draws just the tokens of a valid text string +@composite +def draw_token_text_valid_tokens(draw): + (tokens, _) = draw(draw_token_text_valid()) + return tokens + + # Tests parse_text works correctly # We expect the following behaviour: # - The resulting text is the value of tokens between StartText and EndText # - The value of the tokens is joined by U+0020 SPACE code points # - The Token's value is the resulting text -# template_parse_valid provides general parsing properties -@template_parse_valid(Parser().parse_text, draw_token_text_valid()) -def test_parse_text_valid(): - pass +# template_test_valid provides general parsing properties +@given(draw_token_text_valid()) +def test_parse_text_valid(valid_data): + (tokens, expected) = valid_data + template_test_valid(Parser().parse_text, tokens, expected) # Test parsing text without StartText @@ -72,14 +81,15 @@ # - Error if StartText's token value is not "StartText" # - Have ParseError.PARSE_TEXT as the exception code # - Have ParseTask.PARSE_TEXT as the context's parse task -@template_parse_invalid(Parser().parse_text) -def test_parse_text_invalid_nostarttext(draw, parent_context): - (tokens, _) = draw(draw_token_text_valid()) - token = draw(draw_token_not_starttext()) - new_tokens = [token] + tokens[1:0] +@given(draw_token_text_valid_tokens(), draw_token_not_starttext()) +def test_parse_text_invalid_nostarttext(tokens, not_starttext): + new_tokens = [not_starttext] + tokens[1:0] + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_TEXT, new_tokens[0], parent_context) - error = ParseErrorException(ParseError.WRONG_TOKEN, token, "StartText", context) - return (new_tokens, error) + error = ParseErrorException( + ParseError.WRONG_TOKEN, not_starttext, "StartText", context + ) + template_test_invalid(Parser().parse_text, parent_context, new_tokens, error) # Tests parsing empty text @@ -87,11 +97,11 @@ # - Error if there is no StartText token at all # - Have ParseError.NO_TOKEN as the exception code # - Have ParseTask.PARSE_TEXT as the context's parse task -@template_parse_invalid(Parser().parse_text) -def test_parse_text_invalid_empty(draw, parent_context): +def test_parse_text_invalid_empty(): + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_TEXT, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) - return ([], error) + template_test_invalid(Parser().parse_text, parent_context, [], error) # Tests parsing text with a StartText token in it @@ -99,14 +109,13 @@ # - Error if a StartText token is in the text content # - Have ParseError.FOUND_STARTTEXT as the exception code # - Have ParseTask.PARSE_TEXT as the context's parse task -@template_parse_invalid(Parser().parse_text) -def test_parse_text_invalid_extrastarttext(draw, parent_context): - (tokens, _) = draw(draw_token_text_valid()) - start = static_token_by_value("StartText") - new_tokens = insert_random_within(draw, tokens, start) - context = ParseContext(ParseTask.PARSE_TEXT, new_tokens[0], parent_context) +@given(draw_random_within(draw_token_text_valid_tokens(), "StartText")) +def test_parse_text_invalid_extrastarttext(within): + (tokens, start) = within + parent_context = static_parse_context() + context = ParseContext(ParseTask.PARSE_TEXT, tokens[0], parent_context) error = ParseErrorException(ParseError.FOUND_STARTTEXT, start, None, context) - return (new_tokens, error) + template_test_invalid(Parser().parse_text, parent_context, tokens, error) # Tests parsing text without an EndText token @@ -114,9 +123,10 @@ # - Error if there is no EndText token at all # - Have ParseError.NO_TOKEN as the exception code # - Have ParseTask.PARSE_TEXT as the context's parse task -@template_parse_invalid(Parser().parse_text) -def test_parse_text_invalid_noendtext(draw, parent_context): - (tokens, _) = draw(draw_token_text_valid()) +@given(draw_token_text_valid_tokens()) +def test_parse_text_invalid_noendtext(tokens): + new_tokens = tokens[0:-1] + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_TEXT, tokens[0], parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) - return (tokens[0:-1], error) + template_test_invalid(Parser().parse_text, parent_context, new_tokens, error)