diff --git a/tests/test_parse.py b/tests/test_parse.py index 592afe0..1a84f39 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -8,74 +8,17 @@ integers, lists, one_of, - text, - sampled_from, ) from src import parse, tokenize from tests import test_tokenize -# Draws a random syntax type -@composite -def draw_syntax_type(draw): - return draw(sampled_from(list(tokenize.SyntaxType))) - - -# Draws a token syntax value -@composite -def draw_syntax_token(draw): - value = draw(test_tokenize.draw_token_classified()) - location = draw(test_tokenize.draw_token_location()) - type = tokenize.SyntaxType.TOKEN - return tokenize.Syntax(value.value, location, type) - - -# Draws a text syntax value -@composite -def draw_syntax_text(draw): - value = draw(text()) - location = draw(test_tokenize.draw_token_location()) - type = tokenize.SyntaxType.TEXT - return tokenize.Syntax(value, location, type) - - -# Draws a random syntax -@composite -def draw_syntax_random(draw): - strategies = [ - draw_syntax_token(), - draw_syntax_text(), - ] - return draw(one_of(strategies)) - - -# Test syntax getters -@given(text(), test_tokenize.draw_token_location(), draw_syntax_type()) -def test_parse_syntax_getters(value, location, type): - # Use text as a somewhat random value - test = tokenize.Syntax(value, location, type) - assert test.value == value - assert test.location == location - assert test.type == type - - -# Test syntax equals -@given(draw_syntax_random(), draw_syntax_random()) -def test_parse_syntax_equality(syntax1, syntax2): - equals = ( - syntax1.type == syntax2.type - and syntax1.value == syntax2.value - and syntax1.location == syntax2.location - ) - assert (syntax1 == syntax2) == equals - - # Tests that a syntax stream reads items correctly # We expect the following behaviour: # - All items are popped in order # - None is returned at the end of the stream -@given(lists(draw_syntax_random())) +@given(lists(test_tokenize.draw_syntax_random())) def test_parse_syntax_stream(nodes): stream = parse.SyntaxStream(nodes.copy()) read = [] @@ -90,7 +33,7 @@ # Draws syntax and a syntax without whitespace in it @composite def draw_syntax_whitespace(draw): - input = draw(lists(draw_syntax_random())) + input = draw(lists(test_tokenize.draw_syntax_random())) syntax = [] for s in input: if s.type != tokenize.SyntaxType.TOKEN or s.value not in ["\n", " "]: @@ -111,7 +54,7 @@ # Draws a random token suitable for text building @composite def draw_text_value_token(draw): - token = draw(draw_syntax_token()) + token = draw(test_tokenize.draw_syntax_token()) assume(token.value not in ["StartText", "EndText"]) return token @@ -147,7 +90,7 @@ # - The Syntax's value is the resulting text # - The Syntax's type is SyntaxType.TEXT # - The Syntax's location is the StartText location -@given(draw_syntax_random(), draw_syntax_text_valid()) +@given(test_tokenize.draw_syntax_random(), draw_syntax_text_valid()) def test_parse_text_valid(canary, test_data): (tokens, result) = test_data stream = parse.SyntaxStream(tokens + [canary]) @@ -167,7 +110,7 @@ def draw_syntax_text_invalid_nostarttext(draw): (tokens, _) = draw(draw_syntax_text_valid()) if draw(booleans()): - token = draw(draw_syntax_random()) + token = draw(test_tokenize.draw_syntax_random()) assume( not (token.type == tokenize.SyntaxType.TOKEN and token.value == "StartText") ) @@ -183,7 +126,7 @@ @composite def draw_syntax_text_invalid_invalidcontent(draw): (tokens, _) = draw(draw_syntax_text_valid()) - token = draw(draw_syntax_random()) + token = draw(test_tokenize.draw_syntax_random()) assume(token.type != tokenize.SyntaxType.TOKEN) pos = draw(integers(min_value=1, max_value=(len(tokens) - 1))) new_tokens = tokens[0:pos] + [token] + tokens[pos:] diff --git a/tests/test_tokenize.py b/tests/test_tokenize.py index 0a8858a..85e8f47 100644 --- a/tests/test_tokenize.py +++ b/tests/test_tokenize.py @@ -44,6 +44,61 @@ return tokenize.SyntaxLocation(line, column, filename) +# Draws a random syntax type +@composite +def draw_syntax_type(draw): + return draw(sampled_from(list(tokenize.SyntaxType))) + + +# Draws a token syntax value +@composite +def draw_syntax_token(draw): + value = draw(draw_token_classified()) + location = draw(draw_token_location()) + type = tokenize.SyntaxType.TOKEN + return tokenize.Syntax(value.value, location, type) + + +# Draws a text syntax value +@composite +def draw_syntax_text(draw): + value = draw(text()) + location = draw(draw_token_location()) + type = tokenize.SyntaxType.TEXT + return tokenize.Syntax(value, location, type) + + +# Draws a random syntax +@composite +def draw_syntax_random(draw): + strategies = [ + draw_syntax_token(), + draw_syntax_text(), + ] + return draw(one_of(strategies)) + + +# Test syntax getters +@given(text(), draw_token_location(), draw_syntax_type()) +def test_tokenize_syntax_getters(value, location, type): + # Use text as a somewhat random value + test = tokenize.Syntax(value, location, type) + assert test.value == value + assert test.location == location + assert test.type == type + + +# Test syntax equals +@given(draw_syntax_random(), draw_syntax_random()) +def test_tokenize_syntax_equality(syntax1, syntax2): + equals = ( + syntax1.type == syntax2.type + and syntax1.value == syntax2.value + and syntax1.location == syntax2.location + ) + assert (syntax1 == syntax2) == equals + + # Draws a random token @composite def draw_token_random(draw):