Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 9, 2020
2 parents 5681de3 + ef9744b commit 50cfa0a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
30 changes: 30 additions & 0 deletions aibolit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ def flatten(l):
return [item for sublist in l for item in sublist]


def add_pattern_if_ignored(
dct: Dict[str, Any],
pattern_item: Dict[Any, Any],
results_list: List[Any]) -> None:
""" If pattern code is not ignored, add it to the result list
:param dct: dict, where key is pattern, value is list of lines range to ignore
:param pattern_item: pattern dict which was get after `inference` function
:param results_list: result list to add
"""
ignored_lines = dct.get(pattern_item['pattern_code'])
if ignored_lines:
for place in ignored_lines:
# get lines range of ignored code
start_line_to_ignore = place[0]
end_line_to_ignore = place[1]
new_code_lines = []
for line in pattern_item['code_lines']:
if (line >= start_line_to_ignore) and (line <= end_line_to_ignore):
continue
else:
new_code_lines.append(line)
pattern_item['code_lines'] = new_code_lines
if len(pattern_item['code_lines']) > 0:
results_list.append(pattern_item)
else:
results_list.append(pattern_item)


def find_annotation_by_node_type(
tree: javalang.tree.CompilationUnit,
node_type) -> Dict[Any, Any]:
Expand Down
23 changes: 22 additions & 1 deletion test/recommend/test_recommend_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from aibolit.config import Config

from aibolit.__main__ import list_dir, calculate_patterns_and_metrics, \
create_xml_tree, create_text, format_converter_for_pattern, find_start_and_end_lines, find_annotation_by_node_type
create_xml_tree, create_text, format_converter_for_pattern, find_start_and_end_lines, \
find_annotation_by_node_type, add_pattern_if_ignored


class TestRecommendPipeline(TestCase):
Expand Down Expand Up @@ -287,3 +288,23 @@ def test_find_annotation_by_field_declaration(self):
fields_with_annotations = find_annotation_by_node_type(tree, javalang.tree.FieldDeclaration)
patterns_found_with_fields = list(fields_with_annotations.values())
self.assertEqual(patterns_found_with_fields, [['P23'], ['P23', 'P22']])

def test_pattern_ignore(self):
pattern_item = {'code_lines': [20],
'pattern_code': 'P13',
'pattern_name': 'Null check',
'importance': 30.95612931128819}
results = []
pattern_ignored = {'P13': [[10, 20]]}
add_pattern_if_ignored(pattern_ignored, pattern_item, results)
self.assertEqual(results, [])

def test_pattern_not_ignore(self):
pattern_item = {'code_lines': [20, 30],
'pattern_code': 'P14',
'pattern_name': 'Null check',
'importance': 30.95612931128819}
results = []
pattern_ignored = {'P14': [[60, 100]]}
add_pattern_if_ignored(pattern_ignored, pattern_item, results)
self.assertEqual(results[0]['code_lines'], pattern_item['code_lines'])

0 comments on commit 50cfa0a

Please sign in to comment.