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, filename='F', file=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, filename='F', file=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, filename='F', file=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, filename='F', file=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, filename='F', file=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_invalid_quotes(self): with execute_bear(self.uut, self.filename, self.single_quote_file, dependency_results=self.dep_results) as results: res_list = list(results) self.assertEqual(len(res_list), 1) self.assertEqual(res_list[0].diffs[self.filename].unified_diff, '--- \n' '+++ \n' '@@ -2,5 +2,5 @@\n' " '''\n" ' Multiline string\n' " '''\n" "-'a string with single quotes!'\n" '+"a string with single quotes!"\n' ' "A double quoted string with \' in it"\n') self.section['preferred_quotation'] = "'" with execute_bear(self.uut, self.filename, self.double_quote_file, dependency_results=self.dep_results) as results: res_list = list(results) self.assertEqual(len(res_list), 1) self.assertEqual(res_list[0].diffs[self.filename].unified_diff, '--- \n' '+++ \n' '@@ -2,5 +2,5 @@\n' " '''\n" ' Multiline string\n' " '''\n" '-"a string with double quotes!"\n' "+'a string with double quotes!'\n" " 'A single quoted string with \" in it'\n")
def test_keyword_in_comment(self): dep_results = {'AnnotationBear': {}} text = ['# todo 123'] with execute_bear(self.uut, filename='F', file=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, filename='F', file=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, filename='F', file=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_regex(self): text = [ '# add two given values and result the result\n', 'def add(a, b):', ' return a+b\n', ' \n', 'print(add(2, 3))\n' ] regex_keyword = 'r.s.l.' with execute_bear(self.uut, filename='F', file=text, regex_keyword=regex_keyword, dependency_results=self.dep_results) as result: self.assertEqual( result[0].message, 'The line contains the keyword' " 'result' which resulted in a" ' match with given regex.') text = ['# bla bla bla', 'Issue #123', 'bla bla bla'] regex_keyword = '[iI]ssue #[1-9][0-9]*' with execute_bear(self.uut, filename='F', file=text, regex_keyword=regex_keyword, dependency_results=self.dep_results) as result: self.assertEqual( result[0].message, 'The line contains the keyword' " 'Issue #123' which resulted " 'in a match with given regex.')
def test_keyword_regex(self): text = ['# add two given values and result the result\n', 'def add(a, b):', ' return a+b\n', ' \n', 'print(add(2, 3))\n'] regex_keyword = 'r.s.l.' with execute_bear(self.uut, filename='F', file=text, regex_keyword=regex_keyword, dependency_results=self.dep_results) as result: self.assertEqual(result[0].message, 'The line contains the keyword' " 'result' which resulted in a" ' match with given regex.') text = ['# bla bla bla', 'Issue #123', 'bla bla bla'] regex_keyword = '[iI]ssue #[1-9][0-9]*' with execute_bear(self.uut, filename='F', file=text, regex_keyword=regex_keyword, dependency_results=self.dep_results) as result: self.assertEqual(result[0].message, 'The line contains the keyword' " 'Issue #123' which resulted " 'in a match with given regex.')
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_valid_quotes(self): with execute_bear(self.uut, self.filename, self.double_quote_file, dependency_results=self.dep_results) as results: self.assertEqual(len(results), 0) self.section['preferred_quotation'] = "'" with execute_bear(self.uut, self.filename, self.single_quote_file, dependency_results=self.dep_results) as results: self.assertEqual(len(results), 0)
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_error_handling(self): dep_results = {'AnnotationBear': [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!')]} with execute_bear(self.uut, self.filename, self.double_quote_file, dependency_results=dep_results) as results: self.assertEqual(len(results), 0)
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_stdout_stderr_on_linter_test_fail(self): class TestLinter: @staticmethod def process_output(output, filename, file): pass @staticmethod def create_arguments(filename, file, config_file): code = '\n'.join([ 'import sys', "print('hello stdout')", "print('hello stderr', file=sys.stderr)" ]) return '-c', code # Testing with both stdout and stderr enabled uut = (linter(sys.executable, use_stdout=True, use_stderr=True)(TestLinter)(Section('TEST_SECTION'), Queue())) try: with execute_bear(uut, 'filename', ['file']) as result: raise AssertionError except AssertionError as ex: self.assertIn( 'Program arguments:\n' "('-c', \"import sys\\n" "print('hello stdout')\\n" "print('hello stderr', file=sys.stderr)\")\n" 'The program yielded the following output:\n\n' 'Stdout:\n' 'hello stdout\n\n' 'Stderr:\n' 'hello stderr', str(ex)) # Testing with only stdout enabled uut = (linter(sys.executable, use_stdout=True)(TestLinter)(Section('TEST_SECTION'), Queue())) try: with execute_bear(uut, 'filename', ['file']) as result: raise AssertionError except AssertionError as ex: self.assertIn( 'Program arguments:\n' "('-c', \"import sys\\n" "print('hello stdout')\\n" "print('hello stderr', file=sys.stderr)\")\n" 'The program yielded the following output:\n\n' 'hello stdout', str(ex))
def test_html_in_markdown(self): content = html_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Do not use HTML in markdown') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_module_scope(self): file_text = ['def foo():\n', ' pass\n'] with execute_bear(self.uut, self.filename, file_text) as result: self.assertIsNotNone(result[0].module_scope) self.assertIsInstance(result[0].module_scope, ModuleScope)
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_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_wrong_language(self): self.section.append(Setting('language', 'anything')) logger = logging.getLogger() annotation_bear_result_type = namedtuple('result', 'contents') dep_results = { 'AnnotationBear': [ annotation_bear_result_type( 'coalang specification for anything not found.') ] } text = ['# todo 123'] with self.assertLogs(logger, 'ERROR') as log: with execute_bear(self.uut, filename='F', file=text, dependency_results=dep_results) as result: self.assertEqual(len(result), 1) self.assertEqual(result[0].diffs, {}) self.assertEqual(result[0].affected_code[0].start.line, 1) self.assertEqual(len(log.output), 1) self.assertIn( log.output[0], 'ERROR:root:coalang specification' ' for anything not found.')
def test_function(self): test_file_content = load_testfile(test_file).splitlines(True) arguments = {'language': 'python', 'docstyle': 'default'} if optional_setting: arguments.update(optional_setting) section = Section('test-section') for key, value in arguments.items(): section[key] = value with execute_bear( DocumentationStyleBear(section, Queue()), test_file, test_file_content, **arguments) as results: diff = Diff(test_file_content) for result in results: # Only the given test file should contain a patch. self.assertEqual(len(result.diffs), 1) diff += result.diffs[test_file] correct_file_content = load_testfile(expected_file).splitlines(True) self.assertEqual(correct_file_content, diff.modified)
def test_pyflakes_messages(self): file_text = ['import sys\n'] with execute_bear(self.uut, self.filename, file_text) as result: self.assertIsNotNone(result[0].pyflakes_messages) self.assertEqual(len(result[0].pyflakes_messages), 1) self.assertIsInstance(result[0].pyflakes_messages[0], UnusedImport)
def test_without_simplify(self): content = in_file1.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: fdict = {fname: file} results[0].apply(fdict) self.assertEqual(''.join(fdict[fname]), out_file1)
def test_first_heading_level(self): content = first_heading_level_file.splitlines() self.section.append(Setting('first_heading_level', 2)) with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'First heading level should be `2`') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_empty_keywords_list(self): self.section.append(Setting('keywords', '')) text = ['bears = KeywordBear\n'] with execute_bear(self.uut, filename='F', file=text, dependency_results=self.dep_results) as result: self.assertEqual(len(result), 0)
def test_duplicate_headings(self): content = duplicate_heading_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Do not use headings with similar content ' 'per section (3:1)') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_invalid_message(self): content = test_file3.splitlines() self.section.append(Setting('max_line_length', '10')) with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Line must be at most 10 characters') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_stdout_stderr_on_linter_test_fail(self): class TestLinter: @staticmethod def process_output(output, filename, file): pass @staticmethod def create_arguments(filename, file, config_file): code = '\n'.join(['import sys', "print('hello stdout')", "print('hello stderr', file=sys.stderr)"]) return '-c', code # Testing with both stdout and stderr enabled uut = (linter(sys.executable, use_stdout=True, use_stderr=True) (TestLinter) (Section('TEST_SECTION'), Queue())) try: with execute_bear(uut, 'filename', ['file']) as result: raise AssertionError except AssertionError as ex: self.assertIn('Program arguments:\n' "('-c', \"import sys\\n" "print('hello stdout')\\n" "print('hello stderr', file=sys.stderr)\")\n" 'The program yielded the following output:\n\n' 'Stdout:\n' 'hello stdout\n\n' 'Stderr:\n' 'hello stderr', str(ex)) # Testing with only stdout enabled uut = (linter(sys.executable, use_stdout=True) (TestLinter) (Section('TEST_SECTION'), Queue())) try: with execute_bear(uut, 'filename', ['file']) as result: raise AssertionError except AssertionError as ex: self.assertIn('Program arguments:\n' "('-c', \"import sys\\n" "print('hello stdout')\\n" "print('hello stderr', file=sys.stderr)\")\n" 'The program yielded the following output:\n\n' 'hello stdout', str(ex))
def test_invalid_link(self): content = test_file4.splitlines() self.section.append(Setting('check_links', True)) with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Link to unknown heading: `world`') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
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_checkbox_content_indentation(self): content = checkbox_content_indentation_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Checkboxes should be followed by a single ' 'character') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_blockquote_indentation(self): content = blockquote_indentation_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'Remove 1 space between blockquote and ' 'content') self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
def test_function_scopes(self): file_text = ['def Foo():\n', ' pass\n', 'def Bar():\n', ' pass\n'] with execute_bear(self.uut, self.filename, file_text) as result: self.assertIsNotNone(result[0].function_scopes) self.assertEqual(len(result[0].function_scopes), 2) for scope in result[0].function_scopes: self.assertIsInstance(scope, FunctionScope)
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_lang_exception(self): self.section.append(Setting('language', 'BlaBlaBla')) ERROR_MESSAGE = 'ERROR:root:Language BlaBlaBla is not yet supported.' logger = logging.getLogger() with self.assertLogs(logger, 'ERROR') as log: with execute_bear(self.uut, filename='F', file='') as result: self.assertEqual(len(log.output), 1) self.assertEqual(log.output[0], ERROR_MESSAGE)
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_generator_scopes(self): file_text = ['(1 for a, b in [(1, 2)])\n', '(1 for a, b in [(1, 2, 3)])\n'] with execute_bear(self.uut, self.filename, file_text) as result: self.assertIsNotNone(result[0].generator_scopes) self.assertEqual(len(result[0].generator_scopes), 2) for scope in result[0].generator_scopes: self.assertIsInstance(scope, GeneratorScope)
def test_warning(self): content = test_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[6].message, 'comment not indented' ' like content (comments-indentation)') self.assertEqual(results[6].affected_code[0].start.line, 11) self.assertEqual(results[6].affected_code[0].end.line, 11) self.assertEqual(results[6].severity, RESULT_SEVERITY.NORMAL)
def test_error(self): content = test_file.splitlines() with prepare_file(content, None) as (file, fname): with execute_bear(self.uut, fname, file) as results: self.assertEqual(results[0].message, 'too many blank lines' ' (1 > 0) (empty-lines)') self.assertEqual(results[0].affected_code[0].start.line, 1) self.assertEqual(results[0].affected_code[0].end.line, 1) self.assertEqual(results[0].severity, RESULT_SEVERITY.MAJOR)