From 8287f948d4a42548951405f61597450ebd77c71a Mon Sep 17 00:00:00 2001 From: brns Date: Tue, 12 Mar 2024 22:33:13 +0100 Subject: [PATCH] feat: cognitive complexity - fix test cases for older versions --- .../contrib/test_cognitive_complexity.py | 96 +++++++++++-------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/radon/tests/contrib/test_cognitive_complexity.py b/radon/tests/contrib/test_cognitive_complexity.py index 0da5b77..907fcb8 100644 --- a/radon/tests/contrib/test_cognitive_complexity.py +++ b/radon/tests/contrib/test_cognitive_complexity.py @@ -7,6 +7,7 @@ import pytest from radon.contrib.cognitive_complexity import CognitiveComplexityVisitor + from radon.visitors import * dedent = lambda code: textwrap.dedent(code).strip() @@ -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): @@ -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 @@ -663,5 +677,3 @@ def f(self): -#if sys.version_info[:2] >= (3, 10): -# BLOCKS.extend(MATCH_STATEMENT_BLOCKS)