| Total Complexity | 66 | 
| Total Lines | 499 | 
| Duplicated Lines | 0 % | 
Complex classes like coalib.tests.output.ConsoleInteractionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import os  | 
            ||
| 103 | class ConsoleInteractionTest(unittest.TestCase):  | 
            ||
| 104 | |||
| 105 | def setUp(self):  | 
            ||
| 106 | self.log_printer = LogPrinter(ConsolePrinter(print_colored=False))  | 
            ||
| 107 | self.console_printer = ConsolePrinter(print_colored=False)  | 
            ||
| 108 |         self.file_diff_dict = {} | 
            ||
| 109 |         self.local_bears = OrderedDict([("default", [SomelocalBear]), | 
            ||
| 110 |                                         ("test", [SomelocalBear])]) | 
            ||
| 111 |         self.global_bears = OrderedDict([("default", [SomeglobalBear]), | 
            ||
| 112 |                                          ("test", [SomeglobalBear])]) | 
            ||
| 113 | |||
| 114 | self.old_open_editor_applicable = OpenEditorAction.is_applicable  | 
            ||
| 115 | OpenEditorAction.is_applicable = staticmethod(lambda *args: False)  | 
            ||
| 116 | |||
| 117 | self.old_apply_patch_applicable = ApplyPatchAction.is_applicable  | 
            ||
| 118 | ApplyPatchAction.is_applicable = staticmethod(lambda *args: False)  | 
            ||
| 119 | |||
| 120 | def tearDown(self):  | 
            ||
| 121 | OpenEditorAction.is_applicable = self.old_open_editor_applicable  | 
            ||
| 122 | ApplyPatchAction.is_applicable = self.old_apply_patch_applicable  | 
            ||
| 123 | |||
| 124 | def test_require_settings(self):  | 
            ||
| 125 | self.assertRaises(TypeError, acquire_settings, self.log_printer, 0)  | 
            ||
| 126 |         self.assertEqual(acquire_settings(self.log_printer, {0: 0}), {}) | 
            ||
| 127 | |||
| 128 | with simulate_console_inputs(0, 1, 2) as generator:  | 
            ||
| 129 | self.assertEqual(acquire_settings(self.log_printer,  | 
            ||
| 130 |                                               {"setting": ["help text", | 
            ||
| 131 | "SomeBear"]}),  | 
            ||
| 132 |                              {"setting": 0}) | 
            ||
| 133 | |||
| 134 | self.assertEqual(acquire_settings(self.log_printer,  | 
            ||
| 135 |                                               {"setting": ["help text", | 
            ||
| 136 | "SomeBear",  | 
            ||
| 137 | "AnotherBear"]}),  | 
            ||
| 138 |                              {"setting": 1}) | 
            ||
| 139 | |||
| 140 | self.assertEqual(acquire_settings(self.log_printer,  | 
            ||
| 141 |                                               {"setting": ["help text", | 
            ||
| 142 | "SomeBear",  | 
            ||
| 143 | "AnotherBear",  | 
            ||
| 144 | "YetAnotherBear"]}),  | 
            ||
| 145 |                              {"setting": 2}) | 
            ||
| 146 | |||
| 147 | self.assertEqual(generator.last_input, 2)  | 
            ||
| 148 | |||
| 149 | def test_print_result(self):  | 
            ||
| 150 | print_result(self.console_printer,  | 
            ||
| 151 | self.log_printer,  | 
            ||
| 152 | None,  | 
            ||
| 153 | self.file_diff_dict,  | 
            ||
| 154 | "illegal value",  | 
            ||
| 155 |                      {}) | 
            ||
| 156 | |||
| 157 | with simulate_console_inputs(0):  | 
            ||
| 158 | print_result(self.console_printer,  | 
            ||
| 159 | self.log_printer,  | 
            ||
| 160 | None,  | 
            ||
| 161 | self.file_diff_dict,  | 
            ||
| 162 |                          Result("origin", "msg", diffs={}), | 
            ||
| 163 |                          {}) | 
            ||
| 164 | |||
| 165 | with make_temp() as testfile_path:  | 
            ||
| 166 |             file_dict = { | 
            ||
| 167 | testfile_path: ["1\n", "2\n", "3\n"],  | 
            ||
| 168 | "f_b": ["1", "2", "3"]  | 
            ||
| 169 | }  | 
            ||
