Skip to content

Commit

Permalink
feat: cognitive complexity - fix test cases for older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
brns committed Mar 12, 2024
1 parent c8f0d22 commit 8287f94
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions radon/tests/contrib/test_cognitive_complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from radon.contrib.cognitive_complexity import CognitiveComplexityVisitor

from radon.visitors import *

dedent = lambda code: textwrap.dedent(code).strip()
Expand Down Expand Up @@ -40,45 +41,6 @@ def wrapped_f(*args):

return wrapped_f


@assert_complexity(1)
def test_match():
point = (5, 5)
match point: # +1
case (0, 0):
print("Origin")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")


@assert_complexity(2)
def test_match_adds_nested():
status = 200
if status:
match status: # +2 (nesting=1)
case 400:
return "Bad request"
case 200:
return "Good request"
case _:
return "Something's wrong with the internet"


@assert_complexity(4)
def test_match_increases_nesting():
point = (5, 5)
match point if point else (0,0): # + 1 + 1 (nesting=0)
case (0, 0):
print("Origin")
case (0, y):
if y > 5 : # + 2 (nesting=1)
print(f"Y={y}")
case _:
raise ValueError("Not a point")




class IfElseTestCase(unittest.TestCase):
Expand Down Expand Up @@ -557,8 +519,60 @@ async def f(a, b):
]


MATCH_STATEMENT_BLOCKS = [
(
'''
def test_match():
point = (5, 5)
match point: # +1
case (0, 0):
print("Origin")
case (x, y):
print(f"X={x}, Y={y}")
case _:
raise ValueError("Not a point")
''', (0,1)

),
('''
def test_match_adds_nested():
status = 200
if status:
match status: # +2 (nesting=1)
case 400:
return "Bad request"
case 200:
return "Good request"
case _:
return "Something's wrong with the internet"
''', (0, 2)),

(
'''
def test_match_increases_nesting():
point = (5, 5)
match point if point else (0,0): # + 1 + 1 (nesting=0)
case (0, 0):
print("Origin")
case (0, y):
if y > 5 : # + 2 (nesting=1)
print(f"Y={y}")
case _:
raise ValueError("Not a point")
''', (0,4)
)
]

if sys.version_info[:2] >= (3, 10):
SINGLE_FUNCTIONS_CASES.extend(MATCH_STATEMENT_BLOCKS)





@pytest.mark.parametrize("code,expected", SINGLE_FUNCTIONS_CASES)
def test_visitor_async_functions(code, expected):
def test_visitor_single_functions(code, expected):
visitor = CognitiveComplexityVisitor.from_code(dedent(code))
print(vars(visitor))
assert len(visitor.functions) == 1
Expand Down Expand Up @@ -663,5 +677,3 @@ def f(self):



#if sys.version_info[:2] >= (3, 10):
# BLOCKS.extend(MATCH_STATEMENT_BLOCKS)

0 comments on commit 8287f94

Please sign in to comment.