|
| 1 | +from queue import Queue |
| 2 | +from textwrap import dedent |
| 3 | +import unittest |
| 4 | +import shutil |
| 5 | +import platform |
| 6 | + |
| 7 | +from coalib.results.Diff import Diff |
| 8 | +from coalib.settings.Section import Section |
| 9 | +from coalib.testing.LocalBearTestHelper import execute_bear |
| 10 | +from coalib.testing.BearTestHelper import generate_skip_decorator |
| 11 | + |
| 12 | +from bears.documentation.DocGrammarBear import DocGrammarBear |
| 13 | + |
| 14 | + |
| 15 | +def make_docstring(main_desc: str='', |
| 16 | + param_desc: str='', |
| 17 | + return_desc: str=''): |
| 18 | + """ |
| 19 | + This assembles a simple docstring having a main description, a parameter |
| 20 | + description and a return description. This makes the tests readibilty |
| 21 | + clean. |
| 22 | +
|
| 23 | + :param main_desc: |
| 24 | + Contains the main description of the docstring. |
| 25 | + :param param_desc: |
| 26 | + Contatins the parameter description of the docstring. |
| 27 | + :param return_desc: |
| 28 | + Contains the return description of the docstring. |
| 29 | + :return: |
| 30 | + Returns an assembled docstring. |
| 31 | + """ |
| 32 | + docstring = dedent('"""\n' |
| 33 | + '{}' |
| 34 | + '\n' |
| 35 | + ':param xyz:{}' |
| 36 | + ':return:{}' |
| 37 | + '"""\n') |
| 38 | + return docstring.format(main_desc, |
| 39 | + param_desc, |
| 40 | + return_desc).splitlines(True) |
| 41 | + |
| 42 | + |
| 43 | +def test(test_data, expected_data, optional_setting=None): |
| 44 | + def test_function(self): |
| 45 | + arguments = {'language': 'python', 'docstyle': 'default'} |
| 46 | + if optional_setting: |
| 47 | + arguments.update(optional_setting) |
| 48 | + section = Section('test-section') |
| 49 | + for key, value in arguments.items(): |
| 50 | + section[key] = value |
| 51 | + |
| 52 | + with execute_bear( |
| 53 | + DocGrammarBear(section, Queue()), |
| 54 | + 'dummy_filename', |
| 55 | + test_data, |
| 56 | + **arguments) as results: |
| 57 | + |
| 58 | + diff = Diff(test_data) |
| 59 | + for result in results: |
| 60 | + # Only the given test file should contain a patch. |
| 61 | + self.assertEqual(len(result.diffs), 1) |
| 62 | + |
| 63 | + diff += result.diffs['dummy_filename'] |
| 64 | + |
| 65 | + self.assertEqual(expected_data, diff.modified) |
| 66 | + |
| 67 | + return test_function |
| 68 | + |
| 69 | + |
| 70 | +@generate_skip_decorator(DocGrammarBear) |
| 71 | +class DocGrammarBearTest(unittest.TestCase): |
| 72 | + |
| 73 | + def test_check_prerequisites(self): |
| 74 | + _shutil_which = shutil.which |
| 75 | + try: |
| 76 | + shutil.which = lambda *args, **kwargs: None |
| 77 | + self.assertEqual(DocGrammarBear.check_prerequisites(), |
| 78 | + 'java is not installed.') |
| 79 | + |
| 80 | + shutil.which = lambda *args, **kwargs: 'path/to/java' |
| 81 | + self.assertTrue(DocGrammarBear.check_prerequisites()) |
| 82 | + finally: |
| 83 | + shutil.which = _shutil_which |
| 84 | + |
| 85 | + test_spelling = test( |
| 86 | + make_docstring(main_desc='Thiss is main descrpton.\n'), |
| 87 | + make_docstring(main_desc='This is main description.\n')) |
| 88 | + |
| 89 | + test_capitalize_sentence_start = test( |
| 90 | + make_docstring(main_desc='this sentence starts with small letter\n'), |
| 91 | + make_docstring(main_desc='This sentence starts with small letter\n')) |
| 92 | + |
| 93 | + test_extra_whitespace = test( |
| 94 | + make_docstring(main_desc='This sentence has extra white spaces\n'), |
| 95 | + make_docstring(main_desc='This sentence has extra white spaces\n')) |
| 96 | + |
| 97 | + test_apostrophe_comma = test( |
| 98 | + make_docstring(main_desc='This sentence doesnt have an apostrophe\n'), |
| 99 | + make_docstring(main_desc='This sentence doesn\'t have an ' |
| 100 | + 'apostrophe\n')) |
| 101 | + |
| 102 | + correct_docstring = make_docstring( |
| 103 | + main_desc='This documentation has correct grammar.\n', |
| 104 | + param_desc='Dummy description.\n', |
| 105 | + return_desc='Return Nothing.\n') |
| 106 | + |
| 107 | + test_correct_grammar = test(correct_docstring, correct_docstring) |
| 108 | + |
| 109 | + test_disable_setting_UPPERCASE_SENTENCE_START = test( |
| 110 | + make_docstring(main_desc='sentence starting with lowercase.\n', |
| 111 | + param_desc='dummy description.\n', |
| 112 | + return_desc='Nothing.\n'), |
| 113 | + make_docstring(main_desc='sentence starting with lowercase.\n', |
| 114 | + param_desc='dummy description.\n', |
| 115 | + return_desc='Nothing.\n'), |
| 116 | + {'languagetool_disable_rules': 'UPPERCASE_SENTENCE_START'}) |
| 117 | + |
| 118 | + # FRENCH_WHITESPACE adds a unicode space if it finds empty strings. |
| 119 | + # which was breaking this test case. |
| 120 | + test_language_french = unittest.skipIf( |
| 121 | + platform.system() == 'Windows', |
| 122 | + 'language-check fails for different locale on windows')( |
| 123 | + test( |
| 124 | + make_docstring(main_desc='il monte en haut si il veut.\n'), |
| 125 | + make_docstring(main_desc='Il monte s’il veut.\n'), |
| 126 | + {'locale': 'fr', |
| 127 | + 'languagetool_disable_rules': 'FRENCH_WHITESPACE'})) |
| 128 | + |
| 129 | + # explicit language test cases to check the breakage of DocGrammarBear. |
| 130 | + test_java_explicit = test([ |
| 131 | + 'class Square {\n', |
| 132 | + ' /**\n', |
| 133 | + ' * Returnss Area of a square.\n', |
| 134 | + ' *\n', |
| 135 | + ' *@param side side of squaree\n', |
| 136 | + ' *@return area of a square\n', |
| 137 | + ' */\n', |
| 138 | + ' public int Area(int side) {\n', |
| 139 | + ' return side * side;\n' |
| 140 | + ' }\n', |
| 141 | + '}'], [ |
| 142 | + 'class Square {\n', |
| 143 | + ' /**\n', |
| 144 | + ' * Returns Area of a square.\n', |
| 145 | + ' *\n', |
| 146 | + ' *@param side Side of square\n', |
| 147 | + ' *@return Area of a square\n', |
| 148 | + ' */\n', |
| 149 | + ' public int Area(int side) {\n', |
| 150 | + ' return side * side;\n' |
| 151 | + ' }\n', |
| 152 | + '}'], |
| 153 | + {'language': 'java'}) |
| 154 | + |
| 155 | + test_python_explicit = test([ |
| 156 | + 'def improper_grammar(param1):\n', |
| 157 | + ' """\n', |
| 158 | + ' Documntation contains gramatical mistakess.DocGrammarBear\n', |
| 159 | + ' doesnt check for style.\n', |
| 160 | + ' :param param1: Contains parameter descrption.\n', |
| 161 | + ' :return: returns nothing. first letter small.\n', |
| 162 | + ' """\n', |
| 163 | + ' return None'], [ |
| 164 | + 'def improper_grammar(param1):\n', |
| 165 | + ' """\n', |
| 166 | + ' Documentation contains grammatical mistakes. DocGrammarBear\n', |
| 167 | + ' doesn\'t check for style.\n', |
| 168 | + ' :param param1: Contains parameter description.\n', |
| 169 | + ' :return: Returns nothing. First letter small.\n', |
| 170 | + ' """\n', |
| 171 | + ' return None']) |
0 commit comments