| 170 | diff = Diff(file_dict[testfile_path])  | 
            ||
| 171 | diff.delete_line(2)  | 
            ||
| 172 | diff.change_line(3, "3\n", "3_changed\n")  | 
            ||
| 173 | |||
| 174 | with simulate_console_inputs(1), self.assertRaises(ValueError):  | 
            ||
| 175 | ApplyPatchAction.is_applicable = staticmethod(  | 
            ||
| 176 | lambda *args: True)  | 
            ||
| 177 | print_result(self.console_printer,  | 
            ||
| 178 | self.log_printer,  | 
            ||
| 179 | None,  | 
            ||
| 180 | self.file_diff_dict,  | 
            ||
| 181 |                              Result("origin", "msg", diffs={ | 
            ||
| 182 | testfile_path: diff}),  | 
            ||
| 183 | file_dict)  | 
            ||
| 184 | |||
| 185 | # Interaction must be closed by the user with `0` if it's not a  | 
            ||
| 186 | # param  | 
            ||
| 187 |             with simulate_console_inputs("INVALID", | 
            ||
| 188 | -1,  | 
            ||
| 189 | 1,  | 
            ||
| 190 | 0,  | 
            ||
| 191 | 3) as input_generator:  | 
            ||
| 192 |                 curr_section = Section("") | 
            ||
| 193 | print_section_beginning(self.console_printer, curr_section)  | 
            ||
| 194 | print_result(self.console_printer,  | 
            ||
| 195 | self.log_printer,  | 
            ||
| 196 | curr_section,  | 
            ||
| 197 | self.file_diff_dict,  | 
            ||
| 198 |                              Result("origin", "msg", diffs={ | 
            ||
| 199 | testfile_path: diff}),  | 
            ||
| 200 | file_dict)  | 
            ||
| 201 | self.assertEqual(input_generator.last_input, 3)  | 
            ||
| 202 | |||
| 203 | self.file_diff_dict.clear()  | 
            ||
| 204 | |||
| 205 | with open(testfile_path) as f:  | 
            ||
| 206 | self.assertEqual(f.readlines(), ["1\n", "3_changed\n"])  | 
            ||
| 207 | |||
| 208 | os.remove(testfile_path + ".orig")  | 
            ||
| 209 | |||
| 210 | name, section = get_action_info(curr_section,  | 
            ||
| 211 | TestAction().get_metadata())  | 
            ||
| 212 | self.assertEqual(input_generator.last_input, 4)  | 
            ||
| 213 |                 self.assertEqual(str(section), " {param : '3'}") | 
            ||
| 214 | self.assertEqual(name, "TestAction")  | 
            ||
| 215 | |||
| 216 | # Check if the user is asked for the parameter only the first time.  | 
            ||
| 217 | # Use OpenEditorAction that needs this parameter (editor command).  | 
            ||
| 218 | with simulate_console_inputs(1, "test_editor", 0, 1, 0) as generator:  | 
            ||
| 219 | OpenEditorAction.is_applicable = staticmethod(lambda *args: True)  | 
            ||
| 220 | |||
| 221 |             patch_result = Result("origin", "msg", diffs={testfile_path: diff}) | 
            ||
| 222 | patch_result.file = "f_b"  | 
            ||
| 223 | |||
| 224 | print_result(self.console_printer,  | 
            ||
| 225 | self.log_printer,  | 
            ||
| 226 | curr_section,  | 
            ||
| 227 | self.file_diff_dict,  | 
            ||
| 228 | patch_result,  | 
            ||
| 229 | file_dict)  | 
            ||
| 230 | # choose action, choose editor, choose no action (-1 -> 2)  | 
            ||
| 231 | self.assertEqual(generator.last_input, 2)  | 
            ||
| 232 | |||
| 233 | # It shoudn't ask for parameter again  | 
            ||
| 234 | print_result(self.console_printer,  | 
            ||
| 235 | self.log_printer,  | 
            ||
| 236 | curr_section,  | 
            ||
| 237 | self.file_diff_dict,  | 
            ||
| 238 | patch_result,  | 
            ||
| 239 | file_dict)  | 
            ||
| 240 | self.assertEqual(generator.last_input, 4)  | 
            ||
