diff --git a/tests/parse/test_directive.py b/tests/parse/test_directive.py index edc4cb0..2bbf201 100644 --- a/tests/parse/test_directive.py +++ b/tests/parse/test_directive.py @@ -42,6 +42,7 @@ static_token_by_value, ) from tests.parse.test_error import static_parse_context +from src.ast_types import Bool, Statement # # Helper functions @@ -121,6 +122,30 @@ ) +# A simple directive tokens and result +def static_directive_valid(): + return ( + [static_token_by_value("True"), static_token_by_value("Done")], + Statement(Bool(True), None, []), + ) + + +# An invalid directive tokens +def static_directive_invalid(): + return [static_token_by_value("Done")] + + +# An invalid directive tokens +def static_directive_invalid_error(parent_context): + token = static_directive_invalid()[0] + directive_context = ParseContext(ParseTask.PARSE_DIRECTIVE, token, parent_context) + statement_context = ParseContext( + ParseTask.PARSE_STATEMENT, token, directive_context + ) + context = ParseContext(ParseTask.PARSE_SUBJECT, token, statement_context) + return ParseErrorException(ParseError.FOUND_TERMINATOR, token, None, context) + + # # Test functions # diff --git a/tests/parse/test_file.py b/tests/parse/test_file.py index a68a9ce..a84aebe 100644 --- a/tests/parse/test_file.py +++ b/tests/parse/test_file.py @@ -11,62 +11,29 @@ # PARSE_FILE - Used when parsing the file # # No parse errors are generated. -# -# The following parsers are used and have their errors -# and data structures propagated: -# parse_directive - Used to parse a directive -import enum - -from hypothesis import assume, given -from hypothesis.strategies import composite, data, integers, just, lists +from hypothesis import given +from hypothesis.strategies import composite, just, lists from src.token import TokenStream from src.parse import ( ParseContext, - ParseError, - ParseErrorException, ParseTask, Parser, - read_token, ) -from tests.parse.templates import ( - template_test_invalid, -) -from tests.test_token import ( - draw_token_random, - static_token_by_value, -) +from tests.parse.templates import template_test_invalid from tests.parse.test_error import static_parse_context - -# -# Helper functions -# - - -# Values used by the mocked parser -class MockDirective(enum.Enum): - MockDirective = enum.auto() - - -# Mocks and tests the parse_directive parser -# Instead of parsing directives, return a mock value -# it instead returns a mock value -class MockParser(Parser): - def parse_directive(self, stream, parent_context): - read_token(stream, "MockDirective", parent_context) - return MockDirective.MockDirective - - -# A valid directive -def static_directive(): - return ([static_token_by_value("MockDirective")], MockDirective.MockDirective) +from tests.parse.test_directive import ( + static_directive_valid, + static_directive_invalid, + static_directive_invalid_error, +) # A valid file @composite def draw_file_valid(draw): - directives = draw(lists(just(static_directive()))) + directives = draw(lists(just(static_directive_valid()))) all_tokens = [] all_expected = [] for (tokens, expected) in directives: @@ -87,7 +54,7 @@ def test_parse_file_valid(test_data): (tokens, expected) = test_data stream = TokenStream(tokens.copy()) - parsed = MockParser().parse_file(stream, None) + parsed = Parser().parse_file(stream, None) assert parsed == expected assert stream.pop() is None @@ -96,21 +63,13 @@ # We expect the following behaviour: # - The error context is PARSE_FILE # - A wrong directive error is propagated -@given(draw_file_valid(), data()) -def test_parse_file_invalid(test_data, data): - (tokens, _) = test_data - assume(len(tokens) > 0) - new_token = data.draw(draw_token_random(), "error token") - assume(new_token.value != "MockDirective") - max_chosen = len(tokens) - 1 - chosen = data.draw( - integers(min_value=0, max_value=max_chosen), "error token position" - ) - new_tokens = tokens[:chosen] + [new_token] + tokens[chosen + 1 :] +@given(draw_file_valid()) +def test_parse_file_invalid(test_data): + (tokens, expected) = test_data + invalid_directive = static_directive_invalid() + new_tokens = tokens + invalid_directive + parser = Parser().parse_file parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_FILE, new_tokens[0], parent_context) - error = ParseErrorException( - ParseError.WRONG_TOKEN, new_token, "MockDirective", context - ) - parser = MockParser().parse_file + error = static_directive_invalid_error(context) template_test_invalid(parser, parent_context, new_tokens, error)