diff --git a/tests/parse/test_bool.py b/tests/parse/test_bool.py index 7b5e7b8..1471eac 100644 --- a/tests/parse/test_bool.py +++ b/tests/parse/test_bool.py @@ -1,23 +1,16 @@ # SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia -from hypothesis import assume +from hypothesis import assume, given from hypothesis.strategies import composite from src.ast_types import Bool from src.parse import ParseContext, ParseError, ParseErrorException, ParseTask, Parser -from tests.parse.templates import template_parse_valid, template_parse_invalid +from tests.parse.templates import template_test_valid, template_test_invalid +from tests.parse.test_error import static_parse_context from tests.test_token import draw_token_bool, draw_token_random -# Draws tokens to make a valid boolean -@composite -def draw_token_bool_valid(draw): - token = draw(draw_token_bool()) - value = token.value == "True" - return ([token], Bool(value)) - - # Draws tokens to not make a valid boolean @composite def draw_token_not_bool(draw): @@ -30,10 +23,12 @@ # We expect the following behaviour: # - The resulting boolean is True if the first token is True # - The resulting boolean is False if the first token is False -# template_parse_valid provides general parsing properties -@template_parse_valid(Parser().parse_bool, draw_token_bool_valid()) -def test_parse_bool_valid(): - pass +# template_test_valid provides general parsing properties +@given(draw_token_bool()) +def test_parse_bool_valid(token): + value = token.value == "True" + expected = Bool(value) + template_test_valid(Parser().parse_bool, [token], expected) # Tests parsing of invalid booleans @@ -41,12 +36,13 @@ # - Error if the token is not True or False # - Have ParseError.NOT_BOOL as the exception code # - Have ParseTask.PARSE_BOOL as the context's parse task -@template_parse_invalid(Parser().parse_bool) -def test_parse_bool_invalid_incorrect(draw, parent_context): - token = draw(draw_token_not_bool()) +@given(draw_token_not_bool()) +def test_parse_bool_invalid_incorrect(token): + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_BOOL, token, parent_context) error = ParseErrorException(ParseError.NOT_BOOL, token, None, context) - return ([token], error) + parser = Parser().parse_bool + template_test_invalid(parser, parent_context, [token], error) # Tests parsing of empty tokens @@ -54,8 +50,9 @@ # - Error if there isn't a token # - Have ParseError.NO_TOKEN as the exception code # - Have ParseTask.PARSE_BOOL as the context's parse task -@template_parse_invalid(Parser().parse_bool) -def test_parse_bool_invalid_empty(draw, parent_context): +def test_parse_bool_invalid_empty(): + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_BOOL, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) - return ([], error) + parser = Parser().parse_bool + template_test_invalid(parser, parent_context, [], error)