| 241 | |||
| 242 | def test_print_affected_files(self):  | 
            ||
| 243 | with retrieve_stdout() as stdout, \  | 
            ||
| 244 | make_temp() as some_file:  | 
            ||
| 245 |             file_dict = {some_file: ["1\n", "2\n", "3\n"]} | 
            ||
| 246 | affected_code = (SourceRange.from_values(some_file),)  | 
            ||
| 247 | print_affected_files(self.console_printer,  | 
            ||
| 248 | self.log_printer,  | 
            ||
| 249 |                                  Section(""), | 
            ||
| 250 |                                  Result("origin", | 
            ||
| 251 | "message",  | 
            ||
| 252 | affected_code=affected_code),  | 
            ||
| 253 | file_dict,  | 
            ||
| 254 | color=True)  | 
            ||
| 255 | self.assertEqual(stdout.getvalue(),  | 
            ||
| 256 | "\n"+relpath(some_file)+"\n")  | 
            ||
| 257 | |||
| 258 | def test_acquire_actions_and_apply(self):  | 
            ||
| 259 | with make_temp() as testfile_path:  | 
            ||
| 260 |             file_dict = {testfile_path: ["1\n", "2\n", "3\n"]} | 
            ||
| 261 | diff = Diff(file_dict[testfile_path])  | 
            ||
| 262 | diff.delete_line(2)  | 
            ||
| 263 | diff.change_line(3, "3\n", "3_changed\n")  | 
            ||
| 264 | with simulate_console_inputs(1, 0) as generator, \  | 
            ||
| 265 | retrieve_stdout() as sio:  | 
            ||
| 266 | ApplyPatchAction.is_applicable = staticmethod(  | 
            ||
| 267 | lambda *args: True)  | 
            ||
| 268 | acquire_actions_and_apply(self.console_printer,  | 
            ||
| 269 | self.log_printer,  | 
            ||
| 270 |                                           Section(""), | 
            ||
| 271 | self.file_diff_dict,  | 
            ||
| 272 |                                           Result("origin", "message", diffs={ | 
            ||
| 273 | testfile_path: diff}),  | 
            ||
| 274 | file_dict)  | 
            ||
| 275 | self.assertEqual(generator.last_input, 1)  | 
            ||
| 276 | self.assertIn(ApplyPatchAction.success_message, sio.getvalue())  | 
            ||
| 277 | |||
| 278 | def test_print_result_no_input(self):  | 
            ||
| 279 | with make_temp() as testfile_path:  | 
            ||
| 280 |             file_dict = {testfile_path: ["1\n", "2\n", "3\n"]} | 
            ||
| 281 | diff = Diff(file_dict[testfile_path])  | 
            ||
| 282 | diff.delete_line(2)  | 
            ||
| 283 | diff.change_line(3, "3\n", "3_changed\n")  | 
            ||
| 284 | with simulate_console_inputs(1, 2, 3) as generator, \  | 
            ||
| 285 | retrieve_stdout() as stdout:  | 
            ||
| 286 | ApplyPatchAction.is_applicable = staticmethod(  | 
            ||
| 287 | lambda *args: True)  | 
            ||
| 288 | print_results_no_input(self.log_printer,  | 
            ||
| 289 |                                        Section("someSection"), | 
            ||
| 290 |                                        [Result("origin", "message", diffs={ | 
            ||
| 291 | testfile_path: diff})],  | 
            ||
| 292 | file_dict,  | 
            ||
| 293 | self.file_diff_dict,  | 
            ||
| 294 | color=False)  | 
            ||
| 295 | self.assertEqual(generator.last_input, -1)  | 
            ||
| 296 | self.assertEqual(stdout.getvalue(),  | 
            ||
| 297 | """  | 
            ||
| 298 | Project wide:  | 
            ||
| 299 | | | [NORMAL] origin:  | 
            ||
| 300 | | | message  | 
            ||
| 301 | """)  | 
            ||
| 302 | |||
| 303 | def test_print_section_beginning(self):  | 
            ||
| 304 | with retrieve_stdout() as stdout:  | 
            ||
| 305 |             print_section_beginning(self.console_printer, Section("name")) | 
            ||
| 306 | self.assertEqual(stdout.getvalue(), "Executing section name...\n")  | 
            ||
