Skip to content

Commit

Permalink
Closes #191, closes #174
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Oct 4, 2018
1 parent 0cb400d commit b5e8917
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 70 deletions.
6 changes: 6 additions & 0 deletions tests/fixtures/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
def some(): # noqa: Z110, Z113
from my_module import some_function # noqa: Z435

class Nested(object): ... # noqa: 431

def nested(): ... # noqa: Z430


Expand All @@ -36,6 +38,10 @@ class BadClass: # noqa: Z306
def some_static():
...

@staticmethod # noqa: Z433
async def some_async_static():
...

def __del__(self, *args, **kwargs): # noqa: Z434
...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,39 @@
)

nested_class = """
class Parent:
class {0}: ...
class Parent(object):
class {0}(object): ...
"""

nested_class_in_method = """
class Parent:
class Parent(object):
def container(self):
class {0}: ...
class {0}(object): ...
"""

nested_class_in_async_method = """
class Parent(object):
async def container(self):
class {0}(object): ...
"""

nested_class_in_function = """
def container():
class {0}: ...
class {0}(object): ...
"""

nested_class_in_async_function = """
async def container():
class {0}(object): ...
"""


@pytest.mark.parametrize('code', [
nested_class,
nested_class_in_method,
nested_class_in_async_method,
nested_class_in_function,
nested_class_in_async_function,
])
def test_nested_class(assert_errors, parse_ast_tree, code, default_options):
"""Testing that nested classes are restricted."""
Expand Down Expand Up @@ -64,7 +77,9 @@ def test_whitelist_nested_classes(
])
@pytest.mark.parametrize('code', [
nested_class_in_method,
nested_class_in_async_method,
nested_class_in_function,
nested_class_in_async_function,
])
def test_whitelist_nested_classes_in_functions(
assert_errors, parse_ast_tree, whitelist_name, code, default_options,
Expand All @@ -81,7 +96,10 @@ def test_whitelist_nested_classes_in_functions(
def test_ordinary_class(assert_errors, parse_ast_tree, default_options):
"""Testing that it is possible to write basic classes."""
tree = parse_ast_tree("""
class Ordinary:
class Ordinary(object):
def method(self): ...
class Second(Ordinary):
def method(self): ...
""")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,88 @@
NestedComplexityVisitor,
)

nested_function = """
# Functions:

nested_function_in_function = """
def container():
def {0}(): ...
"""

nested_method = """
class Raw:
nested_function_in_async_function = """
async def container():
def {0}(): ...
"""

nested_async_function_in_async_function = """
async def container():
async def {0}(): ...
"""

nested_async_function_in_function = """
def container():
async def {0}(): ...
"""

# Methods:

nested_function_in_method = """
class Raw(object):
def container(self):
def {0}(): ...
"""

nested_function_in_async_method = """
class Raw(object):
async def container(self):
def {0}(): ...
"""

nested_async_function_in_async_method = """
class Raw(object):
async def container(self):
async def {0}(): ...
"""

nested_async_function_in_method = """
class Raw(object):
def container(self):
async def {0}(): ...
"""

# Lambdas:

lambda_in_function = """
def container():
lazy_value = lambda: 12
"""

lambda_in_async_function = """
async def container():
lazy_value = lambda: 12
"""

lambda_in_method = """
class Raw(object):
def container(self):
lazy_value = lambda: 12
"""

lambda_in_async_method = """
class Raw(object):
def container(self):
lazy_value = lambda: 12
"""


@pytest.mark.parametrize('code', [
nested_function,
nested_method,
nested_function_in_function,
nested_async_function_in_function,
nested_function_in_async_function,
nested_async_function_in_async_function,
nested_function_in_method,
nested_async_function_in_method,
nested_function_in_async_method,
nested_async_function_in_async_method,
])
def test_nested_function(assert_errors, parse_ast_tree, code, default_options):
"""Testing that nested functions are restricted."""
Expand All @@ -38,8 +105,14 @@ def test_nested_function(assert_errors, parse_ast_tree, code, default_options):

@pytest.mark.parametrize('whitelist_name', NESTED_FUNCTIONS_WHITELIST)
@pytest.mark.parametrize('code', [
nested_function,
nested_method,
nested_function_in_function,
nested_async_function_in_function,
nested_function_in_async_function,
nested_async_function_in_async_function,
nested_function_in_method,
nested_async_function_in_method,
nested_function_in_async_method,
nested_async_function_in_async_method,
])
def test_whitelist_nested_functions(
assert_errors, parse_ast_tree, whitelist_name, code, default_options,
Expand All @@ -53,14 +126,17 @@ def test_whitelist_nested_functions(
assert_errors(visitor, [])


@pytest.mark.parametrize('code', [
lambda_in_function,
lambda_in_async_function,
lambda_in_method,
lambda_in_async_method,
])
def test_lambda_nested_functions(
assert_errors, parse_ast_tree, default_options,
assert_errors, parse_ast_tree, code, default_options,
):
"""Testing that it is possible to nest lambda inside functions."""
tree = parse_ast_tree("""
def container():
lazy_value = lambda: 12
""")
tree = parse_ast_tree(code)

visitor = NestedComplexityVisitor(default_options, tree=tree)
visitor.run()
Expand All @@ -83,17 +159,3 @@ def container():
visitor.run()

assert_errors(visitor, [NestedFunctionViolation])


def test_lambda_nested_method(assert_errors, parse_ast_tree, default_options):
"""Testing that it is possible to nest lambda inside methods."""
tree = parse_ast_tree("""
class Raw:
def container(self):
lazy_value = lambda: 12
""")

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

assert_errors(visitor, [])
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,52 @@ class Example(object):
def {0}(): ...
"""

async_magic_method = """
class Example(object):
async def {0}(): ...
"""


@pytest.mark.parametrize('code', [
magic_method,
async_magic_method,
])
@pytest.mark.parametrize('method', BAD_MAGIC_METHODS)
def test_wrong_magic_used(
assert_errors, parse_ast_tree, method, default_options,
assert_errors,
parse_ast_tree,
code,
method,
default_options,
):
"""Testing that some magic methods are restricted."""
tree = parse_ast_tree(magic_method.format(method))
tree = parse_ast_tree(code.format(method))

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

assert_errors(visitor, [BadMagicMethodViolation])


@pytest.mark.parametrize('code', [
magic_method,
async_magic_method,
])
@pytest.mark.parametrize('method', [
'__add__',
'__init__',
'next',
'regular',
])
def test_regular_method_used(
assert_errors, parse_ast_tree, method, default_options,
assert_errors,
parse_ast_tree,
code,
method,
default_options,
):
"""Testing that other methods are working fine."""
tree = parse_ast_tree(magic_method.format(method))
tree = parse_ast_tree(code.format(method))

visitor = WrongClassVisitor(default_options, tree=tree)
visitor.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,50 @@ class Example(object):
def should_fail(): ...
"""

async_decorated_method = """
class Example(object):
@{0}
async def should_fail(): ...
"""


def test_staticmethod_used(assert_errors, parse_ast_tree, default_options):
@pytest.mark.parametrize('code', [
decorated_method,
async_decorated_method,
])
def test_staticmethod_used(
assert_errors,
parse_ast_tree,
code,
default_options,
):
"""Testing that some built-in functions are restricted as decorators."""
tree = parse_ast_tree(decorated_method.format('staticmethod'))
tree = parse_ast_tree(code.format('staticmethod'))

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

assert_errors(visitor, [StaticMethodViolation])


@pytest.mark.parametrize('code', [
decorated_method,
async_decorated_method,
])
@pytest.mark.parametrize('decorator', [
'classmethod',
'custom',
'with_params(12, 100)',
])
def test_regular_decorator_used(
assert_errors, parse_ast_tree, decorator, default_options,
assert_errors,
parse_ast_tree,
decorator,
code,
default_options,
):
"""Testing that other decorators are allowed."""
tree = parse_ast_tree(decorated_method.format(decorator))
tree = parse_ast_tree(code.format(decorator))

visitor = WrongClassVisitor(default_options, tree=tree)
visitor.run()
Expand Down
Loading

0 comments on commit b5e8917

Please sign in to comment.