diff --git a/tests/parse/templates.py b/tests/parse/templates.py index d718e18..169b09c 100644 --- a/tests/parse/templates.py +++ b/tests/parse/templates.py @@ -10,6 +10,18 @@ from tests.test_token import static_token_by_value +# Draws tokens with an element randomly between the first and last token +# Returns the new list and new token +@composite +def draw_random_within(draw, source, new): + list = draw(source) + data = static_token_by_value(new) + pos = draw(integers(min_value=1, max_value=(len(list) - 1))) + new_data = list[0:pos] + [data] + list[pos:] + return (new_data, data) + + +# Deprecated: Do not use # Inserts an element randomly between the first and last token of a list def insert_random_within(draw, list, data): pos = draw(integers(min_value=1, max_value=(len(list) - 1))) diff --git a/tests/parse/test_note.py b/tests/parse/test_note.py index 5a7b46b..07b0d0c 100644 --- a/tests/parse/test_note.py +++ b/tests/parse/test_note.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-2.1-only # Copyright 2022 Jookia -from hypothesis import assume +from hypothesis import assume, given from hypothesis.strategies import ( composite, lists, @@ -15,10 +15,11 @@ ParseTask, ) from tests.parse.templates import ( - insert_random_within, - template_parse_invalid, - template_parse_valid, + draw_random_within, + template_test_invalid, + template_test_valid, ) +from tests.parse.test_error import static_parse_context from tests.test_token import ( static_token_by_value, draw_token_random, @@ -48,16 +49,16 @@ start = static_token_by_value("StartNote") end = static_token_by_value("EndNote") all_tokens = [start] + tokens + [end] - return (all_tokens, None) + return all_tokens # 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_token_note_valid()) -def test_parse_note_valid(): - pass +@given(draw_token_note_valid()) +def test_parse_note_valid(tokens): + template_test_valid(NoteSkipper().skip_note, tokens, None) # Tests parsing notes without StartNote @@ -65,14 +66,13 @@ # - Error if StartNote's token value is not "StartNote" # - Have ParseError.WRONG_TOKEN as the exception code # - Have ParseTask.PARSE_NOTE as the context's parse task -@template_parse_invalid(NoteSkipper().skip_note) -def test_parse_note_invalid_nostartnote(draw, parent_context): - (tokens, _) = draw(draw_token_note_valid()) - token = draw(draw_token_not_startnote()) +@given(draw_token_note_valid(), draw_token_not_startnote()) +def test_parse_note_invalid_nostartnote(tokens, token): new_tokens = [token] + tokens[1:0] + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_NOTE, new_tokens[0], parent_context) error = ParseErrorException(ParseError.WRONG_TOKEN, token, "StartNote", context) - return (new_tokens, error) + template_test_invalid(NoteSkipper().skip_note, parent_context, new_tokens, error) # Tests parsing empty notes @@ -80,25 +80,24 @@ # - Error if there is no StartNote token at all # - Have ParseError.NO_TOKEN as the exception code # - Have ParseTask.PARSE_NOTE as the context's parse task -@template_parse_invalid(NoteSkipper().skip_note) -def test_parse_note_invalid_empty(draw, parent_context): +def test_parse_note_invalid_empty(): + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_NOTE, None, parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) - return ([], error) + template_test_invalid(NoteSkipper().skip_note, parent_context, [], 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 # - Have ParseTask.PARSE_NOTE as the context's parse task -@template_parse_invalid(NoteSkipper().skip_note) -def test_parse_note_invalid_extrastartnote(draw, parent_context): - (tokens, _) = draw(draw_token_note_valid()) - start = static_token_by_value("StartNote") - new_tokens = insert_random_within(draw, tokens, start) - context = ParseContext(ParseTask.PARSE_NOTE, new_tokens[0], parent_context) +@given(draw_random_within(draw_token_note_valid(), "StartNote")) +def test_parse_note_invalid_extrastartnote(within): + (tokens, start) = within + parent_context = static_parse_context() + context = ParseContext(ParseTask.PARSE_NOTE, tokens[0], parent_context) error = ParseErrorException(ParseError.FOUND_STARTNOTE, start, None, context) - return (new_tokens, error) + template_test_invalid(NoteSkipper().skip_note, parent_context, tokens, error) # Tests parsing a note without an EndNote token @@ -106,9 +105,10 @@ # - Error if there is no EndNote token at all # - Have ParseError.NO_TOKEN as the exception code # - Have ParseTask.PARSE_NOTE as the context's parse task -@template_parse_invalid(NoteSkipper().skip_note) -def test_parse_note_invalid_noendnote(draw, parent_context): - (tokens, _) = draw(draw_token_note_valid()) +@given(draw_token_note_valid()) +def test_parse_note_invalid_noendnote(tokens): + new_tokens = tokens[0:-1] + parent_context = static_parse_context() context = ParseContext(ParseTask.PARSE_NOTE, tokens[0], parent_context) error = ParseErrorException(ParseError.NO_TOKEN, None, None, context) - return (tokens[0:-1], error) + template_test_invalid(NoteSkipper().skip_note, parent_context, new_tokens, error)