Skip to content

Commit

Permalink
test:
Browse files Browse the repository at this point in the history
added tests that checks that invalid use of class attributes
and getters and setters are prohbited.
Relates to #22, wemake-services#1054
  • Loading branch information
fwald committed Feb 26, 2020
1 parent f818784 commit 0871695
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions tests/test_visitors/test_ast/test_classes/test_getter_setter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def set_attribute(self):
get_attribute(self)
"""

class_attribute_template = """
instance_attribute_template = """
class Template(object):
def __init__(self):
self.{0}{1}{2}
Expand All @@ -94,6 +94,15 @@ def {4}(self):
...
"""

class_attribute_template = """
class Template(object):
{0}{1}
{2}
def {3}:
...
"""

class_mixed = """
class Test(object):
first: int
Expand Down Expand Up @@ -173,7 +182,7 @@ def test_nonmatching_attribute_getter_setter(
mode,
):
"""Testing that non matching attribute and getter/setter is allowed."""
test_instance = class_attribute_template.format(
test_instance = instance_attribute_template.format(
access, attribute_name, assignment, annotation, method_name,
)
tree = parse_ast_tree(mode(test_instance))
Expand Down Expand Up @@ -207,7 +216,7 @@ def test_instance_and_class_getter_setter(
mode,
):
"""Testing that instance/class attribute and getter/setter is prohibited."""
test_instance = class_attribute_template.format(
test_instance = instance_attribute_template.format(
access, attribute_name, assignment, annotation, method_name,
)
tree = parse_ast_tree(mode(test_instance))
Expand Down Expand Up @@ -260,3 +269,40 @@ def test_invalid_getter_and_setter(
visitor.run()

assert_errors(visitor, [UnpythonicGetterSetterViolation])


@pytest.mark.parametrize(('attribute_name', 'annotation', 'method_name'), [
('attribute', '', 'get_attribute()'),
('attribute', '', 'get_attribute(self)'),
('attribute', '@classmethod', 'get_attribute(self)'),
('attribute', '', 'set_attribute()'),
('attribute', '', 'set_attribute(self)'),
('attribute', '@classmethod', 'set_attribute(self)'),
])
@pytest.mark.parametrize('assignment', [
' = 1',
': int = 1',
' = other = 1',
', other = 1, 2',
])
def test_class_attributes_getter_setters(
assert_errors,
parse_ast_tree,
default_options,
attribute_name,
annotation,
method_name,
assignment,
mode,
):
"""Testing that using getter/setters with class attributes is prohibited."""
test_instance = class_attribute_template.format(
attribute_name, assignment, annotation, method_name,
)
tree = parse_ast_tree(mode(test_instance))

visitor = WrongClassVisitor(default_options, tree=tree)
visitor.run()

assert_errors(visitor, [UnpythonicGetterSetterViolation])

0 comments on commit 0871695

Please sign in to comment.