# SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia <contact@jookia.org> # Clear notes syntax consists of the following: # - One or more tokens or notes # # Parsing gives the following: # All tokens that aren't notes # # The following error contexts are used: # CLEAR_NOTES - Used when parsing the file # # The following parse errors are generated: # FOUND_ENDNOTE - When a stray EndNote token is found from hypothesis import given from hypothesis.strategies import composite, just, lists, one_of from newlang.parse2.parse import ( clear_notes, ParseContext, ParseError, ParseErrorException, ParseTask, ) from newlang.parse2.token import TokenStream from tests.parse2.templates import template_test_invalid from tests.parse2.test_error import static_parse_context from tests.parse2.test_note import ( draw_note_value_token, static_note_tokens, static_note_invalid, static_note_invalid_error, ) from tests.parse2.test_token import static_token_by_value # Draws a tokens of notes and non-notes and output without notes @composite def draw_notes_to_clear(draw): token_sets = draw( lists(one_of([lists(draw_note_value_token()), just(static_note_tokens())])) ) output = [] tokens = [] for set in token_sets: tokens += set if set != static_note_tokens(): output += set return (tokens, output) # Tests clear_notes filters out notes # We expect the following behaviour: # - Tokens that are part of note structures are removed # template_test provides general parsing properties @given(draw_notes_to_clear()) def test_parse2_clear_notes_valid(test_data): (tokens, result) = test_data stream = TokenStream(tokens) cleared = clear_notes(stream, None) assert cleared == result # Tests clear_notes passes through note errors # We expect the following behaviour: # - When an invalid note is parsed the error is propagated # - Have ParseTask.CLEAR_NOTES as the context's parse task def test_parse2_clear_notes_startnote_propagation(): tokens = static_note_invalid() parent_context = static_parse_context() context = ParseContext(ParseTask.CLEAR_NOTES, tokens[0], parent_context) error = static_note_invalid_error(context) template_test_invalid(clear_notes, parent_context, tokens, error) # Tests clear_notes errors when finding an EndNote # We expect the following behaviour: # - When EndNote is found a ParseError.FOUND_ENDNOTE error is raised # - Have ParseTask.CLEAR_NOTES as the context's parse task @given(lists(draw_note_value_token())) def test_parse2_clear_notes_invalid_endnote(tokens): token = static_token_by_value("EndNote") new_tokens = tokens + [token] parent_context = static_parse_context() context = ParseContext(ParseTask.CLEAR_NOTES, new_tokens[0], parent_context) error = ParseErrorException(ParseError.FOUND_ENDNOTE, token, None, context) template_test_invalid(clear_notes, parent_context, new_tokens, error)