| 307 | |||
| 308 | def test_nothing_done(self):  | 
            ||
| 309 | with retrieve_stdout() as stdout:  | 
            ||
| 310 | nothing_done(self.log_printer)  | 
            ||
| 311 |             self.assertIn("No existent section was targeted or enabled. " | 
            ||
| 312 | "Nothing to do.\n",  | 
            ||
| 313 | stdout.getvalue())  | 
            ||
| 314 | |||
| 315 | def test_print_results_empty(self):  | 
            ||
| 316 | with retrieve_stdout() as stdout:  | 
            ||
| 317 |             print_results(self.log_printer, Section(""), [], {}, {}) | 
            ||
| 318 | self.assertEqual(stdout.getvalue(), "")  | 
            ||
| 319 | |||
| 320 | def test_print_results_project_wide(self):  | 
            ||
| 321 | with retrieve_stdout() as stdout:  | 
            ||
| 322 | print_results(self.log_printer,  | 
            ||
| 323 |                           Section(""), | 
            ||
| 324 |                           [Result("origin", "message")], | 
            ||
| 325 |                           {}, | 
            ||
| 326 |                           {}, | 
            ||
| 327 | color=False)  | 
            ||
| 328 | self.assertEqual(  | 
            ||
| 329 |                 "\n{}\n|    | [NORMAL] origin:\n|    | message" | 
            ||
| 330 | "\n".format(STR_PROJECT_WIDE),  | 
            ||
| 331 | stdout.getvalue())  | 
            ||
| 332 | |||
| 333 | def test_print_results_for_file(self):  | 
            ||
| 334 | with retrieve_stdout() as stdout:  | 
            ||
| 335 | print_results(  | 
            ||
| 336 | self.log_printer,  | 
            ||
| 337 |                 Section(""), | 
            ||
| 338 |                 [Result.from_values("SpaceConsistencyBear", | 
            ||
| 339 | "Trailing whitespace found",  | 
            ||
| 340 | file="filename",  | 
            ||
| 341 | line=2)],  | 
            ||
| 342 |                 {abspath("filename"): ["test line\n", "line 2\n", "line 3\n"]}, | 
            ||
| 343 |                 {}, | 
            ||
| 344 | color=False)  | 
            ||
| 345 |             self.assertEqual("""\nfilename | 
            ||
| 346 | | 2| line 2  | 
            ||
| 347 | | | [NORMAL] SpaceConsistencyBear:  | 
            ||
| 348 | | | Trailing whitespace found  | 
            ||
| 349 | """,  | 
            ||
| 350 | stdout.getvalue())  | 
            ||
| 351 | |||
| 352 | with retrieve_stdout() as stdout:  | 
            ||
| 353 | print_results(  | 
            ||
| 354 | self.log_printer,  | 
            ||
| 355 |                 Section(""), | 
            ||
| 356 |                 [Result.from_values("SpaceConsistencyBear", | 
            ||
| 357 | "Trailing whitespace found",  | 
            ||
| 358 | file="filename",  | 
            ||
| 359 | line=5)],  | 
            ||
| 360 |                 {abspath("filename"): ["test line\n", | 
            ||
| 361 | "line 2\n",  | 
            ||
| 362 | "line 3\n",  | 
            ||
| 363 | "line 4\n",  | 
            ||
| 364 | "line 5\n"]},  | 
            ||
| 365 |                 {}, | 
            ||
| 366 | color=False)  | 
            ||
| 367 |             self.assertEqual("""\nfilename | 
            ||
| 368 | | 5| line 5  | 
            ||
| 369 | | | [NORMAL] SpaceConsistencyBear:  | 
            ||
| 370 | | | Trailing whitespace found  | 
            ||
| 371 | """,  | 
            ||
| 372 | stdout.getvalue())  | 
            ||
| 373 | |||
| 374 | def test_print_results_sorting(self):  | 
            ||
| 375 | with retrieve_stdout() as stdout:  | 
            ||
