diff --git a/src/parse.py b/src/parse.py index f8970d0..0ea999a 100644 --- a/src/parse.py +++ b/src/parse.py @@ -114,10 +114,10 @@ token = stream.peek() while token is not None: # Found a note, skip it - if token.value == "StartNote": + if token.type == SyntaxType.TOKEN and token.value == "StartNote": self.skip_note(stream, context) # EndNote found outside note - elif token.value == "EndNote": + elif token.type == SyntaxType.TOKEN and token.value == "EndNote": raise ParseErrorException( ParseError.FOUND_ENDNOTE, token, None, context ) diff --git a/tests/parse/test_clear_notes.py b/tests/parse/test_clear_notes.py index c4cac61..13546a6 100644 --- a/tests/parse/test_clear_notes.py +++ b/tests/parse/test_clear_notes.py @@ -48,9 +48,9 @@ # Draws a random syntax suitable for note clearing testing @composite -def draw_clear_notes_value_token(draw): +def draw_clear_notes_value(draw): token = draw(draw_syntax_random()) - assume(token.value not in ["EndNote"]) + assume(not (token.type == SyntaxType.TOKEN and token.value == "EndNote")) return token @@ -60,7 +60,9 @@ tokens = draw(lists(draw_clear_notes_value())) output = [] for token in tokens: - if token.value != "StartNote": + # Our modified parse_note only parses the StartNote, so we expect + # the output to only remove StartNote values + if token.type != SyntaxType.TOKEN or token.value != "StartNote": output.append(token) return (tokens, output) @@ -86,7 +88,7 @@ new_tokens = tokens + [start] context = ParseContext(ParseTask.CLEAR_NOTES, new_tokens[0], parent_context) for token in new_tokens: - if token.value == value: + if token.type == SyntaxType.TOKEN and token.value == value: error = ParseErrorException(error, token, None, context) return (new_tokens, error) diff --git a/tests/parse/test_note.py b/tests/parse/test_note.py index d97d12e..e33d35b 100644 --- a/tests/parse/test_note.py +++ b/tests/parse/test_note.py @@ -39,7 +39,7 @@ @composite def draw_syntax_not_startnote(draw): token = draw(draw_syntax_token()) - assume(not (token.type == SyntaxType.TOKEN and token.value == "StartNote")) + assume(token.value != "StartNote") return token @@ -89,6 +89,19 @@ 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