/
test_nested_functions.py
97 lines (72 loc) · 2.42 KB
/
test_nested_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
import pytest
from wemake_python_styleguide.visitors.complexity.nested import (
NESTED_FUNCTIONS_WHITELIST,
NestedComplexityVisitor,
NestedFunctionViolation,
)
nested_function = """
def container():
def {0}(): ...
"""
nested_method = """
class Raw:
def container(self):
def {0}(): ...
"""
@pytest.mark.parametrize('code', [
nested_function,
nested_method,
])
def test_nested_function(assert_errors, parse_ast_tree, code, default_options):
"""Testing that nested functions are restricted."""
tree = parse_ast_tree(code.format('nested'))
visiter = NestedComplexityVisitor(default_options)
visiter.visit(tree)
assert_errors(visiter, [NestedFunctionViolation])
@pytest.mark.parametrize('whitelist_name', NESTED_FUNCTIONS_WHITELIST)
@pytest.mark.parametrize('code', [
nested_function,
nested_method,
])
def test_whitelist_nested_functions(
assert_errors, parse_ast_tree, whitelist_name, code, default_options,
):
"""Testing that it is possible to nest whitelisted functions."""
tree = parse_ast_tree(code.format(whitelist_name))
visiter = NestedComplexityVisitor(default_options)
visiter.visit(tree)
assert_errors(visiter, [])
def test_lambda_nested_functions(
assert_errors, parse_ast_tree, default_options,
):
"""Testing that it is possible to nest lambda inside functions."""
tree = parse_ast_tree("""
def container():
lazy_value = lambda: 12
""")
visiter = NestedComplexityVisitor(default_options)
visiter.visit(tree)
assert_errors(visiter, [])
def test_lambda_nested_lambdas(assert_errors, parse_ast_tree, default_options):
"""
Testing that it is restricted to nest lambdas.
See: https://github.com/wemake-services/wemake-python-styleguide/issues/94
"""
tree = parse_ast_tree("""
def container():
nested_lambda = lambda: lambda value: value + 12
""")
visiter = NestedComplexityVisitor(default_options)
visiter.visit(tree)
assert_errors(visiter, [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
""")
visiter = NestedComplexityVisitor(default_options)
visiter.visit(tree)
assert_errors(visiter, [])