| Conditions | 4 |
| Total Lines | 74 |
| Lines | 0 |
| Ratio | 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 | from collections import OrderedDict |
||
| 51 | def test_parse(self): |
||
| 52 | default_should = OrderedDict([ |
||
| 53 | ('a_default', 'val'), |
||
| 54 | ('another', 'val'), |
||
| 55 | ('comment0', '# do you know that thats a comment'), |
||
| 56 | ('test', 'content'), |
||
| 57 | ('t', '')]) |
||
| 58 | |||
| 59 | makefiles_should = OrderedDict([ |
||
| 60 | ('j', 'a\nmultiline\nvalue'), |
||
| 61 | ('another', 'a\nmultiline\nvalue'), |
||
| 62 | ('comment1', '# just a omment'), |
||
| 63 | ('comment2', '# just a omment'), |
||
| 64 | ('lastone', 'val'), |
||
| 65 | ('comment3', ''), |
||
| 66 | ('a_default', 'val'), |
||
| 67 | ('comment0', '# do you know that thats a comment'), |
||
| 68 | ('test', 'content'), |
||
| 69 | ('t', '')]) |
||
| 70 | |||
| 71 | empty_elem_strip_should = OrderedDict([ |
||
| 72 | ('a', 'a, b, c'), |
||
| 73 | ('b', 'a, ,, d'), |
||
| 74 | ('c', ',,,'), |
||
| 75 | ('comment4', ''), |
||
| 76 | ('a_default', 'val'), |
||
| 77 | ('another', 'val'), |
||
| 78 | ('comment0', '# do you know that thats a comment'), |
||
| 79 | ('test', 'content'), |
||
| 80 | ('t', '')]) |
||
| 81 | |||
| 82 | self.assertRaises(FileNotFoundError, |
||
| 83 | self.uut.parse, |
||
| 84 | self.nonexistentfile) |
||
| 85 | sections = self.uut.parse(self.file) |
||
| 86 | self.assertNotEqual(self.uut.parse(self.file, True), sections) |
||
| 87 | |||
| 88 | # Check section "DEFAULT". |
||
| 89 | key, val = sections.popitem(last=False) |
||
| 90 | self.assertTrue(isinstance(val, Section)) |
||
| 91 | self.assertEqual(key, 'default') |
||
| 92 | |||
| 93 | is_dict = OrderedDict() |
||
| 94 | for k in val: |
||
| 95 | is_dict[k] = str(val[k]) |
||
| 96 | self.assertEqual(is_dict, default_should) |
||
| 97 | |||
| 98 | # Check section "MakeFiles". |
||
| 99 | key, val = sections.popitem(last=False) |
||
| 100 | self.assertTrue(isinstance(val, Section)) |
||
| 101 | self.assertEqual(key, 'makefiles') |
||
| 102 | |||
| 103 | is_dict = OrderedDict() |
||
| 104 | for k in val: |
||
| 105 | is_dict[k] = str(val[k]) |
||
| 106 | self.assertEqual(is_dict, makefiles_should) |
||
| 107 | |||
| 108 | self.assertEqual(val["comment1"].key, "comment1") |
||
| 109 | |||
| 110 | # Check section "EMPTY_ELEM_STRIP". |
||
| 111 | key, val = sections.popitem(last=False) |
||
| 112 | self.assertTrue(isinstance(val, Section)) |
||
| 113 | self.assertEqual(key, 'empty_elem_strip') |
||
| 114 | |||
| 115 | is_dict = OrderedDict() |
||
| 116 | for k in val: |
||
| 117 | is_dict[k] = str(val[k]) |
||
| 118 | self.assertEqual(is_dict, empty_elem_strip_should) |
||
| 119 | |||
| 120 | |||
| 121 | # Check inexistent Section. |
||
| 122 | self.assertRaises(IndexError, |
||
| 123 | self.uut.get_section, |
||
| 124 | "inexistent section") |
||
| 125 | |||
| 152 |