| 376 | print_results(self.log_printer,  | 
            ||
| 377 |                           Section(""), | 
            ||
| 378 |                           [Result.from_values("SpaceConsistencyBear", | 
            ||
| 379 | "Trailing whitespace found",  | 
            ||
| 380 | file="file",  | 
            ||
| 381 | line=5),  | 
            ||
| 382 |                            Result.from_values("SpaceConsistencyBear", | 
            ||
| 383 | "Trailing whitespace found",  | 
            ||
| 384 | file="file",  | 
            ||
| 385 | line=2)],  | 
            ||
| 386 |                           {abspath("file"): ["test line\n", | 
            ||
| 387 | "line 2\n",  | 
            ||
| 388 | "line 3\n",  | 
            ||
| 389 | "line 4\n",  | 
            ||
| 390 | "line 5\n"]},  | 
            ||
| 391 |                           {}, | 
            ||
| 392 | color=False)  | 
            ||
| 393 | |||
| 394 |             self.assertEqual(""" | 
            ||
| 395 | file  | 
            ||
| 396 | | 2| line 2  | 
            ||
| 397 | | | [NORMAL] SpaceConsistencyBear:  | 
            ||
| 398 | | | Trailing whitespace found  | 
            ||
| 399 | |||
| 400 | file  | 
            ||
| 401 | | 5| line 5  | 
            ||
| 402 | | | [NORMAL] SpaceConsistencyBear:  | 
            ||
| 403 | | | Trailing whitespace found  | 
            ||
| 404 | """,  | 
            ||
| 405 | stdout.getvalue())  | 
            ||
| 406 | |||
| 407 | def test_print_results_multiple_ranges(self):  | 
            ||
| 408 | affected_code = (  | 
            ||
| 409 |             SourceRange.from_values("some_file", 5, end_line=7), | 
            ||
| 410 |             SourceRange.from_values("another_file", 1, 3, 1, 5), | 
            ||
| 411 |             SourceRange.from_values("another_file", 3, 3, 3, 5)) | 
            ||
| 412 | with retrieve_stdout() as stdout:  | 
            ||
| 413 | print_results(  | 
            ||
| 414 | self.log_printer,  | 
            ||
| 415 |                 Section(""), | 
            ||
| 416 |                 [Result("ClangCloneDetectionBear", | 
            ||
| 417 | "Clone Found",  | 
            ||
| 418 | affected_code)],  | 
            ||
| 419 |                 {abspath("some_file"): ["line " + str(i + 1) + "\n" | 
            ||
| 420 | for i in range(10)],  | 
            ||
| 421 |                  abspath("another_file"): ["line " + str(i + 1) + "\n" | 
            ||
| 422 | for i in range(10)]},  | 
            ||
| 423 |                 {}, | 
            ||
| 424 | color=False)  | 
            ||
| 425 |             self.assertEqual(""" | 
            ||
| 426 | another_file  | 
            ||
| 427 | | 1| line 1  | 
            ||
| 428 | |||
| 429 | another_file  | 
            ||
| 430 | | 3| line 3  | 
            ||
| 431 | |||
| 432 | some_file  | 
            ||
| 433 | | 5| line 5  | 
            ||
| 434 | | 6| line 6  | 
            ||
| 435 | | 7| line 7  | 
            ||
| 436 | | | [NORMAL] ClangCloneDetectionBear:  | 
            ||
| 437 | | | Clone Found  | 
            ||
| 438 | """,  | 
            ||
| 439 | stdout.getvalue())  | 
            ||
| 440 | |||
| 441 | def test_print_results_missing_file(self):  | 
            ||
| 442 | self.log_printer = LogPrinter(NullPrinter())  | 
            ||
| 443 | with retrieve_stdout() as stdout:  | 
            ||
| 444 | print_results(  | 
            ||
| 445 | self.log_printer,  | 
            ||
| 446 |                 Section(""), | 
            ||
| 447 |                 [Result("t", "msg"), | 
            ||
| 448 |                  Result.from_values("t", "msg", file="file", line=5)], | 
            ||
| 449 |                 {}, | 
            ||
| 450 |                 {}, | 
            ||
| 451 | color=False)  | 
            ||
| 452 |             self.assertEqual("\n" + STR_PROJECT_WIDE + "\n" | 
            ||
| 453 | "| | [NORMAL] t:\n"  | 
            ||
| 454 | "| | msg\n"  | 
            ||
| 455 | # Second results file isn't there, no context is  | 
            ||
| 456 | # printed, only a warning log message which we  | 
            ||
| 457 | # don't catch  | 
            ||
| 458 | "| | [NORMAL] t:\n"  | 
            ||
| 459 | "| | msg\n", stdout.getvalue())  | 
            ||
