def test_get_method_nodes(self, custom_python_code): import imp custom_dir = os.path.join(TEST_DATA_DIR, 'custom_data') fname = os.path.join(custom_dir, 'python_file_for_input.py') mod = imp.load_source("python_file_for_input", fname) ast_node = ast.parse(custom_python_code) class_finder = ClassFinder() class_finder.visit(ast_node) class_nodes = class_finder.class_nodes_dict for class_name in class_nodes.keys(): class_node = class_nodes[class_name] ast_wrapper = AstClassWrapper(class_node) no_of_methods = mod.get_number_of_methods(class_name) assert len(ast_wrapper.get_method_nodes()) == no_of_methods
def pipeline_assembled_classes(self, class_node): class_wrapper = AstClassWrapper(class_node) method_matrix = self.cmmm.build_method_matrix(class_wrapper) filtered_matrix = self.ccomct.filter_process([method_matrix]) method_chains = self.cmca.filter_process(filtered_matrix) self.merged_chains = self.ctcm.filter_process(method_chains) return self.cca.filter_process(self.merged_chains)
def test_filter_process(self, custom_python_code): ast_node = ast.parse(custom_python_code) class_finder = ClassFinder() class_finder.visit(ast_node) weight_ssm = 0.5 weight_cdm = 0.5 min_coupling = 0.4 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) class_nodes = class_finder.class_nodes_dict mod = self.get_input_as_module() matrix_factory = mod.get_method_matrix chain_factory = mod.get_method_chains #method_matrices = self.get_method_matrices() cmca = CrisMethodChainsAssembler() for class_name in class_nodes.keys(): custom_matrix = matrix_factory(class_name, weight_ssm, weight_cdm) class_node = class_nodes[class_name] ast_wrapper = AstClassWrapper(class_node) method_matrix = cmmm.build_method_matrix(ast_wrapper) ccomct = CrisCOMConstantThresholdFilter(min_coupling) filtered_matrix = ccomct.filter_process([method_matrix]) method_chains = cmca.filter_process(filtered_matrix) method_names = [[node.name for node in mc.method_ast_nodes] for mc in method_chains] custom_chains = chain_factory(class_name) assert len(method_names) == len(custom_chains) assert method_names in custom_chains
def test_constructor(self, list_of_ast_class_nodes): for python_file, node, class_nodes in list_of_ast_class_nodes: for class_node in class_nodes: class_wrapper = AstClassWrapper(class_node) assert class_wrapper != None assert len(class_wrapper.method_nodes) == \ len(class_wrapper.method_names)
def test_get_instance_variables(self, custom_python_code): custom_fields_by_class_name = \ { 'SomeClass': ['some_var', 'var1', 'var2', 'yet_another_var', 'var3'] } ast_node = ast.parse(custom_python_code) class_finder = ClassFinder() class_finder.visit(ast_node) class_nodes = class_finder.class_nodes_dict for class_name in class_nodes.keys(): class_node = class_nodes[class_name] class_fields = custom_fields_by_class_name[class_name] ast_wrapper = AstClassWrapper(class_node) ast_wrapper_instance_vars = ast_wrapper.get_instance_variables() assert len(ast_wrapper_instance_vars) > 0 assert len(class_fields) == len(ast_wrapper_instance_vars) assert len(ast_wrapper_instance_vars - set(class_fields)) == 0
def test_ssm_metric(self, cls_gen): metric = StructuralSimilarityBetweenMethods() for simple_cls in cls_gen.generate(1000, 10, 10): cls_source = simple_cls.get_source_code() logging.info("TestMetrics::" + \ "test_ssm_metric:simple_cls.source_code:\n" + \ cls_source) ast_wrapper = AstClassWrapper(simple_cls.get_ast_node()) custom_ssm = simple_cls.get_ssm_matrix() matrix_to_test = self.build_test_matrix(metric, ast_wrapper) logging.info("TestMetrics::" + \ "test_ssm_metric:custom_ssm:\n" + \ print_matrix_with_fabulousness(custom_ssm)) logging.info("TestMetrics::" + \ "test_ssm_metric:matrix_to_test:\n" + \ print_matrix_with_fabulousness(matrix_to_test)) for method_node in ast_wrapper.get_method_nodes(): logging.info(metric.find_instance_variables(method_node, ast_wrapper)) assert custom_ssm == matrix_to_test
def test_for_crashes(self, list_of_ast_class_nodes): counter = 0 for python_file, node, class_nodes in list_of_ast_class_nodes: for class_node in class_nodes: ast_wrapper = AstClassWrapper(class_node) weight_ssm = 0.5 weight_cdm = 0.5 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) assert cmmm.build_method_matrix(ast_wrapper) != None counter += 1
def test_filter_process(self, list_of_ast_class_nodes): weight_ssm = 0.7 weight_cdm = 0.3 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) min_coupling = random.random() ccctf = CrisCOMConstantThresholdFilter(min_coupling) for python_file, node, class_nodes in list_of_ast_class_nodes: for class_node in class_nodes: wrapper = AstClassWrapper(class_node) matrix = cmmm.build_method_matrix(wrapper) original = deepcopy(matrix.matrix) filtered_matrix = ccctf.filter_process(matrix) assert filtered_matrix != None assert not self.has_value_less_than(filtered_matrix.matrix, min_coupling)
def test_filter_process(self, cls_gen): w_ssm = 0.5 w_cdm = 0.5 min_coupling = 0.4 min_length = 2 metrics = [(StructuralSimilarityBetweenMethods(), w_ssm), (CallBasedDependenceBetweenMethods(), w_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) cmca = CrisMethodChainsAssembler() ccomct = CrisCOMConstantThresholdFilter(min_coupling) ctcm = CrisTrivialChainMerger(metrics, min_length) for simple_cls in cls_gen.generate(1000, 10, 10): cls_source = simple_cls.get_source_code() self.log("simple_cls source", cls_source) class_node = simple_cls.get_ast_node() class_wrapper = AstClassWrapper(class_node) method_matrix = cmmm.build_method_matrix(class_wrapper) method_matrix_matrix = method_matrix.get_matrix() custom_matrix = simple_cls.get_matrix(w_ssm, w_cdm) filtered_matrix = ccomct.filter_process(method_matrix) filtered_matrix_matrix = filtered_matrix.get_matrix() method_chains = cmca.filter_process(filtered_matrix) custom_filtered_matrix = simple_cls.filter_matrix(w_ssm, w_cdm, min_coupling) self.log("filtered matrix", print_matrix(custom_filtered_matrix)) assert custom_filtered_matrix == filtered_matrix_matrix # method_names = [[node.name for node in mc.method_ast_nodes] # for mc in method_chains] custom_chains = simple_cls.get_method_chains(w_ssm, w_cdm, min_coupling) custom_merged_chains = simple_cls.merge_trivial_chains( l_to_s(custom_chains), min_length, w_ssm, w_cdm ) merged_chains = ctcm.filter_process(method_chains) method_names = [x.get_method_names() for x in merged_chains] self.log("custom merged chains", print_chains(custom_merged_chains)) self.log("pipeline merged chains", print_chains(method_names)) assert len(custom_merged_chains) == len(method_names) assert self.compare_chains(custom_merged_chains, method_names)
def test_build_method_matrix_clsgen(self, cls_gen): weight_ssm = 0.5 weight_cdm = 0.5 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) for simple_cls in cls_gen.generate(100, 10, 10): cls_source = simple_cls.get_source_code() logging.info("TestCrisMethodByMethodMatrix::" + \ "test_build_method_matrix:simple_cls.source_code:\n" + \ cls_source) custom_matrix = simple_cls.get_matrix(weight_ssm, weight_cdm) ast_wrapper = AstClassWrapper(simple_cls.get_ast_node()) method_matrix = cmmm.build_method_matrix(ast_wrapper) logging.info("\ncustom_matrix:\n" + str( print_matrix(custom_matrix))) matrix = method_matrix.get_matrix() logging.info("\nmatrix_under_test:\n" + str(print_matrix(matrix))) assert matrix == custom_matrix
def test_filter_process(self, list_of_ast_class_nodes): weight_ssm = 0.7 weight_cdm = 0.3 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) ccctf = CrisCOMVariableThresholdFilter() for python_file, node, class_nodes in list_of_ast_class_nodes: for class_node in class_nodes: ast_dump = astpp.dump(class_node) wrapper = AstClassWrapper(class_node) matrix = cmmm.build_method_matrix(wrapper) original = deepcopy(matrix.matrix) min_coupling = -1 if len(original) > 0: min_coupling = CrisCOMVariableThresholdFilter.\ calculate_median(original) filtered_matrix = ccctf.filter_process(matrix) assert filtered_matrix != None assert not self.has_value_less_than(filtered_matrix.matrix, min_coupling)
def test_build_method_matrix(self, custom_python_code): ast_node = ast.parse(custom_python_code) class_finder = ClassFinder() class_finder.visit(ast_node) weight_ssm = 0.7 weight_cdm = 0.3 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) class_nodes = class_finder.class_nodes_dict matrix_factory = self.get_matrix_factory() #method_matrices = self.get_method_matrices() for class_name in class_nodes.keys(): custom_matrix = matrix_factory(class_name, weight_ssm, weight_cdm) class_node = class_nodes[class_name] ast_wrapper = AstClassWrapper(class_node) method_matrix = cmmm.build_method_matrix(ast_wrapper) matrix = method_matrix.matrix no_of_nodes = len(method_matrix.method_nodes) mm_weights = method_matrix.weights assert matrix == custom_matrix
def test_filter_process_with_cls_gen(self, cls_gen): weight_ssm = 0.5 weight_cdm = 0.5 min_coupling = 0.4 metrics = [(StructuralSimilarityBetweenMethods(), weight_ssm), (CallBasedDependenceBetweenMethods(), weight_cdm)] cmmm = CrisMethodByMethodMatrix(metrics) cmca = CrisMethodChainsAssembler() ccomct = CrisCOMConstantThresholdFilter(min_coupling) for simple_cls in cls_gen.generate(100, 10, 10): cls_source = simple_cls.get_source_code() # logging.info("TestCrisMethodChainAssembler::" + \ # "test_filter_process_with_cls_gen:simple_cls.source_code:\n" + \ # cls_source) class_node = simple_cls.get_ast_node() class_wrapper = AstClassWrapper(class_node) method_matrix = cmmm.build_method_matrix(class_wrapper) method_matrix_matrix = method_matrix.get_matrix() custom_matrix = simple_cls.get_matrix(weight_ssm, weight_cdm) filtered_matrix = ccomct.filter_process([method_matrix]) filtered_matrix_matrix = filtered_matrix.get_matrix() method_chains = cmca.filter_process(filtered_matrix) custom_filtered_matrix = simple_cls.filter_matrix(weight_ssm, weight_cdm, min_coupling) method_names = [[node.name for node in mc.method_ast_nodes] for mc in method_chains] custom_chains = simple_cls.get_method_chains(weight_ssm, weight_cdm, min_coupling) assert method_matrix_matrix != None assert method_matrix_matrix == custom_matrix assert custom_filtered_matrix == filtered_matrix_matrix # logging.info("filtered_matrix:\n" + print_matrix( # custom_filtered_matrix)) # logging.info("custom_chains:\n" + str(custom_chains)) # logging.info("method_names:\n" + str(method_names)) assert len(method_names) == len(custom_chains) assert self.compare_chains(method_names, custom_chains)