Newer
Older
NewLang / tests / parse / test_bool.py
# SPDX-License-Identifier: LGPL-2.1-only
# Copyright 2022 Jookia <contact@jookia.org>

from hypothesis import assume
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.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):
    token = draw(draw_token_random())
    assume(token.value not in ["True", "False"])
    return token


# 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
# template_parse_valid provides general parsing properties
@template_parse_valid(Parser().parse_bool, draw_token_bool_valid())
def test_parse_bool_valid():
    pass


# Tests parsing of invalid booleans
# We expect the following behaviour:
# - Error if the token is not True or False
# - Have ParseError.NOT_BOOL as the exception code
@template_parse_invalid(Parser().parse_bool)
def test_parse_bool_invalid_incorrect(draw, parent_context):
    token = draw(draw_token_not_bool())
    context = ParseContext(ParseTask.PARSE_BOOL, token, parent_context)
    error = ParseErrorException(ParseError.NOT_BOOL, token, None, context)
    return ([token], error)


# Tests parsing of empty tokens
# We expect the following behaviour:
# - Error if there isn't a token
# - Have ParseError.NO_TOKEN as the exception code
@template_parse_invalid(Parser().parse_bool)
def test_parse_bool_invalid_empty(draw, parent_context):
    context = ParseContext(ParseTask.PARSE_BOOL, None, parent_context)
    error = ParseErrorException(ParseError.NO_TOKEN, None, None, context)
    return ([], error)