def test_keyword_diff(self): text = ['# todo 123\n'] comments = [SourceRange.from_values("F", 1, 1, 1, 10)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +0,0 @@\n' '-# todo 123\n') text = ['test = 55 # todo 123\n'] comments = [SourceRange.from_values("F", 1, 11, 1, 23)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-test = 55 # todo 123\n' '+test = 55\n')
def test_keyword_diff(self): text = ['# todo 123\n'] comments = [SourceRange.from_values('F', 1, 1, 1, 10)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +0,0 @@\n' '-# todo 123\n') text = ['test = 55 # todo 123\n'] comments = [SourceRange.from_values('F', 1, 11, 1, 23)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-test = 55 # todo 123\n' '+test = 55\n')
def test_keyword_in_comment(self): dep_results = { 'AnnotationBear': {} } text = ['# todo 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) self.assertEqual(len(result), 1) dep_results = {'AnnotationBear': HiddenResult( 'AnnotationBear', {'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) self.assertEqual(len(result), 1) dep_results = {'AnnotationBear': HiddenResult( 'AnnotationBear', 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) self.assertEqual(len(result), 1)
def test_keyword_between_code(self): self.section.append(Setting('language', 'c')) self.section.append(Setting('keywords', 'todo')) text = ['int a=0; /* TODO: Test */ int b=1;\n'] comments = [SourceRange.from_values("F", 1, 10, 1, 25)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-int a=0; /* TODO: Test */ int b=1;\n' '+int a=0; int b=1;\n') text = ['int a = 0; /* TODO test\n', 'another test\n', '*/\n'] comments = [SourceRange.from_values("F", 1, 12, 3, 2)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-int a = 0; /* TODO test\n' '+int a = 0; /*\n' ' another test\n' ' */\n') text = ['/* TODO\n', 'test\n', '*/\n'] comments = [SourceRange.from_values("F", 1, 1, 3, 2)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-/* TODO\n' '+/*\n' ' test\n' ' */\n')
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)), ) with execute_bear(self.c_uut, 'F', text) as result: self.assertEqual(result[0].contents['strings'], ()) self.assertEqual(result[0].contents['comments'], compare) text = ['/*Multiline which does not end'] with execute_bear(self.c_uut, 'F', text) as result: self.assertEqual(result[0].message, '/* has no closure')
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)),) with execute_bear(self.python_uut, 'F', text) as result: self.assertEqual(result[0].contents['strings'], compare) self.assertEqual(result[0].contents['comments'], ()) text = ["a'\n", "b'\n"] with execute_bear(self.python_uut, 'F', text) as result: self.assertEqual(result[0].message, "' has no closure")
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)),) with execute_bear(self.c_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], ()) self.assertEqual(result[0].contents['comments'], compare) text = ['/*Multiline which does not end'] with execute_bear(self.c_uut, "F", text) as result: self.assertEqual(result[0].message, '/* has no closure')
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)),) with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], compare) self.assertEqual(result[0].contents['comments'], ()) text = ["a'\n", "b'\n"] with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].message, "' has no closure")
def test_combined_strings(self): file_text = [ '"some string #with comment"\n', '"""\n', "now a multiline string ''' <- this one not\n", '"""\n', '"""\n' 'another comment # rather harmless\n', '"""\n' ] string1_start = 0 string1_end = len(file_text[0]) - 2 string1 = SourceRange.from_absolute_position( "F", AbsolutePosition(file_text, string1_start), AbsolutePosition(file_text, string1_end)) string2_start = string1_end + 2 text = ''.join(file_text) string2_end = text.find('"""', string2_start + 1) + 2 #+2 for length of """ string2 = SourceRange.from_absolute_position( "F", AbsolutePosition(file_text, string2_start), AbsolutePosition(file_text, string2_end)) string3_start = text.find('"""', string2_end + 1) string3_end = text.find('"""', string3_start + 1) + 2 #+2 for length of """ string3 = SourceRange.from_absolute_position( "F", 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'], ())
def test_combined_strings(self): file_text = ['"some string #with comment"\n', '"""\n', "now a multiline string ''' <- this one not\n", '"""\n', '"""\n' 'another comment # rather harmless\n', '"""\n'] string1_start = 0 string1_end = len(file_text[0]) - 2 string1 = SourceRange.from_absolute_position( "F", AbsolutePosition(file_text, string1_start), AbsolutePosition(file_text, string1_end)) string2_start = string1_end+2 text = ''.join(file_text) string2_end = text.find('"""', string2_start + 1) + 2 #+2 for length of """ string2 = SourceRange.from_absolute_position( "F", AbsolutePosition(file_text, string2_start), AbsolutePosition(file_text, string2_end)) string3_start = text.find('"""', string2_end + 1) string3_end = text.find('"""', string3_start + 1) + 2 #+2 for length of """ string3 = SourceRange.from_absolute_position( "F", 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'], ())
def test_no_coalang(self): self.section1.append(Setting('language', 'Valyrian')) text = ["Valar Morghulis"] uut = AnnotationBear(self.section1, Queue()) with execute_bear(uut, "F", text) as result: self.assertEqual(result[0].contents, "coalang specification for Valyrian not found.")
def test_keyword_outside_of_comment(self): text = ['todo = 123\n'] with execute_bear(self.uut, "F", text, dependency_results=self.dep_results) as result: self.assertEquals(result[0].diffs, {})
def test_no_coalang(self): self.section1.append(Setting('language', 'Valyrian')) text = ['Valar Morghulis'] uut = AnnotationBear(self.section1, Queue()) with execute_bear(uut, 'F', text) as result: self.assertEqual(result[0].contents, 'coalang specification for Valyrian not found.')
def test_empty_keyword(self): text = ['a == b'] with execute_bear(self.uut, "F", text, dependency_results=self.dep_results) as result: self.assertEqual(result, [])
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)), ) with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], ()) self.assertEqual(result[0].contents['comments'], compare)
def test_external_coalang(self): self.section1.append(Setting('coalang_dir', escape(os.path.join( os.path.dirname(__file__), 'test_files'), '\\'))) self.section1.append(Setting('language', 'test')) uut = AnnotationBear(self.section1, Queue()) text = ['//comment line 1\n', '"""string line 2"""'] with execute_bear(uut, "F", text) as result: self.assertNotEqual(result[0].contents['strings'], ()) self.assertNotEqual(result[0].contents['comments'], ())
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)),) with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], compare) self.assertEqual(result[0].contents['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)),) with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], ()) self.assertEqual(result[0].contents['comments'], compare)
def test_variable_spacing(self): content = (r'foo {{var }} bar', ) with execute_bear(self.uut, 'F', content) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-foo {{var }} bar' '+foo {{ var }} bar')
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)), ) with execute_bear(self.python_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], compare) self.assertEqual(result[0].contents['comments'], ())
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)), ) with execute_bear(self.c_uut, "F", text) as result: self.assertEqual(result[0].contents['strings'], ()) self.assertEqual(result[0].contents['comments'], compare)
def test_variable_spacing(self): content = (r'foo {{var }} bar',) with execute_bear(self.uut, 'F', content) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-foo {{var }} bar' '+foo {{ var }} bar')
def test_keyword_not_in_comment(self): text = ['# comment 123\n', 'a = "TODO"\n'] comments = [SourceRange.from_values("F", 1, 1, 1, 40)] dep_results = { 'AnnotationBear': [self.annotation_bear_result_type({'comments': comments})] } with execute_bear(self.uut, "F", text, dependency_results=dep_results) as result: self.assertEqual(len(result[0].diffs), 0)
def test_missing_label(self): content = [line + '\n' for line in bad_file1.splitlines()] with execute_bear(self.uut, 'F', content) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -2,4 +2,4 @@\n' ' foo\n' ' {% for x in something %}\n' ' render stuff\n' '-{% endfor %} more stuff\n' '+{% endfor %}{# for x in something #} more stuff\n')
def test_keyword_not_in_comment(self): text = ['# comment 123\n', 'a = "TODO"\n'] comments = [SourceRange.from_values('F', 1, 1, 1, 40)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(len(result[0].diffs), 0)
def test_wrong_label(self): content = [line + '\n' for line in bad_file2.splitlines()] with execute_bear(self.uut, 'F', content) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -2,4 +2,4 @@\n' ' foo\n' ' {% if x == something %}\n' ' render stuff\n' '-{% endif %}{# some random comment #} more stuff\n' '+{% endif %}{# if x == something #} more stuff\n')
def test_external_coalang(self): self.section1.append( Setting( 'coalang_dir', escape(os.path.join(os.path.dirname(__file__), 'test_files'), '\\'))) self.section1.append(Setting('language', 'test')) uut = AnnotationBear(self.section1, Queue()) text = ['//comment line 1\n', '"""string line 2"""'] with execute_bear(uut, "F", text) as result: self.assertNotEqual(result[0].contents['strings'], ()) self.assertNotEqual(result[0].contents['comments'], ())
def test_escape_strings(self): text = [r"'I\'ll be back' -T1000"] uut = AnnotationBear(self.section1, Queue()) test_range = SourceRange.from_absolute_position( '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,)) text = [''' """"quoting inside quoting" """ '''] uut = AnnotationBear(self.section1, Queue()) with execute_bear(uut, 'F', text) as results: for result in results: # The """" was recognized as a string start and end before. # That lead to a Result being yielded because of unclosed # quotes, this asserts that no such thing happened. self.assertEqual(type(result), HiddenResult)
def test_control_spacing(self): content = [ '{% for x in y %}\n', 'rendering stuff\n', '{% endfor%}{# for x in y #}\n' ] with execute_bear(self.uut, 'F', content) as result: self.assertEqual( result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' ' {% for x in y %}\n' ' rendering stuff\n' '-{% endfor%}{# for x in y #}\n' '+{% endfor %}{# for x in y #}\n')
def test_control_spacing(self): content = [ '{% for x in y %}\n', 'rendering stuff\n', '{% endfor%}{# for x in y #}\n' ] with execute_bear(self.uut, 'F', content) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' ' {% for x in y %}\n' ' rendering stuff\n' '-{% endfor%}{# for x in y #}\n' '+{% endfor %}{# for x in y #}\n')
def test_string_with_comments(self): text = ["some #comment\n", "with 'string' in next line"] comment_start = text[0].find('#') 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)), )] 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])
def test_keyword_in_comment(self): dep_results = {'AnnotationBear': {}} text = ['# todo 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) self.assertEqual(len(result), 1) dep_results = { 'AnnotationBear': HiddenResult('AnnotationBear', {'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) self.assertEqual(len(result), 1) dep_results = {'AnnotationBear': HiddenResult('AnnotationBear', 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) self.assertEqual(len(result), 1)
def test_string_with_comments(self): text = ["some #comment\n", "with 'string' in next line"] comment_start = text[0].find('#') 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)),)] 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])
def test_output(self): """ Validating that the yielded results are correct. """ affected_code = ( SourceRange.from_values(self.filename, start_line=111, start_column=1, end_line=143, end_column=2), ) expected_result = Result( self.bear, "The function 'levels(int, int, int)' should be simplified. Its " "cyclomatic complexity is 10 which exceeds maximal recommended " "value of 8.", affected_code=affected_code, ) with execute_bear(self.bear, self.filename, self.file, 8) as out: self.assertEqual(len(out), 1) out[0].additional_info = "" # Let's not test this, static and huge self.assertEqual(out[0], expected_result)
def test_output(self): """ Validating that the yielded results are correct. """ affected_code = (SourceRange.from_values(self.filename, start_line=111, start_column=1, end_line=143, end_column=2), ) expected_result = Result( self.bear, "The function 'levels(int, int, int)' should be simplified. Its " "cyclomatic complexity is 10 which exceeds maximal recommended " "value of 8.", affected_code=affected_code) with execute_bear(self.bear, self.filename, self.file, 8) as out: self.assertEqual(len(out), 1) out[0].additional_info = "" # Let's not test this, static and huge self.assertEqual(out[0], expected_result)
def test_empty_keyword(self): text = ['a == b'] with execute_bear(self.uut, 'F', text, dependency_results=self.dep_results) as result: self.assertEqual(result, [])
def test_keyword_between_code(self): self.section.append(Setting('language', 'c')) self.section.append(Setting('keywords', 'todo')) text = ['int a=0; /* TODO: Test */ int b=1;\n'] comments = [SourceRange.from_values('F', 1, 10, 1, 25)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1 +1 @@\n' '-int a=0; /* TODO: Test */ int b=1;\n' '+int a=0; int b=1;\n') text = ['int a = 0; /* TODO test\n', 'another test\n', '*/\n'] comments = [SourceRange.from_values('F', 1, 12, 3, 2)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-int a = 0; /* TODO test\n' '+int a = 0; /*\n' ' another test\n' ' */\n') text = ['/* TODO\n', 'test\n', '*/\n'] comments = [SourceRange.from_values('F', 1, 1, 3, 2)] dep_results = { 'AnnotationBear': [ self.annotation_bear_result_type({'comments': comments}) ] } with execute_bear(self.uut, 'F', text, dependency_results=dep_results) as result: self.assertEqual(result[0].diffs['F'].unified_diff, '--- \n' '+++ \n' '@@ -1,3 +1,3 @@\n' '-/* TODO\n' '+/*\n' ' test\n' ' */\n')
def test_keyword_outside_of_comment(self): text = ['todo = 123\n'] with execute_bear(self.uut, 'F', text, dependency_results=self.dep_results) as result: self.assertEquals(result[0].diffs, {})