# SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia <contact@jookia.org> from hypothesis import assume from hypothesis.strategies import composite from src.parse import ParseContext, ParseError, ParseErrorException, ParseTask, Parser from src.syntax import Syntax, SyntaxType from tests.parse.templates import template_parse_valid, template_parse_invalid from tests.parse.test_parse import draw_parse_context from tests.test_syntax import draw_token_bool, draw_syntax_random # Draws tokens to make a valid boolean @composite def draw_syntax_bool_valid(draw): token = draw(draw_token_bool()) value = token.value == "True" result = Syntax(value, token.location, SyntaxType.BOOL) return ([token], result) # Tests parse_bool works correctly # 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 # - The Syntax's type is SyntaxType.BOOL # template_parse_valid provides general parsing properties @template_parse_valid(Parser().parse_bool, draw_syntax_bool_valid()) def test_parse_bool_valid(): pass # Generate an invalid boolean # We expect the following behaviour: # - Error if the token is not a SyntaxType.TOKEN # - Error if the token is not True or False @template_parse_invalid(Parser().parse_bool) def test_parse_bool_invalid_incorrect(draw): parent_context = draw(draw_parse_context()) token = draw(draw_syntax_random()) assume(not (token.type == SyntaxType.TOKEN and token.value in ["True", "False"])) context = ParseContext(ParseTask.PARSE_BOOL, token, parent_context) if token.type == SyntaxType.TOKEN: error = ParseErrorException(ParseError.NOT_BOOL, token, None, context) else: error = ParseErrorException(ParseError.NOT_TOKEN, token, None, context) return ([token], error, parent_context) # Generate no boolean # We expect the following behaviour: # - Error if there isn't a token @template_parse_invalid(Parser().parse_bool) def test_parse_bool_invalid_empty(draw): parent_context = draw(draw_parse_context()) context = ParseContext(ParseTask.PARSE_BOOL, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) return ([], error, parent_context)