| 460 | |||
| 461 | def test_print_results_missing_line(self):  | 
            ||
| 462 | with retrieve_stdout() as stdout:  | 
            ||
| 463 | print_results(  | 
            ||
| 464 | self.log_printer,  | 
            ||
| 465 |                 Section(""), | 
            ||
| 466 |                 [Result.from_values("t", "msg", file="file", line=5), | 
            ||
| 467 |                  Result.from_values("t", "msg", file="file", line=6)], | 
            ||
| 468 |                 {abspath("file"): ["line " + str(i + 1) for i in range(5)]}, | 
            ||
| 469 |                 {}, | 
            ||
| 470 | color=False)  | 
            ||
| 471 |             self.assertEqual("\n" | 
            ||
| 472 | "file\n"  | 
            ||
| 473 | "| 5| line 5\n"  | 
            ||
| 474 | "| | [NORMAL] t:\n"  | 
            ||
| 475 | "| | msg\n"  | 
            ||
| 476 | "\n"  | 
            ||
| 477 | "file\n"  | 
            ||
| 478 |                              "|    | {}\n" | 
            ||
| 479 | "| | [NORMAL] t:\n"  | 
            ||
| 480 | "| | msg\n".format(STR_LINE_DOESNT_EXIST),  | 
            ||
| 481 | stdout.getvalue())  | 
            ||
| 482 | |||
| 483 | def test_print_results_without_line(self):  | 
            ||
| 484 | with retrieve_stdout() as stdout:  | 
            ||
| 485 | print_results(  | 
            ||
| 486 | self.log_printer,  | 
            ||
| 487 |                 Section(""), | 
            ||
| 488 |                 [Result.from_values("t", "msg", file="file")], | 
            ||
| 489 |                 {abspath("file"): []}, | 
            ||
| 490 |                 {}, | 
            ||
| 491 | color=False)  | 
            ||
| 492 | self.assertEqual(  | 
            ||
| 493 | "\nfile\n"  | 
            ||
| 494 | "| | [NORMAL] t:\n"  | 
            ||
| 495 | "| | msg\n",  | 
            ||
| 496 | stdout.getvalue())  | 
            ||
| 497 | |||
| 498 | def test_print_bears_empty(self):  | 
            ||
| 499 | with retrieve_stdout() as stdout:  | 
            ||
| 500 |             bears = {} | 
            ||
| 501 | print_bears(self.log_printer.printer, bears, True)  | 
            ||
| 502 |             self.assertEqual("No bears to show.\n", stdout.getvalue()) | 
            ||
| 503 | with retrieve_stdout() as stdout:  | 
            ||
| 504 |             bears = {} | 
            ||
| 505 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 506 |             self.assertEqual("No bears to show.\n", stdout.getvalue()) | 
            ||
| 507 | |||
| 508 | def test_print_bears(self):  | 
            ||
| 509 | with retrieve_stdout() as stdout:  | 
            ||
| 510 |             bears = {TestBear: ["default", "docs"]} | 
            ||
| 511 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 512 | expected_string = "TestBear:\n"  | 
            ||
| 513 | expected_string += " Test bear Description.\n\n"  | 
            ||
| 514 | expected_string += " Used in:\n"  | 
            ||
| 515 | expected_string += " * default\n"  | 
            ||
| 516 | expected_string += " * docs\n\n"  | 
            ||
| 517 | expected_string += " Needed Settings:\n"  | 
            ||
| 518 | expected_string += " * setting1: Required Setting.\n\n"  | 
            ||
| 519 | expected_string += " Optional Settings:\n"  | 
            ||
| 520 |             expected_string += "   * setting2: Optional Setting. (" | 
            ||
| 521 | expected_string += "Optional, defaults to 'None'."  | 
            ||
| 522 | expected_string += ")\n\n"  | 
            ||
| 523 | |||
| 524 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 525 | |||
| 526 | def test_print_bears_no_settings(self):  | 
            ||
| 527 | with retrieve_stdout() as stdout:  | 
            ||
| 528 |             bears = {SomeBear: ["default"]} | 
            ||
