| Conditions | 1 |
| Total Lines | 98 |
| 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 unittest |
||
| 30 | def test_logging(self): |
||
| 31 | uut = LogPrinter(StringPrinter(), timestamp_format="") |
||
| 32 | uut.log_message(self.log_message, end="") |
||
| 33 | self.assertEqual(uut.printer.string, str(self.log_message)) |
||
| 34 | |||
| 35 | uut = LogPrinter(StringPrinter(), log_level=LOG_LEVEL.DEBUG) |
||
| 36 | uut.log_message(self.log_message, end="") |
||
| 37 | self.assertEqual( |
||
| 38 | uut.printer.string, |
||
| 39 | "[ERROR][" + self.timestamp.strftime("%X") + "] " + |
||
| 40 | Constants.COMPLEX_TEST_STRING) |
||
| 41 | |||
| 42 | uut.printer.clear() |
||
| 43 | uut.log(LOG_LEVEL.ERROR, |
||
| 44 | Constants.COMPLEX_TEST_STRING, |
||
| 45 | timestamp=self.timestamp, |
||
| 46 | end="") |
||
| 47 | self.assertEqual( |
||
| 48 | uut.printer.string, |
||
| 49 | "[ERROR][" + self.timestamp.strftime("%X") + "] " + |
||
| 50 | Constants.COMPLEX_TEST_STRING) |
||
| 51 | |||
| 52 | uut.printer.clear() |
||
| 53 | uut.debug(Constants.COMPLEX_TEST_STRING, |
||
| 54 | "d", |
||
| 55 | timestamp=self.timestamp, |
||
| 56 | end="") |
||
| 57 | self.assertEqual( |
||
| 58 | uut.printer.string, |
||
| 59 | "[DEBUG][" + self.timestamp.strftime("%X") + "] " + |
||
| 60 | Constants.COMPLEX_TEST_STRING + " d") |
||
| 61 | |||
| 62 | uut.printer.clear() |
||
| 63 | uut.log_level = LOG_LEVEL.INFO |
||
| 64 | uut.debug(Constants.COMPLEX_TEST_STRING, |
||
| 65 | timestamp=self.timestamp, |
||
| 66 | end="") |
||
| 67 | self.assertEqual(uut.printer.string, "") |
||
| 68 | |||
| 69 | uut.printer.clear() |
||
| 70 | uut.info(Constants.COMPLEX_TEST_STRING, |
||
| 71 | "d", |
||
| 72 | timestamp=self.timestamp, |
||
| 73 | end="") |
||
| 74 | self.assertEqual( |
||
| 75 | uut.printer.string, |
||
| 76 | "[INFO][" + self.timestamp.strftime("%X") + "] " + |
||
| 77 | Constants.COMPLEX_TEST_STRING + " d") |
||
| 78 | |||
| 79 | uut.log_level = LOG_LEVEL.WARNING |
||
| 80 | uut.printer.clear() |
||
| 81 | uut.debug(Constants.COMPLEX_TEST_STRING, |
||
| 82 | timestamp=self.timestamp, |
||
| 83 | end="") |
||
| 84 | self.assertEqual(uut.printer.string, "") |
||
| 85 | |||
| 86 | uut.printer.clear() |
||
| 87 | uut.warn(Constants.COMPLEX_TEST_STRING, |
||
| 88 | "d", |
||
| 89 | timestamp=self.timestamp, |
||
| 90 | end="") |
||
| 91 | self.assertEqual( |
||
| 92 | uut.printer.string, |
||
| 93 | "[WARNING][" + self.timestamp.strftime("%X") + "] " + |
||
| 94 | Constants.COMPLEX_TEST_STRING + " d") |
||
| 95 | |||
| 96 | uut.printer.clear() |
||
| 97 | uut.err(Constants.COMPLEX_TEST_STRING, |
||
| 98 | "d", |
||
| 99 | timestamp=self.timestamp, |
||
| 100 | end="") |
||
| 101 | self.assertEqual( |
||
| 102 | uut.printer.string, |
||
| 103 | "[ERROR][" + self.timestamp.strftime("%X") + "] " + |
||
| 104 | Constants.COMPLEX_TEST_STRING + " d") |
||
| 105 | |||
| 106 | uut.log_level = LOG_LEVEL.DEBUG |
||
| 107 | uut.printer.clear() |
||
| 108 | uut.log_exception( |
||
| 109 | "Something failed.", |
||
| 110 | NotImplementedError(Constants.COMPLEX_TEST_STRING), |
||
| 111 | timestamp=self.timestamp) |
||
| 112 | self.assertTrue(uut.printer.string.startswith( |
||
| 113 | "[ERROR][" + self.timestamp.strftime("%X") + |
||
| 114 | "] Something failed.\n" + |
||
| 115 | "[DEBUG][" + self.timestamp.strftime("%X") + |
||
| 116 | "] Exception was:")) |
||
| 117 | |||
| 118 | uut.log_level = LOG_LEVEL.INFO |
||
| 119 | uut.printer.clear() |
||
| 120 | logged = uut.log_exception( |
||
| 121 | "Something failed.", |
||
| 122 | NotImplementedError(Constants.COMPLEX_TEST_STRING), |
||
| 123 | timestamp=self.timestamp, |
||
| 124 | end="") |
||
| 125 | self.assertTrue(uut.printer.string.startswith( |
||
| 126 | "[ERROR][" + self.timestamp.strftime("%X") + |
||
| 127 | "] Something failed.")) |
||
| 128 | |||
| 134 |