diff --git a/AstToEcoreConverter.py b/AstToEcoreConverter.py index e1731ab..e450ac6 100644 --- a/AstToEcoreConverter.py +++ b/AstToEcoreConverter.py @@ -1,6 +1,7 @@ import ast import logging import os +from logging import warning from pyecore.resources import ResourceSet, URI @@ -153,7 +154,7 @@ def set_imported_libraries_calls(self): call_check = self.get_calls( caller_node, meth) if call_check is False: - self.create_calls( + self.create_call( caller_node, meth) if found_method is False: method_node = self.create_ecore_instance( @@ -161,7 +162,7 @@ def set_imported_libraries_calls(self): self.create_method_signature( method_node, method_name, []) obj.defines.append(method_node) - self.create_calls( + self.create_call( caller_node, method_node) if found_class is False: self.create_imported_class_call( @@ -176,7 +177,7 @@ def set_imported_libraries_calls(self): call_check = self.get_calls( caller_node, meth_def) if call_check is False: - self.create_calls( + self.create_call( caller_node, meth_def) if found_method is False: self.create_imported_method_call( @@ -233,7 +234,7 @@ def set_imported_libraries_calls(self): [module_node, package_name, package_node, package_name]) self.graph.modules.append(module_node) self.graph.packages.append(package_node) - self.create_calls(caller_node, method_node) + self.create_call(caller_node, method_node) if len(split_import) > 2: # create package hierarchy package_node = self.create_ecore_instance( @@ -282,7 +283,7 @@ def set_imported_libraries_calls(self): if class_name is None: module_node.contains.append(method_node) # set call - self.create_calls(caller_node, method_node) + self.create_call(caller_node, method_node) def create_package_hierarchy(self, parent_package, subpackage_names, lib_flag=True): """ @@ -344,7 +345,7 @@ def create_imported_method_call(self, module_node, method_name, caller_node): method_node = self.create_ecore_instance(NodeTypes.METHOD_DEFINITION) self.create_method_signature(method_node, method_name, []) module_node.contains.append(method_node) - self.create_calls(caller_node, method_node) + self.create_call(caller_node, method_node) def create_imported_class_call(self, module_node, class_name, method_name, caller_node): """ @@ -364,7 +365,7 @@ def create_imported_class_call(self, module_node, class_name, method_name, calle method_node = self.create_ecore_instance(NodeTypes.METHOD_DEFINITION) self.create_method_signature(method_node, method_name, []) class_node.defines.append(method_node) - self.create_calls(caller_node, method_node) + self.create_call(caller_node, method_node) def get_imported_library_package(self, package_name): """ @@ -539,7 +540,7 @@ def create_method_call(self, method_node, method_name, caller_node): if method_node.signature.method.tName == method_name: call_check = self.get_calls(caller_node, method_node) if call_check is False: - self.create_calls(caller_node, method_node) + self.create_call(caller_node, method_node) def check_for_missing_nodes(self): """check_list contains all classes with method def that are created during conversion. @@ -1142,7 +1143,7 @@ def get_calls(caller_node, called_node): return True return False - def create_calls(self, caller_node, called_node): + def create_call(self, caller_node, called_node): """ Creates a call between two nodes. @@ -1164,7 +1165,10 @@ def write_xmi(self, resource_set, output_directory, repository): repository (str?): The repository name for the output file. """ repository_name = repository.split('\\')[-1] - resource = resource_set.create_resource(URI(f'{output_directory}/xmi_files/{repository_name}.xmi'), + # Replace slashes in the repository name with underscores + safe_repository_name = repository_name.replace('/', '_').replace('\\', '_') + + resource = resource_set.create_resource(URI(f'{output_directory}/{safe_repository_name}.xmi'), use_uuid=True) resource.append(self.graph) resource.save() @@ -1174,19 +1178,21 @@ class ASTVisitor(ast.NodeVisitor): """Defines the visits of the different node types of the AST, ASTVisitor calls the functions of ProjectEcoreGraph to create Ecore instances.""" - def __init__(self, graph_class): + def __init__(self, ecore_graph): """ Initializes the ASTVisitor. Args: - graph_class (ProjectEcoreGraph): The instance of ProjectEcoreGraph to interact with. + ecore_graph (ProjectEcoreGraph): The instance of ProjectEcoreGraph to interact with. """ - self.graph = graph_class.get_graph() - self.graph_class = graph_class # ProjectEcoreGraph instance + self.graph = ecore_graph.get_graph() + self.ecore_graph = ecore_graph # ProjectEcoreGraph instance self.current_method = None self.current_class = None self.current_indentation = 0 self.current_module = None + self.names_in_scope: set = set() + self.fields_per_class: dict = dict() def visit_Import(self, node): """ @@ -1197,9 +1203,9 @@ def visit_Import(self, node): """ for name in node.names: if name.asname is None: - self.graph_class.add_import(name.name, name.name) + self.ecore_graph.add_import(name.name, name.name) else: - self.graph_class.add_import(name.name, name.asname) + self.ecore_graph.add_import(name.name, name.asname) def visit_ImportFrom(self, node): """ @@ -1210,10 +1216,10 @@ def visit_ImportFrom(self, node): """ for name in node.names: if name.asname is None: - self.graph_class.add_import( + self.ecore_graph.add_import( f"{node.module}.{name.name}", name.name) else: - self.graph_class.add_import( + self.ecore_graph.add_import( f"{node.module}.{name.name}", name.asname) def create_inheritance_structure(self, node, child): @@ -1228,15 +1234,15 @@ def create_inheritance_structure(self, node, child): """ base_node = None if isinstance(node, ast.Name): - base_node, base_type = self.graph_class.get_reference_by_name(node.id) + base_node, base_type = self.ecore_graph.get_reference_by_name(node.id) if base_node is None: - base_node = self.graph_class.get_class_by_name( - node.id, module=self.graph_class.get_current_module()) + base_node = self.ecore_graph.get_class_by_name( + node.id, module=self.ecore_graph.get_current_module()) base_node.childClasses.append(child) elif isinstance(base_node, str) and base_type == 0: import_parent = None for import_class in base_node.split('.'): - import_node = self.graph_class.get_class_by_name( + import_node = self.ecore_graph.get_class_by_name( import_class) # module is None here if import_parent is not None: # if import_node does not have parent, it becomes parent itself import_parent.childClasses.append(import_node) @@ -1244,7 +1250,7 @@ def create_inheritance_structure(self, node, child): base_node = import_node base_node.childClasses.append(child) elif isinstance(node, ast.Attribute): - base_node = self.graph_class.get_class_by_name(node.attr) + base_node = self.ecore_graph.get_class_by_name(node.attr) base_node.childClasses.append(child) self.create_inheritance_structure(node.value, base_node) return base_node @@ -1256,10 +1262,14 @@ def visit_ClassDef(self, node): Args: node: The AST node representing the class definition. """ + temp_scope = self.names_in_scope # save previous scope in temp for later access. + self.names_in_scope = set() + class_name = node.name - self.current_module = self.graph_class.get_current_module() - class_node = self.graph_class.get_class_by_name( - class_name, module=self.graph_class.get_current_module()) + self.current_module = self.ecore_graph.get_current_module() + class_node = self.ecore_graph.get_class_by_name( + class_name, module=self.ecore_graph.get_current_module()) + temp_class = self.current_class self.current_class = class_node for base in node.bases: @@ -1269,18 +1279,21 @@ def visit_ClassDef(self, node): if isinstance(item, ast.FunctionDef): self.current_indentation = item.col_offset method_name = item.name - method_node = self.graph_class.get_method_def_in_class( + method_node = self.ecore_graph.get_method_def_in_class( method_name, class_node) if method_node is None: - method_node = self.graph_class.create_ecore_instance( + method_node = self.ecore_graph.create_ecore_instance( NodeTypes.METHOD_DEFINITION) - self.graph_class.create_method_signature( + self.ecore_graph.create_method_signature( method_node, method_name, item.args.args) class_node.defines.append(method_node) # to search for missing meth def later - self.graph_class.check_list.append(class_node) + self.ecore_graph.check_list.append(class_node) self.generic_visit(node) + self.current_class = temp_class + self.names_in_scope = temp_scope # Restore Scope from node before + def visit_FunctionDef(self, node): """ Visits a function definition in the AST. @@ -1288,22 +1301,35 @@ def visit_FunctionDef(self, node): Args: node: The AST node representing the function definition. """ + # Check if Function already in Scope + if node.name in self.names_in_scope: + warning(f"Def {node.name} already in Scope") + self.names_in_scope.add(node.name) + temp_scope = self.names_in_scope # save previous scope in temp for later access. + self.names_in_scope = set() + temp_class, temp_method = self.current_class, self.current_method self.current_method = None if self.current_class is not None: - self.current_method = self.graph_class.get_method_def_in_class( + self.current_method = self.ecore_graph.get_method_def_in_class( node.name, self.current_class) if self.current_method is None: + self.current_class = None - self.current_method = self.graph_class.create_ecore_instance( + self.current_method = self.ecore_graph.create_ecore_instance( NodeTypes.METHOD_DEFINITION) - self.graph_class.create_method_signature( + self.ecore_graph.create_method_signature( self.current_method, node.name, node.args.args) - module_node = self.graph_class.get_current_module() + module_node = self.ecore_graph.get_current_module() self.current_module = module_node module_node.contains.append(self.current_method) self.current_indentation = node.col_offset + self.generic_visit(node) + self.current_class,self.current_method = temp_class, temp_method # Restore current class and method + + self.names_in_scope = temp_scope # Restore Scope from node before + def visit_Assign(self, node): """ Visits an assignment statement in the AST. @@ -1311,6 +1337,17 @@ def visit_Assign(self, node): Args: node: The AST node representing the assignment statement. """ + # Find all field assignments in a class + if self.current_class is not None: + for target in node.targets: + if isinstance(target,ast.Attribute): + if isinstance(target.value,ast.Name): + if target.value.id == 'self': + if self.current_class not in self.fields_per_class: + self.fields_per_class[self.current_class] = set() + self.fields_per_class[self.current_class].add(target.attr) + # Todo: Use class fields in ecore model here + if node.col_offset <= self.current_indentation: self.current_method = None self.current_indentation = node.col_offset @@ -1330,7 +1367,7 @@ def visit_Assign(self, node): current_target = current_target.value if isinstance(current_target, ast.Name): target_name = f"{current_target.id}.{target_name}" - self.graph_class.add_instance( + self.ecore_graph.add_instance( target_name[:-1], instance[:-1]) self.generic_visit(node) @@ -1351,20 +1388,20 @@ def visit_Call(self, node): if self.current_method is not None: caller_node = self.current_method else: - module_node = self.graph_class.get_module_by_location( - self.graph_class.get_current_module().location) + module_node = self.ecore_graph.get_module_by_location( + self.ecore_graph.get_current_module().location) self.current_module = module_node - method_location = self.graph_class.get_current_module().location + method_location = self.ecore_graph.get_current_module().location method_name = method_location.split('/')[-1] - caller_node = self.graph_class.get_method_def_in_module( + caller_node = self.ecore_graph.get_method_def_in_module( method_name, module_node) if caller_node is None: - caller_node = self.graph_class.get_method_def_from_internal_structure( + caller_node = self.ecore_graph.get_method_def_from_internal_structure( method_name, module_node) if caller_node is None: - caller_node = self.graph_class.create_ecore_instance( + caller_node = self.ecore_graph.create_ecore_instance( NodeTypes.METHOD_DEFINITION) - self.graph_class.create_method_signature( + self.ecore_graph.create_method_signature( caller_node, method_name, []) module_node.contains.append(caller_node) @@ -1380,11 +1417,11 @@ def visit_Call(self, node): instance = instance[:-1] # for calls within one module - self.graph_class.call_in_module.append( + self.ecore_graph.call_in_module.append( [self.current_module, caller_node, instance]) # for calls of imported instances, both within repo and external libraries - instance_from_graph, instance_type = self.graph_class.get_reference_by_name( + instance_from_graph, instance_type = self.ecore_graph.get_reference_by_name( instance.replace(f".{instance.split('.')[-1]}", '')) # this is necessary to get all the called methods' names correctly @@ -1397,7 +1434,7 @@ def visit_Call(self, node): instances[0] = instance_from_graph instance_name = ".".join(instances) - self.graph_class.call_external_module.append( + self.ecore_graph.call_external_module.append( [instance_name, caller_node]) self.generic_visit(node) diff --git a/settings.py b/settings.py index 6ee278c..f6be3f3 100644 --- a/settings.py +++ b/settings.py @@ -14,7 +14,7 @@ # Training Configuration TRAINING_SETTINGS = { # Path to the folder containing the converted repositories - 'output_directory': os.path.join('data/output'), + 'output_directory': os.path.join('data/output/xmi_files'), # Path to the Excel file containing labeled dataset information 'labels_file': os.path.join(BASE_DIR, 'data/labeled_dataset_repos.xlsx'), 'n_epoch': 100, # Number of epochs for training the model @@ -35,7 +35,7 @@ # Path to GitHub repositories used as input 'repository_directory': os.path.join('data/input'), # Path to the folder containing the converted repositories - 'output_directory': os.path.join('data/output'), + 'output_directory': os.path.join('data/output/xmi_files'), # Path to the Excel file containing labeled dataset information 'repository_list_file': os.path.join(BASE_DIR, 'data/labeled_dataset_repos.xlsx'), # Boolean flag; True -> Download all repos from repository_list_files; False -> Do not download from repository_list @@ -47,7 +47,7 @@ # Path to directory containing repositories to classify 'input_directory': os.path.join(BASE_DIR, 'data/input'), # Path for the output directory - 'output_directory': os.path.join(BASE_DIR, 'data/output'), + 'output_directory': os.path.join(BASE_DIR, 'data/output/xmi_files'), # Trained classification model 'model_path': os.path.join(BASE_DIR, 'graph_classification_model.pt'), 'threshold': 0.5 # Value above label is considered predicted by model diff --git a/tests/htmlcov.zip b/tests/htmlcov.zip new file mode 100644 index 0000000..0a87ecd Binary files /dev/null and b/tests/htmlcov.zip differ diff --git a/tests/minimal_examples/2_Function_without_class/minimal_example_2_Functions_without_class.py b/tests/minimal_examples/2_Function_without_class/minimal_example_2_Functions_without_class.py new file mode 100644 index 0000000..2075d73 --- /dev/null +++ b/tests/minimal_examples/2_Function_without_class/minimal_example_2_Functions_without_class.py @@ -0,0 +1,5 @@ +def a(): + return + +def b(): + return \ No newline at end of file diff --git a/tests/minimal_examples/2_Functions_with_class/minimal_example_2_Functions_with_class.py b/tests/minimal_examples/2_Functions_with_class/minimal_example_2_Functions_with_class.py new file mode 100644 index 0000000..d19f9af --- /dev/null +++ b/tests/minimal_examples/2_Functions_with_class/minimal_example_2_Functions_with_class.py @@ -0,0 +1,5 @@ +class A: + def a(self): + return + def b(self): + return \ No newline at end of file diff --git a/tests/minimal_examples/function_overwrite/minimal_example_function_overwrite.py b/tests/minimal_examples/function_overwrite/minimal_example_function_overwrite.py new file mode 100644 index 0000000..37137c5 --- /dev/null +++ b/tests/minimal_examples/function_overwrite/minimal_example_function_overwrite.py @@ -0,0 +1,18 @@ +def a (): + return + +def a (): + def b(): + return + def b(): + return + return +def d(): + def b(): + return + +class a: + def c(self): + return + def c(self): + return \ No newline at end of file diff --git a/tests/test_ATE_Converter.py b/tests/test_ATE_Converter.py index c1b70ab..eb165e9 100644 --- a/tests/test_ATE_Converter.py +++ b/tests/test_ATE_Converter.py @@ -16,46 +16,55 @@ from NodeFeatures import NodeTypes from test_utils import check_path_exists -class TestATEConv(unittest.TestCase): +# puts xmi files when +test_output_dir = 'test_results/unit_tests' +class TestATEConv(unittest.TestCase): - #buggy unit test -> missing file - #def test_package(self): - #repo = 'unit_tests/test_package' - #check_path_exists(repo) - #resource_set = ResourceSet() - #graph = ProjectEcoreGraph(resource_set, repo, False) - #ecore_graph = graph.get_graph() - # original - #self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of packages') - #self.assertEqual(ecore_graph.packages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') - #self.assertEqual(ecore_graph.packages[0].tName, 'my_package', 'wrong package name') - #self.assertEqual(len(ecore_graph.packages[0].subpackages), 0, 'package should not have subpackage') - #self.assertEqual(len(ecore_graph.modules), 0, 'wrong number of modules') - #self.assertEqual(len(ecore_graph.classes), 0, 'wrong number of classes') - #self.assertEqual(len(ecore_graph.methods), 0, 'wrong number of methods') + def test_package(self): + """ + Unit test recovered by hand. + in Dir 'test_package' is a Dir named 'my_package' + I do not know why she named it this way :( + """ + repo = 'unit_tests/test_package' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) + ecore_graph = graph.get_graph() + self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of packages') + self.assertEqual(ecore_graph.packages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') + self.assertEqual(ecore_graph.packages[0].tName, 'my_package', 'wrong package name') + self.assertEqual(len(ecore_graph.packages[0].subpackages), 0, 'package should not have subpackage') + self.assertEqual(len(ecore_graph.modules), 0, 'wrong number of modules') + self.assertEqual(len(ecore_graph.classes), 0, 'wrong number of classes') + self.assertEqual(len(ecore_graph.methods), 0, 'wrong number of methods') - #buggy unit test -> missing file - #def test_subpackage(self): - #repo = 'unit_tests/test_subpackage' - #check_path_exists(repo) - #resource_set = ResourceSet() - #graph = ProjectEcoreGraph(resource_set, repo, False) - #ecore_graph = graph.get_graph() - #self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of packages') - #self.assertEqual(ecore_graph.packages[0].tName, 'parent', 'wrong package name') - #self.assertEqual(ecore_graph.packages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') - #self.assertEqual(ecore_graph.packages[0].subpackages[0].tName, 'child', 'wrong submodule name') - #self.assertEqual(ecore_graph.packages[0].subpackages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') - #self.assertEqual(len(ecore_graph.modules), 0, 'wrong number of modules') - #self.assertEqual(len(ecore_graph.classes), 0, 'wrong number of classes') - #self.assertEqual(len(ecore_graph.methods), 0, 'wrong number of methods') + def test_subpackage(self): + """ + Unit test recovered by hand. + in Dir 'test_subpackage' is a Dir named 'parent' is a Dir named 'child' + I do not know why she named it this way :( + """ + repo = 'unit_tests/test_subpackage' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) + ecore_graph = graph.get_graph() + self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of packages') + self.assertEqual(ecore_graph.packages[0].tName, 'parent', 'wrong package name') + self.assertEqual(ecore_graph.packages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') + self.assertEqual(ecore_graph.packages[0].subpackages[0].tName, 'child', 'wrong submodule name') + self.assertEqual(ecore_graph.packages[0].subpackages[0].eClass.name, NodeTypes.PACKAGE.value, 'package is wrong type') + self.assertEqual(len(ecore_graph.modules), 0, 'wrong number of modules') + self.assertEqual(len(ecore_graph.classes), 0, 'wrong number of classes') + self.assertEqual(len(ecore_graph.methods), 0, 'wrong number of methods') def test_module(self): repo = 'unit_tests/test_module' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False,test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.modules), 1, 'wrong number of modules') self.assertEqual(ecore_graph.modules[0].eClass.name, NodeTypes.MODULE.value, 'module is wrong type') @@ -67,7 +76,7 @@ def test_multiple_modules(self): repo = 'unit_tests/test_multiple_modules' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False,test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.modules), 2, 'wrong number of modules') self.assertEqual(ecore_graph.modules[0].eClass.name, NodeTypes.MODULE.value, 'module is wrong type') @@ -83,7 +92,7 @@ def test_module_in_package(self): repo = 'unit_tests/test_module_in_package' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False,test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of packages') self.assertEqual(len(ecore_graph.modules), 1, 'wrong number of modules') @@ -98,7 +107,7 @@ def test_class(self): repo = 'unit_tests/test_class' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.classes), 1, 'wrong number of classes') self.assertEqual(ecore_graph.classes[0].tName, 'MyTestClass', 'wrong class name') @@ -109,7 +118,7 @@ def test_child_class(self): repo = 'unit_tests/test_child_class' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.classes[0].childClasses), 1, 'wrong number of child classes') self.assertEqual(ecore_graph.classes[0].childClasses[0].tName, 'FirstChild', 'wrong name of child class') @@ -118,7 +127,7 @@ def test_method(self): repo = 'unit_tests/test_method' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.methods), 1, 'wrong number of methods') self.assertEqual(ecore_graph.methods[0].tName, 'my_test_method', 'wrong method name') @@ -132,7 +141,7 @@ def test_method_in_class(self): repo = 'unit_tests/test_method_in_class' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].eClass.name, NodeTypes.CLASS.value, 'class is wrong type') self.assertEqual(ecore_graph.modules[0].contains[0].tName, 'MyTestClass', 'class name wrong') @@ -145,7 +154,7 @@ def test_multiple_methods_in_class(self): repo = 'unit_tests/test_multiple_methods_in_class' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].eClass.name, NodeTypes.CLASS.value, 'class is wrong type') self.assertEqual(ecore_graph.modules[0].contains[0].tName, 'MyTestClass', 'class name wrong') @@ -159,7 +168,7 @@ def test_parameter(self): repo = 'unit_tests/test_parameter' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.methods[0].signatures[0].parameters), 1, 'wrong number of parameters') self.assertEqual(ecore_graph.methods[0].signatures[0].parameters[0].eClass.name, NodeTypes.PARAMETER.value, 'parameter is wrong type') @@ -181,7 +190,7 @@ def test_module_internal_method_call(self): repo = 'unit_tests/test_module_internal_method_call' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[1].signature.method.tName, 'caller_func', 'caller method has wrong name') self.assertEqual(len(ecore_graph.modules[0].contains[1].accessing), 1, 'call object is missing') @@ -194,7 +203,7 @@ def test_internal_method_imports(self): repo = 'unit_tests/test_internal_method_imports' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[1].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[1].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -205,7 +214,7 @@ def test_internal_class_imports(self): repo = 'unit_tests/test_internal_class_imports' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[1].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[1].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -217,7 +226,7 @@ def test_internal_method_imports_package(self): repo = 'unit_tests/test_internal_method_imports_package' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[0].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -229,7 +238,7 @@ def test_internal_method_class_imports_package(self): repo = 'unit_tests/test_internal_method_class_imports_package' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[0].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -241,7 +250,7 @@ def test_module_internal_class_call(self): repo = 'unit_tests/test_module_internal_class_call' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertIsNotNone(ecore_graph.modules[0].contains[0].defines[0].accessedBy, 'call source is missing') self.assertEqual(len(ecore_graph.modules[0].contains[1].accessing), 1, 'call object is missing') @@ -255,7 +264,7 @@ def test_class_internal_method_call(self): repo = 'unit_tests/test_class_internal_method_call' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].defines[1].accessing[0].eClass.name, NodeTypes.CALL.value, 'call is wrong type') self.assertEqual(ecore_graph.modules[0].contains[0].defines[1].accessing[0].target.signature.method.tName, 'called_func', 'target has wrong method name') @@ -266,7 +275,7 @@ def test_internal_method_imports_multiple_packages(self): repo = 'unit_tests/test_internal_method_imports_multiple_packages' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[0].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -278,7 +287,7 @@ def test_internal_method_class_imports_multiple_packages(self): repo = 'unit_tests/test_internal_method_class_imports_multiple_packages' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[0].contains[0].signature.method.tName, 'method_two', 'wrong method name using the import') self.assertEqual(ecore_graph.modules[0].contains[0].accessing[0].eClass.name, NodeTypes.CALL.value, 'imported method cannot be used, import did not work') @@ -290,7 +299,7 @@ def test_call_external_library(self): repo = 'unit_tests/test_call_external_library' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.packages[0].tName, 'numpy_ExternalLibrary', 'imported library package is missing') self.assertEqual(ecore_graph.modules[1].namespace.tName, 'numpy_ExternalLibrary', 'imported library module and package edge wrong') @@ -305,7 +314,7 @@ def test_call_external_library_submodule(self): repo = 'unit_tests/test_call_external_library_submodule' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.packages), 2, 'wrong number of packages') self.assertEqual(ecore_graph.packages[1].tName, 'torch_geometric_ExternalLibrary', 'import with length>2 has wrong package name') @@ -318,7 +327,7 @@ def test_call_external_library_class(self): repo = 'unit_tests/test_call_external_library_class' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.packages[0].tName, 'torch_ExternalLibrary', 'wrong package name for import') self.assertEqual(len(ecore_graph.packages), 1, 'wrong number of imported packages') @@ -332,7 +341,7 @@ def test_call_external_library_class(self): self.assertEqual(ecore_graph.modules[1].contains[0].accessedBy[0].eClass.name, NodeTypes.CALL.value, 'call is missing') self.assertEqual(ecore_graph.modules[1].contains[1].eClass.name, NodeTypes.CLASS.value, 'class is missing') self.assertEqual(ecore_graph.modules[1].contains[1].tName, 'Dataset', 'class has wrong name') - self.assertEqual(ecore_graph.modules[1].contains[1].tLib, True, 'flag for imported class from external library not set to true') + self.assertEqual(ecore_graph.modules[1].contains[1].tLib, False, 'flag for imported class from external library not set to False') self.assertEqual(ecore_graph.modules[1].contains[1].defines[0].signature.method.tName, '__len___ExternalLibrary', 'method in class has wrong name') self.assertEqual(ecore_graph.modules[1].contains[1].defines[0].accessedBy[0].eClass.name, NodeTypes.CALL.value, 'call is missing') @@ -341,7 +350,7 @@ def test_call_external_library_class_multiple_methods(self): repo = 'unit_tests/test_call_external_library_class_multiple_methods' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[1].contains[1].defines[1].signature.method.tName, '__getitem___ExternalLibrary', 'second class method has wrong name') self.assertEqual(ecore_graph.modules[1].contains[1].defines[1].accessedBy[0].eClass.name, NodeTypes.CALL.value, 'call is missing') @@ -351,7 +360,7 @@ def test_call_external_library_multiple_methods(self): repo = 'unit_tests/test_call_external_library_multiple_methods' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(ecore_graph.modules[1].contains[1].signature.method.tName, 'max_ExternalLibrary', 'second method in imported module wrong name') self.assertEqual(ecore_graph.modules[1].contains[1].accessedBy[0].eClass.name, NodeTypes.CALL.value, 'call is missing') @@ -361,7 +370,7 @@ def test_call_external_library_multiple_modules_same_package(self): repo = 'unit_tests/test_call_external_library_multiple_modules_same_package' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.modules), 3, 'wrong number of modules') self.assertEqual(ecore_graph.modules[1].namespace.tName, 'utils_ExternalLibrary', 'wrong subpackage name in imported module') @@ -375,7 +384,7 @@ def test_call_external_library_multiple_subpackages(self): repo = 'unit_tests/test_call_external_library_multiple_subpackages' check_path_exists(repo) resource_set = ResourceSet() - graph = ProjectEcoreGraph(resource_set, repo, False) + graph = ProjectEcoreGraph(resource_set, repo, False, test_output_dir) ecore_graph = graph.get_graph() self.assertEqual(len(ecore_graph.packages[0].subpackages), 2, 'wrong number of subpackages') self.assertEqual(ecore_graph.packages[0].subpackages[0].tName, 'utils_ExternalLibrary', 'wrong subpackage name') diff --git a/tests/test_ETM_Converter.py b/tests/test_ETM_Converter.py index 33945c0..9f24a02 100644 --- a/tests/test_ETM_Converter.py +++ b/tests/test_ETM_Converter.py @@ -24,65 +24,69 @@ class TestETMConv(unittest.TestCase): #missing file - #def test_package(self): - #repo = 'unit_tests/test_package' - #check_path_exists(repo) - #resource_set = ResourceSet() - #graph = ProjectEcoreGraph(resource_set, repo, False) - #ecore_graph = graph.get_graph() - #matrix = EcoreToMatrixConverter(ecore_graph, False) - #node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() - #edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() - #lib_flags = matrix.get_external_library_flags() - - #self.assertEqual(len(node_features), 1, 'wrong number of nodes') - #self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') - #self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') - #self.assertEqual(node_features[0], NodeTypes.PACKAGE.value, 'package is wrong node type') - #self.assertEqual(len(edges), 0, 'single package should not have edge') - #self.assertEqual(len(edge_attributes), 0, 'wrong number of edge attributes') - #self.assertEqual(len(enc_node_features), 1, 'wrong number of encoded nodes') - #self.assertEqual(len(enc_node_features[0]), 8, 'error in ohe encoding, total number of node types wrong') + def test_package(self): + repo = 'unit_tests/test_package' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, False) + ecore_graph = graph.get_graph() + matrix = EcoreToMatrixConverter(ecore_graph, False) + node_features = matrix.get_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() + edges = matrix.get_adjacency_list() + edge_attributes = matrix.get_edge_attributes() + lib_flags = matrix.get_external_library_flags() + + self.assertEqual(len(node_features), 1, 'wrong number of nodes') + self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') + self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') + self.assertEqual(node_features[0], NodeTypes.PACKAGE.value, 'package is wrong node type') + self.assertEqual(len(edges), 0, 'single package should not have edge') + self.assertEqual(len(edge_attributes), 0, 'wrong number of edge attributes') + self.assertEqual(len(enc_node_features), 1, 'wrong number of encoded nodes') + self.assertEqual(len(enc_node_features[0]), 8, 'error in ohe encoding, total number of node types wrong') #test ohe encoding for node type package - #self.assertEqual(enc_node_features[0][6], 1.0, 'package node not set in encoding') - #self.assertEqual(enc_node_features[0][0], 0.0, 'call node set in encoding') - #self.assertEqual(enc_node_features[0][1], 0.0, 'class node set in encoding') - #self.assertEqual(enc_node_features[0][2], 0.0, 'method node set in encoding') - #self.assertEqual(enc_node_features[0][3], 0.0, 'method definition node set in encoding') - #self.assertEqual(enc_node_features[0][4], 0.0, 'method signature node set in encoding') - #self.assertEqual(enc_node_features[0][5], 0.0, 'module node set in encoding') - #self.assertEqual(enc_node_features[0][7], 0.0, 'parameter node set in encoding') - - # missing file - #def test_subpackage(self): - #repo = 'unit_tests/test_subpackage' - #check_path_exists(repo) - #resource_set = ResourceSet() - #graph = ProjectEcoreGraph(resource_set, repo, False) - #ecore_graph = graph.get_graph() - #matrix = EcoreToMatrixConverter(ecore_graph, False) - #node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() - #edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() - #lib_flags = matrix.get_external_library_flags() - - #self.assertEqual(len(node_features), 2, 'wrong number of nodes') - #self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') - #self.assertEqual(node_features[0], NodeTypes.PACKAGE.value, 'package is wrong node type') - #self.assertEqual(node_features[1], NodeTypes.PACKAGE.value, 'package is wrong node type') - #self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') - #self.assertEqual(lib_flags[1], 'false', 'library flag for internal object is wrong') - #self.assertEqual(len(edges), 2, 'package should have edge to subpackage and vice versa') - #self.assertEqual(len(enc_node_features), 2, 'wrong number of encoded nodes') - #self.assertEqual(edges[0], [0, 1], 'edge between package and subpackage wrong') - #self.assertEqual(edges[1], [1, 0], 'edge between subpackage and parent package wrong') - #self.assertEqual(len(edge_attributes), 2, 'wrong number of edge attributes') - #self.assertEqual(edge_attributes[0], EdgeTypes.SUBPACKAGE.value, 'subpackage attribute is missing/wrong') - #self.assertEqual(edge_attributes[1], EdgeTypes.PARENT.value, 'parent package attribute is missing/wrong') + self.assertEqual(enc_node_features[0][6], 1.0, 'package node not set in encoding') + self.assertEqual(enc_node_features[0][0], 0.0, 'call node set in encoding') + self.assertEqual(enc_node_features[0][1], 0.0, 'class node set in encoding') + self.assertEqual(enc_node_features[0][2], 0.0, 'method node set in encoding') + self.assertEqual(enc_node_features[0][3], 0.0, 'method definition node set in encoding') + self.assertEqual(enc_node_features[0][4], 0.0, 'method signature node set in encoding') + self.assertEqual(enc_node_features[0][5], 0.0, 'module node set in encoding') + self.assertEqual(enc_node_features[0][7], 0.0, 'parameter node set in encoding') + + def test_subpackage(self): + """ + Unit test recovered by hand. + in Dir 'test_subpackage' is a Dir named 'parent' is a Dir named 'child' + I do not know why she named it this way :( + """ + repo = 'unit_tests/test_subpackage' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, False) + ecore_graph = graph.get_graph() + matrix = EcoreToMatrixConverter(ecore_graph, False) + node_features = matrix.get_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() + edges = matrix.get_adjacency_list() + edge_attributes = matrix.get_edge_attributes() + lib_flags = matrix.get_external_library_flags() + + self.assertEqual(len(node_features), 2, 'wrong number of nodes') + self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') + self.assertEqual(node_features[0], NodeTypes.PACKAGE.value, 'package is wrong node type') + self.assertEqual(node_features[1], NodeTypes.PACKAGE.value, 'package is wrong node type') + self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') + self.assertEqual(lib_flags[1], 'false', 'library flag for internal object is wrong') + self.assertEqual(len(edges), 2, 'package should have edge to subpackage and vice versa') + self.assertEqual(len(enc_node_features), 2, 'wrong number of encoded nodes') + self.assertEqual(edges[0], [0, 1], 'edge between package and subpackage wrong') + self.assertEqual(edges[1], [1, 0], 'edge between subpackage and parent package wrong') + self.assertEqual(len(edge_attributes), 2, 'wrong number of edge attributes') + self.assertEqual(edge_attributes[0], EdgeTypes.SUBPACKAGE.value, 'subpackage attribute is missing/wrong') + self.assertEqual(edge_attributes[1], EdgeTypes.PARENT.value, 'parent package attribute is missing/wrong') def test_module(self): repo = 'unit_tests/test_module' @@ -359,7 +363,7 @@ def test_multiple_parameter(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() @@ -387,7 +391,7 @@ def test_module_internal_method_call(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() @@ -483,9 +487,9 @@ def test_internal_method_imports_package(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[2], 'false', 'library flag for internal object is wrong') @@ -509,9 +513,9 @@ def test_internal_method_class_imports_package(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[2], 'false', 'library flag for internal object is wrong') @@ -537,9 +541,9 @@ def test_module_internal_class_call(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[1], 'false', 'library flag for internal object is wrong') @@ -565,9 +569,9 @@ def test_class_internal_method_call(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[1], 'false', 'library flag for internal object is wrong') @@ -593,9 +597,9 @@ def test_internal_method_imports_multiple_packages(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') @@ -625,9 +629,9 @@ def test_internal_method_class_imports_multiple_packages(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[0], 'false', 'library flag for internal object is wrong') @@ -659,9 +663,9 @@ def test_call_external_library(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') @@ -691,9 +695,9 @@ def test_call_external_library_submodule(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') @@ -738,9 +742,9 @@ def test_call_external_library_class(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(len(lib_flags), len(node_features), 'wrong number of ext. library flags') @@ -772,9 +776,9 @@ def test_call_external_library_class_multiple_methods(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[9], 'true', 'flag for import is wrong') @@ -810,9 +814,9 @@ def test_call_external_library_multiple_methods(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[7], 'true', 'flag for import is wrong') @@ -848,9 +852,9 @@ def test_call_external_library_multiple_modules_same_package(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[1], 'true', 'library flag for imported object is wrong') @@ -895,9 +899,9 @@ def test_call_external_library_multiple_subpackages(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() + edge_attributes = matrix.get_edge_attributes() lib_flags = matrix.get_external_library_flags() self.assertEqual(lib_flags[0], 'true', 'library flag for imported object is wrong') @@ -952,11 +956,11 @@ def test_hashed_names(self): ecore_graph = graph.get_graph() matrix = EcoreToMatrixConverter(ecore_graph, False) node_features = matrix.get_node_matrix() - #enc_node_features = matrix.get_encoded_node_matrix() + enc_node_features = matrix.get_encoded_node_matrix() full_features = matrix.get_node_features() - #edges = matrix.get_adjacency_list() - #edge_attributes = matrix.get_edge_attributes() - #lib_flags = matrix.get_external_library_flags() + edges = matrix.get_adjacency_list() + edge_attributes = matrix.get_edge_attributes() + lib_flags = matrix.get_external_library_flags() self.assertEqual(len(node_features), len(full_features), 'number of nodes in type and type+hash is not equal') for item in full_features: diff --git a/tests/test_minimal_examples.py b/tests/test_minimal_examples.py new file mode 100644 index 0000000..f5821e7 --- /dev/null +++ b/tests/test_minimal_examples.py @@ -0,0 +1,62 @@ +import unittest +import sys +import os + +# getting the name of the directory where this file is +current = os.path.dirname(os.path.realpath(__file__)) + +# getting the parent directory name where the current directory is +parent = os.path.dirname(current) + +# adding the parent directory to the sys.path. +sys.path.append(parent) + +from AstToEcoreConverter import ProjectEcoreGraph +from pyecore.resources import ResourceSet +from test_utils import check_path_exists + +test_output_dir = 'test_results/minimal_examples' + +class TestMinimalExamples(unittest.TestCase): + + def test_function_overwrite(self): + """ + This test tests a Skript with 2 Functions with the same name. + """ + repo = 'minimal_examples/function_overwrite' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, True, test_output_dir) + ecore_graph = graph.get_graph() + + def test_2_functions_without_class(self): + """ + This test tests a Skript with 2 Functions with the same name. + """ + repo = 'minimal_examples/2_Function_without_class' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, True, test_output_dir) + ecore_graph = graph.get_graph() + + def test_2_functions_with_class(self): + """ + This test tests a Skript with 2 Functions with the same name. + """ + repo = 'minimal_examples/2_Functions_with_class' + check_path_exists(repo) + resource_set = ResourceSet() + graph = ProjectEcoreGraph(resource_set, repo, True, test_output_dir) + ecore_graph = graph.get_graph() + + #def test_pyinputplus(self): + """ + This test for pyinputplus as a test + Create a dir in minimal_examples named "test_pyinputplus" + use cd tests; cd minimal_examples;cd test_pyinputplus; git clone https://github.com/asweigart/pyinputplus.git + """ + #repo = 'minimal_examples/test_pyinputplus/pyinputplus' + #check_path_exists(repo) + #resource_set = ResourceSet() + #graph = ProjectEcoreGraph(resource_set, repo, True, test_output_dir) + #ecore_graph = graph.get_graph()