def test_get_import(self) -> None: module_path = ModulePath( 'package.subpackage.module', Module( function_map={ 'f1': Function.of_name('f1'), 'f2': Function.of_name('f2'), }, class_map={ 'c1': Class( name='c1', line_begin=-1, line_end=-1, function_map={ 'f1': Function.of_name('f1'), 'f2': Function.of_name('f2'), }, ), }, )) assert (module_path.get_import('f1') == 'from package.subpackage.module import f1') assert (module_path.get_import('c1') == 'from package.subpackage.module import c1') assert (module_path.get_import('f3') is None)
def test_to_test(self) -> None: assert vars(self.m1.to_test()) == vars(Module( {}, class_map={ 'TestC': Class( 'TestC', -1, -1, { 'test_a': Function( 'test_a', '', -1, -1, FuncDeclType.INSTANCE, [], ), 'test_b': Function( 'test_b', '', -1, -1, FuncDeclType.INSTANCE, [], ), }, [] ), 'Test': Class( 'Test', -1, -1, { 'test_a': Function( 'test_a', '', -1, -1, FuncDeclType.INSTANCE, [], ), }, [], ), }, ))
def setUp(self) -> None: self.m1 = Module( function_map={ 'a': Function('a', '', 1, 10, FuncDeclType.TOP_LEVEL, []), }, class_map={ 'C': Class('C', 12, 30, { 'a': Function( 'a', '', 13, 20, FuncDeclType.INSTANCE, [], ), 'b': Function( 'b', '', 21, 30, FuncDeclType.INSTANCE, [], ) }, []), }, ) self.m2 = Module( function_map={}, class_map={ 'C': Class('C', -1, -1, { 'a': Function( 'a', '', -1, -1, FuncDeclType.INSTANCE, [], ), }, []), }, ) return
def test_load_module_from_path(self) -> None: # ------------------------------------- # Simple file loading # ------------------------------------- module_path = ( self.sample_project_path / 'package/main.py' ) loader = SyntacticModuleLoader() module = loader.load_module_from_path(str(module_path)) assert module is not None # Assertion of top level functions assert ( module.function_map['top_level_function1'] == Function( 'top_level_function1', 1, 3, FuncDeclType.TOP_LEVEL, ) ) # Assertion of class methods assert ( vars(module.class_map['TopLevelClass1']) == vars(Class( 'TopLevelClass1', 5, 19, { 'instance_method1': Function( 'instance_method1', 10, 12, FuncDeclType.INSTANCE, ), 'class_method1': Function( 'class_method1', 14, 16, FuncDeclType.CLASS, ), 'instance_method2': Function( 'instance_method2', 17, 19, FuncDeclType.INSTANCE, ), }, )) ) assert ( module.class_map['TopLevelClass2'] == Class( 'TopLevelClass2', 25, 39, { 'instance_method3': Function( 'instance_method3', 30, 32, FuncDeclType.INSTANCE, ), 'class_method2': Function( 'class_method2', 34, 36, FuncDeclType.CLASS, ), 'instance_method4': Function( 'instance_method4', 37, 39, FuncDeclType.INSTANCE, ), }, ) ) # ------------------------------------- # Complex file loading # ------------------------------------- module_path = ( self.sample_project_path / 'package/subpackage/sub_main.py' ) loader = SyntacticModuleLoader() module = loader.load_module_from_path(str(module_path)) assert module is not None print(module.to_json()) # Assertion of top level functions assert ( module.function_map['sub_top_level_function1'] == Function( 'sub_top_level_function1', 2, 11, FuncDeclType.TOP_LEVEL, ) ) # Assertion of class methods assert ( vars(module.class_map['SubTopLevelClass1']) == vars(Class( 'SubTopLevelClass1', 13, 32, { 'instance_method1': Function( 'instance_method1', 24, 32, FuncDeclType.INSTANCE, ), }, )) ) return
def test_load_module_from_path(self) -> None: # ------------------------------------- # Simple file loading # ------------------------------------- module_path = (self.sample_project_path / 'package/main.py') loader = SyntacticModuleLoader() module = loader.load_module_from_path(str(module_path)) assert module is not None # Assertion of top level functions assert (module.function_map['top_level_function1'] == Function( 'top_level_function1', '', 1, 3, FuncDeclType.TOP_LEVEL, [ 'def top_level_function1() -> None:', ], )) # Assertion of class methods assert (vars(module.class_map['TopLevelClass1']) == vars( Class( 'TopLevelClass1', 5, 19, { 'instance_method1': Function( 'instance_method1', '', 10, 12, FuncDeclType.INSTANCE, [ 'def instance_method1(self) -> None:', ], ), 'class_method1': Function( 'class_method1', '', 14, 16, FuncDeclType.CLASS, [ 'def class_method1(cls) -> None:', ], ), 'instance_method2': Function( 'instance_method2', '', 17, 19, FuncDeclType.INSTANCE, [ 'def instance_method2(self) -> None:', ], ), }, [ 'class TopLevelClass1:', ]))) assert (module.class_map['TopLevelClass2'] == Class( 'TopLevelClass2', 25, 39, { 'instance_method3': Function( 'instance_method3', '', 30, 32, FuncDeclType.INSTANCE, [ 'def instance_method3(self) -> None:', ], ), 'class_method2': Function( 'class_method2', '', 34, 36, FuncDeclType.CLASS, [ 'def class_method2(cls) -> None:', ], ), 'instance_method4': Function( 'instance_method4', '', 37, 39, FuncDeclType.INSTANCE, [ 'def instance_method4(self) -> None:', ], ), }, [ 'class TopLevelClass2:', ])) # ------------------------------------- # Complex file loading # ------------------------------------- module_path = (self.sample_project_path / 'package/subpackage/sub_main.py') loader = SyntacticModuleLoader() module = loader.load_module_from_path(str(module_path)) assert module is not None # Assertion of top level functions assert (module.function_map['sub_top_level_function1'] == Function( 'sub_top_level_function1', 'Top level function.\nsignature is multilined.\n', 2, 11, FuncDeclType.TOP_LEVEL, [ 'def sub_top_level_function1(', ' hoge: int,', ' fuga: str,', ') -> None:', ' """ Top level function.', '', ' signature is multilined.', ' """', ])) # Assertion of class methods assert (vars(module.class_map['SubTopLevelClass1']) == vars( Class( 'SubTopLevelClass1', 13, 32, { 'instance_method1': Function( 'instance_method1', 'Instance method.\nsignature is multilined.\n', 24, 32, FuncDeclType.INSTANCE, [ 'def instance_method1(', ' self, hoge: int,', ') -> None:', ' """ Instance method.', '', ' signature is multilined.', ' """', ], ), }, ['class SubTopLevelClass1:']))) # Assertion of class with docstring assert (vars(module.class_map['SubTopLevelClass2']) == vars( Class( 'SubTopLevelClass2', 34, 41, {}, [ 'class SubTopLevelClass2:', ' """', ' Multiline docstring top level class.', '', ' This should be captured.', ' """', ], ))) return