diff --git a/tests/parse/test_parse.py b/tests/parse/test_parse.py index 2ba5517..6dee641 100644 --- a/tests/parse/test_parse.py +++ b/tests/parse/test_parse.py @@ -18,6 +18,7 @@ parse, ) from src.syntax import SyntaxStream +from tests.templates import template_test_structure from tests.test_syntax import draw_token_classified, draw_syntax_random @@ -36,24 +37,16 @@ return ParseContext(task, syntax, parent) -# Test parse context getters -@given(draw_parse_task(), draw_syntax_random(), text()) -def test_parse_context_getters(task, syntax, parent): - test = ParseContext(task, syntax, parent) - assert test.task == task - assert test.syntax == syntax - assert test.parent == parent - - -# Test parse context equals -@given(draw_parse_context(), draw_parse_context()) -def test_parse_context_equality(context1, context2): - equals = ( - context1.task == context2.task - and context1.syntax == context2.syntax - and context1.parent == context2.parent - ) - assert (context1 == context2) == equals +# Test parse context structure +@template_test_structure( + ParseContext, + draw_parse_context(), + task=draw_parse_task(), + syntax=draw_syntax_random(), + parent=text(), +) +def test_parse_context_structure(): + pass # Draws a random parse error @@ -72,26 +65,17 @@ return ParseErrorException(error, syntax, expected, context) -# Test parse error exception getters -@given(draw_parse_error(), draw_syntax_random(), text(), draw_parse_context()) -def test_parse_error_getters(error, syntax, expected, context): - test = ParseErrorException(error, syntax, expected, context) - assert test.error == error - assert test.syntax == syntax - assert test.expected == expected - assert test.context == context - - -# Test parse error exception equals -@given(draw_parse_error_exception(), draw_parse_error_exception()) -def test_parse_error_equality(except1, except2): - equals = ( - except1.error == except2.error - and except1.syntax == except2.syntax - and except1.expected == except2.expected - and except1.context == except2.context - ) - assert (except1 == except2) == equals +# Test parse error exception structure +@template_test_structure( + ParseErrorException, + draw_parse_error_exception(), + error=draw_syntax_random(), + syntax=draw_syntax_random(), + expected=text(), + context=draw_parse_context(), +) +def test_parse_error_exception_structure(): + pass # Tests the parser wrapper works correctly diff --git a/tests/templates.py b/tests/templates.py new file mode 100644 index 0000000..4f17c18 --- /dev/null +++ b/tests/templates.py @@ -0,0 +1,40 @@ +# SPDX-License-Identifier: LGPL-2.1-only +# Copyright 2022 Jookia + +from hypothesis import given + + +# Tests that a class's getters work +def do_template_test_getters(class_name, members): + test = class_name(**members) + for name, expected in members.items(): + value = getattr(test, name) + if value != expected: + raise AssertionError("%s doesn't match: %s != %s" % (name, value, expected)) + + +# Tests that a class's equality method works +def do_template_test_equals(value1, value2, members): + equals = True + for name in members: + member1 = getattr(value1, name) + member2 = getattr(value2, name) + equals = equals and (member1 == member2) + assert (value1 == value2) == equals + + +# Creates a Hypothesis test that tests class getters and equals +def template_test_structure(class_name, draw, **kwargs): + @given(**kwargs) + def do_getters(**args): + do_template_test_getters(class_name, args) + + @given(draw, draw) + def do_equals(value1, value2): + do_template_test_equals(value1, value2, kwargs.keys()) + + def do_all(): + do_getters() + do_equals() + + return lambda func: do_all diff --git a/tests/test_syntax.py b/tests/test_syntax.py index ea20531..f6446dd 100644 --- a/tests/test_syntax.py +++ b/tests/test_syntax.py @@ -14,6 +14,7 @@ ) from src.syntax import Syntax, SyntaxLocation, SyntaxStream, SyntaxType +from tests.templates import template_test_structure # Keywords recognized by the language keywords = [ @@ -41,24 +42,16 @@ return SyntaxLocation(line, offset, filename) -# Test location getters -@given(integers(), integers(), text()) -def test_syntax_location_getters(line, offset, filename): - test = SyntaxLocation(line, offset, filename) - assert test.line == line - assert test.offset == offset - assert test.file == filename - - -# Test location equals -@given(draw_syntax_location(), draw_syntax_location()) -def test_syntax_location_equality(location1, location2): - equals = ( - location1.line == location2.line - and location1.offset == location2.offset - and location1.file == location2.file - ) - assert (location1 == location2) == equals +# Test syntax location structure +@template_test_structure( + SyntaxLocation, + draw_syntax_location(), + line=integers(), + offset=integers(), + file=text(), +) +def test_syntax_location_structure(): + pass # Draws a random syntax type @@ -200,25 +193,16 @@ return draw(one_of(strategies)) -# Test syntax getters -@given(text(), draw_syntax_location(), draw_syntax_type()) -def test_syntax_syntax_getters(value, location, type): - # Use text as a somewhat random value - test = 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_syntax_syntax_equality(syntax1, syntax2): - equals = ( - syntax1.type == syntax2.type - and syntax1.value == syntax2.value - and syntax1.location == syntax2.location - ) - assert (syntax1 == syntax2) == equals +# Test syntax structure +@template_test_structure( + Syntax, + draw_syntax_random(), + value=text(), + location=draw_syntax_location(), + type=draw_syntax_type(), +) +def test_syntax_syntax_structure(): + pass # Tests that a syntax stream pops items correctly