| Conditions | 2 |
| Total Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import os |
||
| 81 | def test_dbus(self): |
||
| 82 | self.document_object_path = self.remote_object.CreateDocument( |
||
| 83 | self.testcode_c_path, |
||
| 84 | dbus_interface="org.coala_analyzer.v1") |
||
| 85 | |||
| 86 | self.assertRegex(str(self.document_object_path), |
||
| 87 | r"^/org/coala_analyzer/v1/test/\d+/documents/\d+$") |
||
| 88 | |||
| 89 | self.document_object = self.bus.get_object( |
||
| 90 | "org.coala_analyzer.v1.test", |
||
| 91 | self.document_object_path) |
||
| 92 | |||
| 93 | config_file = self.document_object.SetConfigFile( |
||
| 94 | "dummy_config", |
||
| 95 | dbus_interface="org.coala_analyzer.v1") |
||
| 96 | self.assertEqual(config_file, "dummy_config") |
||
| 97 | |||
| 98 | config_file = self.document_object.GetConfigFile( |
||
| 99 | dbus_interface="org.coala_analyzer.v1") |
||
| 100 | self.assertEqual(config_file, "dummy_config") |
||
| 101 | |||
| 102 | config_file = self.document_object.FindConfigFile( |
||
| 103 | dbus_interface="org.coala_analyzer.v1") |
||
| 104 | self.assertEqual(config_file, self.config_path) |
||
| 105 | |||
| 106 | analysis = self.document_object.Analyze( |
||
| 107 | dbus_interface="org.coala_analyzer.v1") |
||
| 108 | |||
| 109 | self.maxDiff = None |
||
| 110 | print(analysis) |
||
| 111 | |||
| 112 | # Run some basic analysis with good debug messages. |
||
| 113 | self.assertEqual(analysis[0], 1, "Exit code was not 1.") |
||
| 114 | self.assertEqual(len(analysis[1]), 0, "Unexpected log messages found.") |
||
| 115 | |||
| 116 | sections = analysis[2] |
||
| 117 | self.assertEqual(len(sections), 1, "Expected only 1 section to run.") |
||
| 118 | |||
| 119 | section = sections[0] |
||
| 120 | self.assertEqual(section[0], "default", |
||
| 121 | "Expected section to be named 'default'.") |
||
| 122 | self.assertTrue(section[1], "Section did not execute successfully.") |
||
| 123 | self.assertEqual(len(section[2]), 2, "Expected 2 results in section.") |
||
| 124 | |||
| 125 | # Remove the ids as they are hashes and cannot be asserted. |
||
| 126 | for result in section[2]: |
||
| 127 | result['id'] = 0 |
||
| 128 | |||
| 129 | # We also test as a dictionary as dbus should be able to convert |
||
| 130 | # it into the correct python types. |
||
| 131 | self.assertEqual(analysis, |
||
| 132 | (1, |
||
| 133 | [], |
||
| 134 | [('default', |
||
| 135 | True, |
||
| 136 | [{'debug_msg': '', |
||
| 137 | 'additional_info': '', |
||
| 138 | 'file': '', |
||
| 139 | 'id': 0, |
||
| 140 | 'line_nr': "", |
||
| 141 | 'message': 'test msg', |
||
| 142 | 'origin': 'LocalTestBear', |
||
| 143 | 'severity': 'NORMAL', |
||
| 144 | 'confidence': '100'}, |
||
| 145 | {'debug_msg': '', |
||
| 146 | 'additional_info': '', |
||
| 147 | 'file': self.testcode_c_path, |
||
| 148 | 'id': 0, |
||
| 149 | 'line_nr': "", |
||
| 150 | 'message': 'test msg', |
||
| 151 | 'origin': 'GlobalTestBear', |
||
| 152 | 'severity': 'NORMAL', |
||
| 153 | 'confidence': '100'}])])) |
||
| 154 | |||
| 155 | config_file = self.document_object.SetConfigFile( |
||
| 156 | self.config_path + "2", |
||
| 157 | dbus_interface="org.coala_analyzer.v1") |
||
| 158 | analysis = self.document_object.Analyze( |
||
| 159 | dbus_interface="org.coala_analyzer.v1") |
||
| 160 | self.assertEqual(analysis[0], 255) |
||
| 161 | self.assertEqual(analysis[1][1]["log_level"], "ERROR") |
||
| 162 | self.assertEqual(analysis[1][1]["message"], Constants.CRASH_MESSAGE) |
||
| 163 | |||
| 164 | # Skip file if file pattern doesn't match |
||
| 165 | # Also test if 2 documents can be opened simultaneously |
||
| 166 | self.document_object_path = self.remote_object.CreateDocument( |
||
| 167 | "test.unknown_ext", |
||
| 168 | dbus_interface="org.coala_analyzer.v1") |
||
| 169 | self.document_object = self.bus.get_object( |
||
| 170 | "org.coala_analyzer.v1.test", |
||
| 171 | self.document_object_path) |
||
| 172 | config_file = self.document_object.SetConfigFile( |
||
| 173 | self.config_path, |
||
| 174 | dbus_interface="org.coala_analyzer.v1") |
||
| 175 | analysis = self.document_object.Analyze( |
||
| 176 | dbus_interface="org.coala_analyzer.v1") |
||
| 177 | self.assertEqual(analysis, (0, [], [])) |
||
| 178 | |||
| 179 | self.remote_object.DisposeDocument( |
||
| 180 | self.testcode_c_path, |
||
| 181 | dbus_interface="org.coala_analyzer.v1") |
||
| 182 | |||
| 183 | self.remote_object.DisposeDocument( |
||
| 184 | "test.unknown_ext", |
||
| 185 | dbus_interface="org.coala_analyzer.v1") |
||
| 186 | |||
| 190 |