diff --git a/tests/test_parse.py b/tests/test_parse.py index 10fe87a..af661d6 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -7,6 +7,7 @@ characters, lists, composite, + randoms, ) from src import parse @@ -98,6 +99,26 @@ ) +# Draws a textual identifier consisting of random characters and reserved words +@composite +def draw_identifier(draw): + identifiers = draw( + lists( + text(alphabet=characters(blacklist_characters=lexer_whitespace), min_size=1) + ) + ) + # If we have no identifiers, draw at least two words so we don't accidentally + # draw a reserved word alone. + min_words = 2 if len(identifiers) == 0 else 0 + words = draw(lists(sampled_from(reserved_words), min_size=min_words)) + all_words = identifiers + words + draw(randoms()).shuffle(all_words) + value = "".join(all_words) + assume(value not in reserved_words) # Reserved words aren't symbols + assume(not value.startswith("#!")) # Shebangs aren't symbols + return value + + # Generates a Text token @composite def draw_token_text(draw): @@ -131,11 +152,7 @@ # Generates a symbol token @composite def draw_token_symbol(draw): - symbol = draw( - text(alphabet=characters(blacklist_characters=lexer_whitespace), min_size=1) - ) - assume(symbol not in reserved_words) # Reserved words aren't symbols - assume(not symbol.startswith("#!")) # Shebangs aren't symbols + symbol = draw(draw_identifier()) return SampleToken(symbol, "symbol", symbol)