1616
1717import ast
1818import pytest
19+ import textwrap as tw
1920from scripts .microgenerator .generate import parse_code , CodeAnalyzer
2021
2122# --- Tests CodeAnalyzer handling of Imports ---
@@ -274,10 +275,12 @@ class YetAnotherClass:
274275
275276
276277def test_codeanalyzer_finds_class ():
277- code = """
278- class MyClass:
279- pass
280- """
278+ code = tw .dedent (
279+ """
280+ class MyClass:
281+ pass
282+ """
283+ )
281284 analyzer = CodeAnalyzer ()
282285 tree = ast .parse (code )
283286 analyzer .visit (tree )
@@ -286,14 +289,16 @@ class MyClass:
286289
287290
288291def test_codeanalyzer_finds_multiple_classes ():
289- code = """
290- class ClassA:
291- pass
292+ code = tw .dedent (
293+ """
294+ class ClassA:
295+ pass
292296
293297
294- class ClassB:
295- pass
296- """
298+ class ClassB:
299+ pass
300+ """
301+ )
297302 analyzer = CodeAnalyzer ()
298303 tree = ast .parse (code )
299304 analyzer .visit (tree )
@@ -303,11 +308,13 @@ class ClassB:
303308
304309
305310def test_codeanalyzer_finds_method ():
306- code = """
307- class MyClass:
308- def my_method(self):
309- pass
310- """
311+ code = tw .dedent (
312+ """
313+ class MyClass:
314+ def my_method(self):
315+ pass
316+ """
317+ )
311318 analyzer = CodeAnalyzer ()
312319 tree = ast .parse (code )
313320 analyzer .visit (tree )
@@ -317,14 +324,16 @@ def my_method(self):
317324
318325
319326def test_codeanalyzer_finds_multiple_methods ():
320- code = """
321- class MyClass:
322- def method_a(self):
323- pass
324-
325- def method_b(self):
326- pass
327- """
327+ code = tw .dedent (
328+ """
329+ class MyClass:
330+ def method_a(self):
331+ pass
332+
333+ def method_b(self):
334+ pass
335+ """
336+ )
328337 analyzer = CodeAnalyzer ()
329338 tree = ast .parse (code )
330339 analyzer .visit (tree )
@@ -334,21 +343,25 @@ def method_b(self):
334343
335344
336345def test_codeanalyzer_no_classes ():
337- code = """
338- def top_level_function():
339- pass
340- """
346+ code = tw .dedent (
347+ """
348+ def top_level_function():
349+ pass
350+ """
351+ )
341352 analyzer = CodeAnalyzer ()
342353 tree = ast .parse (code )
343354 analyzer .visit (tree )
344355 assert len (analyzer .structure ) == 0
345356
346357
347358def test_codeanalyzer_class_with_no_methods ():
348- code = """
349- class MyClass:
350- attribute = 123
351- """
359+ code = tw .dedent (
360+ """
361+ class MyClass:
362+ attribute = 123
363+ """
364+ )
352365 analyzer = CodeAnalyzer ()
353366 tree = ast .parse (code )
354367 analyzer .visit (tree )
@@ -360,72 +373,108 @@ class MyClass:
360373# --- Test Data for Parameterization ---
361374TYPE_TEST_CASES = [
362375 pytest .param (
363- """class TestClass:
364- def func(self, a: int, b: str) -> bool: return True""" ,
376+ tw .dedent (
377+ """
378+ class TestClass:
379+ def func(self, a: int, b: str) -> bool: return True
380+ """
381+ ),
365382 [("a" , "int" ), ("b" , "str" )],
366383 "bool" ,
367384 id = "simple_types" ,
368385 ),
369386 pytest .param (
370- """from typing import Optional
371- class TestClass:
372- def func(self, a: Optional[int]) -> str | None: return 'hello'""" ,
387+ tw .dedent (
388+ """
389+ from typing import Optional
390+ class TestClass:
391+ def func(self, a: Optional[int]) -> str | None: return 'hello'
392+ """
393+ ),
373394 [("a" , "Optional[int]" )],
374395 "str | None" ,
375396 id = "optional_union_none" ,
376397 ),
377398 pytest .param (
378- """from typing import Union
379- class TestClass:
380- def func(self, a: int | float, b: Union[str, bytes]) -> None: pass""" ,
399+ tw .dedent (
400+ """
401+ from typing import Union
402+ class TestClass:
403+ def func(self, a: int | float, b: Union[str, bytes]) -> None: pass
404+ """
405+ ),
381406 [("a" , "int | float" ), ("b" , "Union[str, bytes]" )],
382407 "None" ,
383408 id = "union_types" ,
384409 ),
385410 pytest .param (
386- """from typing import List, Dict, Tuple
387- class TestClass:
388- def func(self, a: List[int], b: Dict[str, float]) -> Tuple[int, str]: return (1, 'a')""" ,
411+ tw .dedent (
412+ """
413+ from typing import List, Dict, Tuple
414+ class TestClass:
415+ def func(self, a: List[int], b: Dict[str, float]) -> Tuple[int, str]: return (1, 'a')
416+ """
417+ ),
389418 [("a" , "List[int]" ), ("b" , "Dict[str, float]" )],
390419 "Tuple[int, str]" ,
391420 id = "generic_types" ,
392421 ),
393422 pytest .param (
394- """import datetime
395- from scripts.microgenerator.tests.unit.test_generate_analyzer import MyClass
396- class TestClass:
397- def func(self, a: datetime.date, b: MyClass) -> MyClass: return b""" ,
423+ tw .dedent (
424+ """
425+ import datetime
426+ from scripts.microgenerator.tests.unit.test_generate_analyzer import MyClass
427+ class TestClass:
428+ def func(self, a: datetime.date, b: MyClass) -> MyClass: return b
429+ """
430+ ),
398431 [("a" , "datetime.date" ), ("b" , "MyClass" )],
399432 "MyClass" ,
400433 id = "imported_types" ,
401434 ),
402435 pytest .param (
403- """from scripts.microgenerator.tests.unit.test_generate_analyzer import AnotherClass, YetAnotherClass
404- class TestClass:
405- def func(self, a: 'AnotherClass') -> 'YetAnotherClass': return AnotherClass()""" ,
436+ tw .dedent (
437+ """
438+ from scripts.microgenerator.tests.unit.test_generate_analyzer import AnotherClass, YetAnotherClass
439+ class TestClass:
440+ def func(self, a: 'AnotherClass') -> 'YetAnotherClass': return AnotherClass()
441+ """
442+ ),
406443 [("a" , "'AnotherClass'" )],
407444 "'YetAnotherClass'" ,
408445 id = "forward_refs" ,
409446 ),
410447 pytest .param (
411- """class TestClass:
412- def func(self, a, b): return a + b""" ,
448+ tw .dedent (
449+ """
450+ class TestClass:
451+ def func(self, a, b): return a + b
452+ """
453+ ),
413454 [("a" , None ), ("b" , None )], # No annotations means type is None
414455 None ,
415456 id = "no_annotations" ,
416457 ),
417458 pytest .param (
418- """from typing import List, Optional, Dict, Union, Any
419- class TestClass:
420- def func(self, a: List[Optional[Dict[str, Union[int, str]]]]) -> Dict[str, Any]: return {}""" ,
459+ tw .dedent (
460+ """
461+ from typing import List, Optional, Dict, Union, Any
462+ class TestClass:
463+ def func(self, a: List[Optional[Dict[str, Union[int, str]]]]) -> Dict[str, Any]: return {}
464+ """
465+ ),
421466 [("a" , "List[Optional[Dict[str, Union[int, str]]]]" )],
422467 "Dict[str, Any]" ,
423468 id = "complex_nested" ,
424469 ),
425470 pytest .param (
426- """from typing import Literal
427- class TestClass:
428- def func(self, a: Literal['one', 'two']) -> Literal[True]: return True""" ,
471+ tw .dedent (
472+ """
473+ from typing import Literal
474+ class TestClass:
475+ def func(self, a: Literal['one', 'two']) -> Literal[True]: return True
476+ """
477+ ),
429478 [("a" , "Literal['one', 'two']" )],
430479 "Literal[True]" ,
431480 id = "literal_type" ,
0 commit comments