| Conditions | 13 |
| Total Lines | 60 |
| 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:
Complex classes like test_Keystroke_harvest() 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 | from curses import ascii |
||
| 70 | def test_Keystroke_harvest(nvim): |
||
| 71 | now = datetime.now() |
||
| 72 | with patch('neovim_prompt.keystroke.datetime') as m1: |
||
| 73 | def side_effect(*args): |
||
| 74 | yield ord('a') |
||
| 75 | yield ord('\x08') # ^H |
||
| 76 | yield list(b'\x80kI') # Insert |
||
| 77 | m1.now.return_value += timedelta(milliseconds=1000) |
||
| 78 | yield ord('b') |
||
| 79 | m1.now.return_value += timedelta(milliseconds=1001) |
||
| 80 | yield ord('c') |
||
| 81 | m1.now.return_value = now |
||
| 82 | nvim.call = MagicMock() |
||
| 83 | nvim.call.side_effect = side_effect() |
||
| 84 | nvim.options = { |
||
| 85 | 'timeout': True, |
||
| 86 | 'timeoutlen': 1000, |
||
| 87 | } |
||
| 88 | |||
| 89 | keys = None |
||
| 90 | timeout = None |
||
| 91 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 92 | assert keys == Keystroke.parse(nvim, b'a') |
||
| 93 | |||
| 94 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 95 | assert keys == Keystroke.parse(nvim, b'a<C-H>') |
||
| 96 | |||
| 97 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 98 | assert keys == Keystroke.parse(nvim, b'a<C-H><INS>') |
||
| 99 | |||
| 100 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 101 | assert keys == Keystroke.parse(nvim, b'a<C-H><INS>b') |
||
| 102 | |||
| 103 | # timeout |
||
| 104 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 105 | assert keys == Keystroke.parse(nvim, b'c') |
||
| 106 | |||
| 107 | |||
| 108 | nvim.call.side_effect = side_effect() |
||
| 109 | nvim.options = { |
||
| 110 | 'timeout': False, |
||
| 111 | 'timeoutlen': 1000, |
||
| 112 | } |
||
| 113 | |||
| 114 | keys = None |
||
| 115 | timeout = None |
||
| 116 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 117 | assert keys == Keystroke.parse(nvim, b'a') |
||
| 118 | |||
| 119 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 120 | assert keys == Keystroke.parse(nvim, b'a<C-H>') |
||
| 121 | |||
| 122 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 123 | assert keys == Keystroke.parse(nvim, b'a<C-H><INS>') |
||
| 124 | |||
| 125 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 126 | assert keys == Keystroke.parse(nvim, b'a<C-H><INS>b') |
||
| 127 | |||
| 128 | keys, timeout = Keystroke.harvest(nvim, keys, timeout) |
||
| 129 | assert keys == Keystroke.parse(nvim, b'a<C-H><INS>bc') |
||
| 130 | |||
| 159 |