| 529 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 530 | expected_string = "SomeBear:\n"  | 
            ||
| 531 | expected_string += " Some Description.\n\n"  | 
            ||
| 532 | expected_string += " Used in:\n"  | 
            ||
| 533 | expected_string += " * default\n\n"  | 
            ||
| 534 | expected_string += " No needed settings.\n\n"  | 
            ||
| 535 | expected_string += " No optional settings.\n\n"  | 
            ||
| 536 | |||
| 537 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 538 | |||
| 539 | def test_print_bears_no_needed_settings(self):  | 
            ||
| 540 | with retrieve_stdout() as stdout:  | 
            ||
| 541 |             bears = {SomeOtherBear: ["test"]} | 
            ||
| 542 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 543 | expected_string = "SomeOtherBear:\n"  | 
            ||
| 544 | expected_string += " This is a Bear.\n\n"  | 
            ||
| 545 | expected_string += " Used in:\n"  | 
            ||
| 546 | expected_string += " * test\n\n"  | 
            ||
| 547 | expected_string += " No needed settings.\n\n"  | 
            ||
| 548 | expected_string += " Optional Settings:\n"  | 
            ||
| 549 |             expected_string += "   * setting: This is an optional setting. (" | 
            ||
| 550 | expected_string += "Optional, defaults to 'None'."  | 
            ||
| 551 | expected_string += ")\n\n"  | 
            ||
| 552 | |||
| 553 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 554 | |||
| 555 | def test_print_bears_no_optional_settings(self):  | 
            ||
| 556 | with retrieve_stdout() as stdout:  | 
            ||
| 557 |             bears = {TestBear2: ["test"]} | 
            ||
| 558 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 559 | expected_string = "TestBear2:\n"  | 
            ||
| 560 | expected_string += " Test bear 2 description.\n\n"  | 
            ||
| 561 | expected_string += " Used in:\n"  | 
            ||
| 562 | expected_string += " * test\n\n"  | 
            ||
| 563 | expected_string += " Needed Settings:\n"  | 
            ||
| 564 | expected_string += " * setting1: Required Setting.\n\n"  | 
            ||
| 565 | expected_string += " No optional settings.\n\n"  | 
            ||
| 566 | |||
| 567 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 568 | |||
| 569 | def test_print_bears_no_sections(self):  | 
            ||
| 570 | with retrieve_stdout() as stdout:  | 
            ||
| 571 |             bears = {SomeBear: []} | 
            ||
| 572 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 573 | expected_string = "SomeBear:\n"  | 
            ||
| 574 | expected_string += " Some Description.\n\n"  | 
            ||
| 575 | expected_string += " No sections.\n\n"  | 
            ||
| 576 | expected_string += " No needed settings.\n\n"  | 
            ||
| 577 | expected_string += " No optional settings.\n\n"  | 
            ||
| 578 | |||
| 579 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 580 | |||
| 581 | def test_show_bears(self):  | 
            ||
| 582 | with retrieve_stdout() as stdout:  | 
            ||
| 583 |             bears = {SomelocalBear: ['default', 'test'], | 
            ||
| 584 | SomeglobalBear: ['default', 'test']}  | 
            ||
| 585 | print_bears(self.log_printer.printer, bears, False)  | 
            ||
| 586 | expected_string = stdout.getvalue()  | 
            ||
| 587 | self.maxDiff = None  | 
            ||
| 588 | with retrieve_stdout() as stdout:  | 
            ||
| 589 | show_bears(self.local_bears,  | 
            ||
| 590 | self.global_bears,  | 
            ||
| 591 | False,  | 
            ||
| 592 | self.log_printer.printer)  | 
            ||
| 593 | self.assertEqual(expected_string, stdout.getvalue())  | 
            ||
| 594 | |||
| 595 | with retrieve_stdout() as stdout:  | 
            ||
| 596 | show_bears(self.local_bears,  | 
            ||
| 597 | self.global_bears,  | 
            ||
| 598 | True,  | 
            ||
| 599 | self.log_printer.printer)  | 
            ||
| 600 |             self.assertEqual(" * SomeglobalBear\n" | 
            ||
| 601 | " * SomelocalBear\n", stdout.getvalue())  | 
            ||
| 602 | |||
| 671 |