From 26ccdf873847d2cb2fd6b0b0c8ca2907d1bc96d8 Mon Sep 17 00:00:00 2001 From: aptrishu Date: Fri, 20 Jan 2017 06:21:00 +0530 Subject: [PATCH] AnnotationBear: Return precise string/comment info Earlier the return used to be either a tuple of source ranges of strings/comments now it is a tuple of namedtuples with fields "start_delimiter_range end_delimiter_range content_range full_range" of singleline strings/multiline strings/singleline comments/multiline comments. Also, It modifies KeywordBear, QuotesBear, IndentationBear and their tests to adapt to AnnotationBear changes. --- bears/general/AnnotationBear.py | 239 ++++++++++++++++++---------- bears/general/IndentationBear.py | 43 ++--- bears/general/KeywordBear.py | 7 +- bears/general/QuotesBear.py | 21 ++- tests/general/AnnotationBearTest.py | 93 ++++++----- tests/general/KeywordBearTest.py | 24 +-- tests/general/QuotesBearTest.py | 25 +-- 7 files changed, 282 insertions(+), 170 deletions(-) diff --git a/bears/general/AnnotationBear.py b/bears/general/AnnotationBear.py index 17dc53a897..9c0f6d4f08 100644 --- a/bears/general/AnnotationBear.py +++ b/bears/general/AnnotationBear.py @@ -1,3 +1,5 @@ +from collections import namedtuple + from coalib.bearlib.languages.LanguageDefinition import LanguageDefinition from coalib.bears.LocalBear import LocalBear from coalib.results.HiddenResult import HiddenResult @@ -23,10 +25,13 @@ def run(self, filename, file, language: str, coalang_dir: str = None): :param coalang_dir: External directory for coalang file. :return: - One HiddenResult containing a dictionary with keys being 'strings' - or 'comments' and values being a tuple of SourceRanges pointing to - the strings and a tuple of SourceRanges pointing to all comments - respectively. The ranges do include string quotes or the comment + One HiddenResult containing a dictionary with keys being + 'singleline strings', 'multiline strings', 'singleline comments' + and 'multiline comments' and values being a tuple of + namedtuples of the format: ```("CodeAnnotation", + "start_delimiter_range end_delimiter_range + content_range full_range")```. + The ranges do include string quotes or the comment starting separator but not anything before (e.g. when using ``u"string"``, the ``u`` will not be in the source range). """ @@ -44,9 +49,9 @@ def run(self, filename, file, language: str, coalang_dir: str = None): multiline_comment_delimiters = dict( lang_dict['multiline_comment_delimiters']) comment_delimiter = dict(lang_dict['comment_delimiter']) - string_ranges = comment_ranges = () + ranges = ((), (), (), ()) try: - string_ranges, comment_ranges = self.find_annotation_ranges( + ranges = self.find_annotation_ranges( file, filename, string_delimiters, @@ -58,7 +63,10 @@ def run(self, filename, file, language: str, coalang_dir: str = None): yield Result(self, str(e), severity=RESULT_SEVERITY.MAJOR, affected_code=(e.code,)) - content = {'strings': string_ranges, 'comments': comment_ranges} + content = {'singleline strings': ranges[0], + 'multiline strings': ranges[1], + 'singleline comments': ranges[2], + 'multiline comments': ranges[3]} yield HiddenResult(self, content) def find_annotation_ranges(self, @@ -88,66 +96,111 @@ def find_annotation_ranges(self, A dictionary containing the various ways to define multi-line comments in a language. :return: - Two tuples first containing a tuple of strings, the second a tuple - of comments. + Four tuples containing namedtuples for singleline strings, + multiline strings, singleline comments and multiline comments + respectively. """ text = ''.join(file) - strings_range = [] - comments_range = [] + singleline_string_range = [] + multiline_string_range = [] + singleline_comment_range = [] + multiline_comment_range = [] + fields = ('start_delimiter_range end_delimiter_range' + ' content_range full_range') + singleline_string = namedtuple('singleline_string', fields) + multiline_string = namedtuple('multiline_string', fields) + singleline_comment = namedtuple('singleline_string', fields) + multiline_comment = namedtuple('multiline_string', fields) position = 0 while position <= len(text): def get_new_position(): - _range, end_position = self.get_range_end_position( - file, - filename, - text, - multiline_string_delimiters, - position, - self.get_multiline) - if end_position and _range: - strings_range.append(_range) + end_position, start_delim, end_delim =\ + self.get_range_end_position( + file, + filename, + text, + multiline_string_delimiters, + position, + self.get_multiline) + if end_position: + seperate_ranges = get_seperate_ranges(file, + filename, + start_delim, + end_delim, + position, + end_position) + multiline_string_range.append( + multiline_string(*seperate_ranges)) + return end_position + 1 - _range, end_position = self.get_range_end_position( - file, - filename, - text, - string_delimiters, - position, - self.get_singleline_strings) - if end_position and _range: - strings_range.append(_range) + end_position, start_delim, end_delim =\ + self.get_range_end_position( + file, + filename, + text, + string_delimiters, + position, + self.get_singleline_strings) + if end_position: + seperate_ranges = get_seperate_ranges(file, + filename, + start_delim, + end_delim, + position, + end_position) + singleline_string_range.append( + singleline_string(*seperate_ranges)) return end_position + 1 - _range, end_position = self.get_range_end_position( - file, - filename, - text, - multiline_comment_delimiters, - position, - self.get_multiline) - if end_position and _range: - comments_range.append(_range) + end_position, start_delim, end_delim =\ + self.get_range_end_position( + file, + filename, + text, + multiline_comment_delimiters, + position, + self.get_multiline) + if end_position: + seperate_ranges = get_seperate_ranges(file, + filename, + start_delim, + end_delim, + position, + end_position) + multiline_comment_range.append( + multiline_comment(*seperate_ranges)) return end_position + 1 - _range, end_position = self.get_range_end_position( - file, - filename, - text, - comment_delimiter, - position, - self.get_singleline_comment, - single_comment=True) - if end_position and _range: - comments_range.append(_range) + end_position, start_delim, end_delim =\ + self.get_range_end_position( + file, + filename, + text, + comment_delimiter, + position, + self.get_singleline_comment, + single_comment=True) + if end_position: + seperate_ranges = get_seperate_ranges(file, + filename, + start_delim, + end_delim, + position, + end_position) + singleline_comment_range.append( + singleline_comment(*seperate_ranges)) return end_position + 1 return position + 1 position = get_new_position() - return tuple(strings_range), tuple(comments_range) + return (tuple(singleline_string_range), + tuple(multiline_string_range), + tuple(singleline_comment_range), + tuple(multiline_comment_range)) @staticmethod def get_range_end_position(file, @@ -157,26 +210,27 @@ def get_range_end_position(file, position, func, single_comment=False): - _range = end_position = None + selected_annotation = end_position = selected_end_annotation = None for annotation in annotations.keys(): if text[position:].startswith(annotation): + selected_annotation = annotation if not single_comment: - ret_val = func(file, - filename, - text, - annotation, - annotations[annotation], - position) + selected_end_annotation = annotations[selected_annotation] + end_position = func(file, + filename, + text, + annotation, + annotations[annotation], + position) else: - ret_val = func(file, - filename, - text, - annotation, - position) - if ret_val: - _range, end_position = ret_val[0], ret_val[1] + selected_end_annotation = '\n' + end_position = func(file, + filename, + text, + annotation, + position) - return _range, end_position + return end_position, selected_annotation, selected_end_annotation @staticmethod def get_multiline(file, @@ -200,8 +254,7 @@ def get_multiline(file, :param position: An integer identifying the position where the annotation started. :return: - A SourceRange object holding the range of the multi-line annotation - and the end_position of the annotation as an integer. + The end_position of the annotation as an integer. """ end_end = get_end_position(annotation_end, text, @@ -212,11 +265,7 @@ def get_multiline(file, AbsolutePosition(file, position)) raise NoCloseError(annotation_start, _range) - return (SourceRange.from_absolute_position( - filename, - AbsolutePosition(file, position), - AbsolutePosition(file, end_end)), - end_end) + return end_end @staticmethod def get_singleline_strings(file, @@ -239,8 +288,7 @@ def get_singleline_strings(file, :position: An integer identifying the position where the string started. :return: - A SourceRange object identifying the range of the single-line - string and the end_position of the string as an integer. + The end_position of the string as an integer. """ end_position = get_end_position(string_end, text, @@ -254,11 +302,7 @@ def get_singleline_strings(file, AbsolutePosition(file, position)) raise NoCloseError(string_start, _range) if newline > end_position: - return (SourceRange.from_absolute_position( - filename, - AbsolutePosition(file, position), - AbsolutePosition(file, end_position)), - end_position) + return end_position @staticmethod def get_singleline_comment(file, filename, text, comment, position): @@ -275,19 +319,14 @@ def get_singleline_comment(file, filename, text, comment, position): :position: An integer identifying the position where the string started. :return: - A SourceRange object identifying the range of the single-line - comment and the end_position of the comment as an integer. + The end_position of the comment as an integer. """ end_position = get_end_position('\n', text, position + len(comment) - 1) if end_position == -1: end_position = len(text) - 1 - return (SourceRange.from_absolute_position( - filename, - AbsolutePosition(file, position), - AbsolutePosition(file, end_position)), - end_position) + return end_position def get_end_position(end_marker, text, position): @@ -300,6 +339,36 @@ def get_end_position(end_marker, text, position): return end_position +def get_seperate_ranges(file, + filename, + start_delim, + end_delim, + start_position, + end_position): + ranges = [] + ranges.append(SourceRange.from_absolute_position( + filename, + AbsolutePosition(file, start_position), + AbsolutePosition(file, start_position + len(start_delim) - 1))) + + ranges.append(SourceRange.from_absolute_position( + filename, + AbsolutePosition(file, end_position - len(end_delim) + 1), + AbsolutePosition(file, end_position))) + + ranges.append(SourceRange.from_absolute_position( + filename, + AbsolutePosition(file, start_position + len(start_delim)), + AbsolutePosition(file, end_position - len(end_delim)))) + + ranges.append(SourceRange.from_absolute_position( + filename, + AbsolutePosition(file, start_position), + AbsolutePosition(file, end_position))) + + return ranges + + class NoCloseError(Exception): def __init__(self, annotation, code): diff --git a/bears/general/IndentationBear.py b/bears/general/IndentationBear.py index 2f37892874..af40203f7a 100644 --- a/bears/general/IndentationBear.py +++ b/bears/general/IndentationBear.py @@ -67,7 +67,12 @@ def run(self, """ lang_settings_dict = LanguageDefinition( language, coalang_dir=coalang_dir) - annotation_dict = dependency_results[AnnotationBear.name][0].contents + dep_contents = dependency_results[AnnotationBear.name][0].contents + annotation_dict = {} + annotation_dict['strings'] = (dep_contents['singleline strings'] + + dep_contents['multiline strings']) + annotation_dict['comments'] = (dep_contents['singleline comments'] + + dep_contents['multiline comments']) # sometimes can't convert strings with ':' to dict correctly if ':' in dict(lang_settings_dict['indent_types']).keys(): indent_types = dict(lang_settings_dict['indent_types']) @@ -168,7 +173,7 @@ def get_absolute_indent_of_range(self, :param encaps_pos: A tuple ofSourceRanges of code regions trapped in between a matching pair of encapsulators. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :return: A tuple of tuples with first element as the range of encapsulator and second element as the @@ -196,7 +201,7 @@ def get_indent_levels(self, :param filename: Name of the file that needs to be checked. :param indent_types: A dictionary with keys as start of indent and values as their corresponding closing indents. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :param encapsulators: A tuple of sourceranges of all encapsulators of a language. @@ -255,7 +260,7 @@ def get_specified_block_range(self, block has begun. :param close_specifier: A character or string indicating that the block has ended. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :return: A tuple whith the first source range being the range of the outermost indentation while @@ -324,7 +329,7 @@ def get_unspecified_block_range(self, :param filename: Name of the file that needs to be checked. :param indent_specifier: A character or string indicating that the indentation should begin. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :param encapsulators: A tuple of sourceranges of all encapsulators of a language. @@ -372,7 +377,7 @@ def get_valid_sequences(file, :param file: File that needs to be checked in the form of a list of strings. :param sequence: Sequence whose validity is to be checked. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :param encapsulators: A tuple of SourceRanges of code regions trapped in between a matching pair of @@ -394,22 +399,22 @@ def get_valid_sequences(file, # ignore if within string for string in annotation_dict['strings']: - if(gt_eq(sequence_position, string.start) and - lt_eq(sequence_position, string.end)): + if(gt_eq(sequence_position, string.full_range.start) and + lt_eq(sequence_position, string.full_range.end)): valid = False # ignore if within comments for comment in annotation_dict['comments']: - if(gt_eq(sequence_position, comment.start) and - lt_eq(sequence_position, comment.end)): + if(gt_eq(sequence_position, comment.full_range.start) and + lt_eq(sequence_position, comment.full_range.end)): valid = False - if(comment.start.line == sequence_position.line and - comment.end.line == sequence_position.line and - check_ending): - sequence_line_text = sequence_line_text[ - :comment.start.column - 1] + sequence_line_text[ - comment.end.column-1:] + if(comment.full_range.start.line == sequence_position.line and + comment.full_range.end.line == sequence_position.line + and check_ending): + sequence_line_text = (sequence_line_text[ + :comment.full_range.start.column - 1] + + sequence_line_text[comment.full_range.end.column-1:]) if encapsulators: for encapsulator in encapsulators: @@ -462,7 +467,7 @@ def get_first_unindent(indent, :param file: A tuple of strings. :param start_line: The line from where to start searching for unindent. - :param annotation_dict: A dictionary containing sourceranges of all the + :param annotation_dict: A dictionary containing namedtuples of all the strings and comments within a file. :param encapsulators: A tuple of SourceRanges of code regions trapped in between a matching pair of encapsulators. @@ -476,8 +481,8 @@ def get_first_unindent(indent, valid = True for comment in annotation_dict['comments']: - if(comment.start.line < line_nr + 1 and - comment.end.line >= line_nr + 1): + if(comment.full_range.start.line < line_nr + 1 and + comment.full_range.end.line >= line_nr + 1): valid = False first_char = file[line_nr].lstrip()[0] if file[line_nr].strip()\ diff --git a/bears/general/KeywordBear.py b/bears/general/KeywordBear.py index bb81ae6e61..26c4d4a60d 100644 --- a/bears/general/KeywordBear.py +++ b/bears/general/KeywordBear.py @@ -23,7 +23,12 @@ def _get_comments(dependency_results): if isinstance(result.contents, str): logging.error(result.contents) else: - yield from result.contents.get('comments', []) + dep_contents = result.contents + annotation_dict = {} + annotation_dict['comments'] = \ + (dep_contents.get('singleline comments', []) + + dep_contents.get('multiline comments', [])) + yield from annotation_dict['comments'] def generate_diff(comments, file, filename, diff --git a/bears/general/QuotesBear.py b/bears/general/QuotesBear.py index f15806eb7f..add3e88151 100644 --- a/bears/general/QuotesBear.py +++ b/bears/general/QuotesBear.py @@ -62,21 +62,28 @@ def run(self, filename, file, dependency_results, :param preferred_quotation: Your preferred quotation character, e.g. ``"`` or ``'``. """ - if not isinstance(dependency_results[AnnotationBear.name][0], + if not isinstance(dependency_results[AnnotationBear.name], HiddenResult): return - if isinstance(dependency_results[AnnotationBear.name][0].contents, + if isinstance(dependency_results[AnnotationBear.name].contents, str): - self.err(dependency_results[AnnotationBear.name][0].contents) + self.err(dependency_results[AnnotationBear.name].contents) return - ranges = dependency_results[AnnotationBear.name][0].contents['strings'] + dep_contents = dependency_results[AnnotationBear.name].contents + annotation_dict = {} + s_a = 'singleline strings' + s_b = 'multiline strings' + annotation_dict['strings'] = (dep_contents[s_a] + + dep_contents[s_b]) + ranges = annotation_dict['strings'] for string_range in ranges: - if (file[string_range.start.line-1][string_range.start.column-1] == + temp_range = string_range.full_range + if (file[temp_range.start.line-1][temp_range.start.column-1] == preferred_quotation): continue - if string_range.start.line == string_range.end.line: + if temp_range.start.line == temp_range.end.line: yield from self.correct_single_line_str( - filename, file, string_range, preferred_quotation) + filename, file, temp_range, preferred_quotation) diff --git a/tests/general/AnnotationBearTest.py b/tests/general/AnnotationBearTest.py index c26d7b154c..12b43c75cc 100644 --- a/tests/general/AnnotationBearTest.py +++ b/tests/general/AnnotationBearTest.py @@ -1,4 +1,5 @@ from queue import Queue +from collections import namedtuple import unittest from bears.general.AnnotationBear import AnnotationBear @@ -22,13 +23,14 @@ def setUp(self): def test_single_line_string(self): text = ["'from start till the end with #comments'\n", ] - compare = (SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, 0), - AbsolutePosition(text, len(text[0]) - 2)),) + compare = SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, 0), + AbsolutePosition(text, len(text[0]) - 2)) with execute_bear(self.python_uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], compare) - self.assertEqual(result[0].contents['comments'], ()) + for _result in result[0].contents['singleline strings']: + self.assertEqual(_result.full_range, compare) + self.assertEqual(result[0].contents['singleline comments'], ()) text = ["a'\n", "b'\n"] with execute_bear(self.python_uut, 'F', text) as result: @@ -36,33 +38,36 @@ def test_single_line_string(self): def test_multiline_string(self): text = ["'''multiline string, #comment within it'''\n"] - compare = (SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, 0), - AbsolutePosition(text, len(text[0])-2)),) + compare = SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, 0), + AbsolutePosition(text, len(text[0])-2)) with execute_bear(self.python_uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], compare) - self.assertEqual(result[0].contents['comments'], ()) + for _result in result[0].contents['multiline strings']: + self.assertEqual(_result.full_range, compare) + self.assertEqual(result[0].contents['singleline comments'], ()) def test_single_line_comment(self): text = ["some #coment with 'string'\n", 'and next line'] - compare = (SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, text[0].find('#')), - AbsolutePosition(text, len(text[0]) - 1)),) + compare = SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, text[0].find('#')), + AbsolutePosition(text, len(text[0]) - 1)) with execute_bear(self.python_uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], ()) - self.assertEqual(result[0].contents['comments'], compare) + self.assertEqual(result[0].contents['singleline strings'], ()) + for _result in result[0].contents['singleline comments']: + self.assertEqual(_result.full_range, compare) def test_multiline_comment(self): text = ['some string /*within \n', "'multiline comment'*/"] - compare = (SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, text[0].find('/*')), - AbsolutePosition(text, len(''.join(text)) - 1)),) + compare = SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, text[0].find('/*')), + AbsolutePosition(text, len(''.join(text)) - 1)) with execute_bear(self.c_uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], ()) - self.assertEqual(result[0].contents['comments'], compare) + self.assertEqual(result[0].contents['singleline strings'], ()) + for _result in result[0].contents['multiline comments']: + self.assertEqual(_result.full_range, compare) text = ['/*Multiline which does not end'] with execute_bear(self.c_uut, 'F', text) as result: @@ -74,17 +79,19 @@ def test_string_with_comments(self): comment_end = len(text[0]) - 1 string_start = ''.join(text).find("'") string_end = ''.join(text).find("'", string_start + 1) - compare = [(SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, string_start), - AbsolutePosition(text, string_end)),), - (SourceRange.from_absolute_position( - 'F', - AbsolutePosition(text, comment_start), - AbsolutePosition(text, comment_end)),)] + compare = [SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, string_start), + AbsolutePosition(text, string_end)), + SourceRange.from_absolute_position( + 'F', + AbsolutePosition(text, comment_start), + AbsolutePosition(text, comment_end))] with execute_bear(self.python_uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], compare[0]) - self.assertEqual(result[0].contents['comments'], compare[1]) + for _result in result[0].contents['singleline strings']: + self.assertEqual(_result.full_range, compare[0]) + for _result in result[0].contents['singleline comments']: + self.assertEqual(_result.full_range, compare[1]) def test_combined_strings(self): file_text = ['"some string #with comment"\n', @@ -116,10 +123,15 @@ def test_combined_strings(self): AbsolutePosition(file_text, string3_start), AbsolutePosition(file_text, string3_end)) with execute_bear(self.python_uut, 'F', file_text) as results: - self.assertIn(string1, results[0].contents['strings']) - self.assertIn(string2, results[0].contents['strings']) - self.assertIn(string3, results[0].contents['strings']) - self.assertEqual(results[0].contents['comments'], ()) + full_ranges = [] + for _result in (results[0].contents['singleline strings'] + + results[0].contents['multiline strings']): + full_ranges.append(_result.full_range) + self.assertIn(string1, full_ranges) + self.assertIn(string2, full_ranges) + self.assertIn(string3, full_ranges) + self.assertEqual(results[0].contents['multiline comments'], ()) + self.assertEqual(results[0].contents['singleline comments'], ()) def test_no_coalang(self): self.section1.append(Setting('language', 'Valyrian')) @@ -136,8 +148,11 @@ def test_escape_strings(self): 'F', AbsolutePosition(text, 0), AbsolutePosition(text, text[0].find("'", 4))) + with execute_bear(uut, 'F', text) as result: - self.assertEqual(result[0].contents['strings'], (test_range,)) + self.assertEqual( + result[0].contents['singleline strings'][0].full_range, + test_range) text = [''' """"quoting inside quoting" diff --git a/tests/general/KeywordBearTest.py b/tests/general/KeywordBearTest.py index 1389a41073..6c4c38cc38 100644 --- a/tests/general/KeywordBearTest.py +++ b/tests/general/KeywordBearTest.py @@ -35,7 +35,7 @@ def setUp(self): self.annotation_bear_result_type = namedtuple('result', ['contents']) self.dep_results = {'AnnotationBear': HiddenResult( - 'AnnotationBear', {'comments': ()})} + 'AnnotationBear', {'singleline comments': ()})} def test_empty_keyword(self): text = ['a == b'] @@ -56,8 +56,8 @@ def test_keyword_in_comment(self): self.assertEqual(len(result), 1) dep_results = {'AnnotationBear': HiddenResult( - 'AnnotationBear', {'comments': 123})} - with execute_bear(self.uut, filename='F', file=text, + 'AnnotationBear', {'singleline comments': 123})} + with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs, {}) self.assertEqual(result[0].affected_code[0].start.line, 1) @@ -76,7 +76,8 @@ def test_keyword_not_in_comment(self): comments = [SourceRange.from_values('F', 1, 1, 1, 40)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'singleline comments': comments}) ] } @@ -89,7 +90,8 @@ def test_keyword_diff(self): comments = [SourceRange.from_values('F', 1, 1, 1, 10)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'singleline comments': comments}) ] } @@ -105,7 +107,8 @@ def test_keyword_diff(self): comments = [SourceRange.from_values('F', 1, 11, 1, 23)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'singleline comments': comments}) ] } with execute_bear(self.uut, filename='F', file=text, @@ -132,7 +135,8 @@ def test_keyword_between_code(self): comments = [SourceRange.from_values('F', 1, 10, 1, 25)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'singleline comments': comments}) ] } @@ -151,7 +155,8 @@ def test_keyword_between_code(self): comments = [SourceRange.from_values('F', 1, 12, 3, 2)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'multiline comments': comments}) ] } @@ -172,7 +177,8 @@ def test_keyword_between_code(self): comments = [SourceRange.from_values('F', 1, 1, 3, 2)] dep_results = { 'AnnotationBear': [ - self.annotation_bear_result_type({'comments': comments}) + self.annotation_bear_result_type( + {'multiline comments': comments}) ] } diff --git a/tests/general/QuotesBearTest.py b/tests/general/QuotesBearTest.py index e7d4f9ddba..3fa357d706 100644 --- a/tests/general/QuotesBearTest.py +++ b/tests/general/QuotesBearTest.py @@ -1,11 +1,13 @@ import unittest from queue import Queue from textwrap import dedent +from collections import namedtuple from coalib.results.HiddenResult import HiddenResult, Result from bears.general.QuotesBear import QuotesBear from coalib.results.SourceRange import SourceRange from coalib.settings.Section import Section +from bears.general.AnnotationBear import AnnotationBear from coalib.testing.LocalBearTestHelper import execute_bear @@ -32,26 +34,29 @@ def setUp(self): """).splitlines(True) self.filename = 'f' - + x = namedtuple('x', 'full_range') + mult = [] + mult.append(x(SourceRange.from_values(self.filename, 2, 1, 4, 3))) + sing = [] + sing.append(x(SourceRange.from_values(self.filename, 5, 1, 5, 30))) + sing.append(x(SourceRange.from_values(self.filename, 6, 1, 6, 37))) self.dep_results = { - 'AnnotationBear': - [HiddenResult( + AnnotationBear.name: + HiddenResult( 'AnnotationBear', - {'comments': (), 'strings': ( - SourceRange.from_values(self.filename, 2, 1, 4, 3), - SourceRange.from_values(self.filename, 5, 1, 5, 30), - SourceRange.from_values(self.filename, 6, 1, 6, 37)) + {'multiline strings': (tuple(mult)), + 'singleline strings': (tuple(sing)) } - )] + ) } def test_error_handling(self): - dep_results = {'AnnotationBear': [Result('test', 'test')]} + dep_results = {AnnotationBear.name: Result('test', 'test')} with execute_bear(self.uut, self.filename, self.double_quote_file, dependency_results=dep_results) as results: self.assertEqual(len(results), 0) - dep_results = {'AnnotationBear': [HiddenResult('a', 'error!')]} + dep_results = {AnnotationBear.name: HiddenResult('a', 'error!')} with execute_bear(self.uut, self.filename, self.double_quote_file, dependency_results=dep_results) as results: self.assertEqual(len(results), 0)