# SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia <contact@jookia.org> from hypothesis import assume from hypothesis.strategies import ( composite, lists, ) from src.parse import ( NoteSkipper, ParseContext, ParseError, ParseErrorException, ParseTask, ) from src.syntax import SyntaxType from tests.parse.templates import ( insert_random_within, template_parse_invalid, template_parse_valid, ) from tests.test_syntax import ( draw_token_by_value, draw_syntax_not_token, draw_syntax_token, ) # Draws a random token suitable for note building @composite def draw_note_value_token(draw): token = draw(draw_syntax_token()) assume(token.value not in ["StartNote", "EndNote"]) return token # Draws a random syntax that isn't a StartNote token @composite def draw_syntax_not_startnote(draw): token = draw(draw_syntax_token()) assume(token.value != "StartNote") return token # Draws tokens to make a valid note @composite def draw_syntax_note_valid(draw): tokens = draw(lists(draw_note_value_token())) start = draw(draw_token_by_value("StartNote")) end = draw(draw_token_by_value("EndNote")) all_tokens = [start] + tokens + [end] return (all_tokens, None) # Tests skip_note works correctly # We expect the following behaviour: # - No value is returned # template_parse_valid provides general parsing properties @template_parse_valid(NoteSkipper().skip_note, draw_syntax_note_valid()) def test_parse_note_valid(): pass # Tests parsing notes without StartNote # We expect the following behaviour: # - Error if StartNote is not a SyntaxType.TOKEN # - Error if StartNote's token value is not "StartNote" @template_parse_invalid(NoteSkipper().skip_note) def test_parse_note_invalid_nostartnote(draw, parent_context): (tokens, _) = draw(draw_syntax_note_valid()) token = draw(draw_syntax_not_startnote()) new_tokens = [token] + tokens[1:0] context = ParseContext(ParseTask.PARSE_NOTE, new_tokens[0], parent_context) if token.type == SyntaxType.TOKEN: error = ParseErrorException(ParseError.WRONG_TOKEN, token, "StartNote", context) else: error = ParseErrorException(ParseError.NOT_TOKEN, token, None, context) return (new_tokens, error) # Tests parsing empty notes # We expect the following behaviour: # - Error if there is no StartNote token at all @template_parse_invalid(NoteSkipper().skip_note) def test_parse_note_invalid_empty(draw, parent_context): context = ParseContext(ParseTask.PARSE_NOTE, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) return ([], error) # Tests parsing note with invalid skipped tokens # We expect the following behaviour: # - Error if a skipped token is not a SyntaxType.TOKEN @template_parse_invalid(NoteSkipper().skip_note) def test_parse_note_invalid_invalidcontent(draw, parent_context): (tokens, _) = draw(draw_syntax_note_valid()) token = draw(draw_syntax_not_token()) new_tokens = insert_random_within(draw, tokens, token) context = ParseContext(ParseTask.PARSE_NOTE, new_tokens[0], parent_context) error = ParseErrorException(ParseError.NOT_TOKEN, token, None, context) return (new_tokens, error) # Tests parsing a note with a StartNote token in it # We expect the following behaviour: # - Error if a StartNote token is in the note content @template_parse_invalid(NoteSkipper().skip_note) def test_parse_note_invalid_extrastartnote(draw, parent_context): (tokens, _) = draw(draw_syntax_note_valid()) start = draw(draw_token_by_value("StartNote")) new_tokens = insert_random_within(draw, tokens, start) context = ParseContext(ParseTask.PARSE_NOTE, new_tokens[0], parent_context) error = ParseErrorException(ParseError.FOUND_STARTNOTE, start, None, context) return (new_tokens, error) # Tests parsing a note without an EndNote token # We expect the following behaviour: # - Error if there is no EndNote token at all @template_parse_invalid(NoteSkipper().skip_note) def test_parse_note_invalid_noendnote(draw, parent_context): (tokens, _) = draw(draw_syntax_note_valid()) context = ParseContext(ParseTask.PARSE_NOTE, tokens[0], parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) return (tokens[0:-1], error)