Conditions | 15 |
Total Lines | 79 |
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_keymap_harvest_timeout() 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 |
||
128 | def test_keymap_harvest_timeout(nvim): |
||
129 | nvim.options = { |
||
130 | 'timeout': True, |
||
131 | 'timeoutlen': 1000, |
||
132 | 'encoding': 'utf-8', |
||
133 | } |
||
134 | |||
135 | now = datetime.now() |
||
136 | with patch('neovim_prompt.keymap.datetime') as m1: |
||
137 | keymap = Keymap() |
||
138 | keymap.register_from_rules(nvim, [ |
||
139 | ('<C-H>', '<prompt:CH>', True), |
||
140 | ('<C-H><C-H>', '<prompt:CHCH>', True), |
||
141 | ]) |
||
142 | |||
143 | # Keypress within timeoutlen |
||
144 | def side_effect(*args): |
||
145 | yield ord('\x08') # ^H |
||
146 | m1.now.return_value += timedelta(milliseconds=999) |
||
147 | yield 0 |
||
148 | yield ord('\x08') # ^H |
||
149 | |||
150 | m1.now.return_value = now |
||
151 | nvim.call = MagicMock() |
||
152 | nvim.call.side_effect = side_effect() |
||
153 | |||
154 | keystroke = keymap.harvest(nvim) |
||
155 | assert keystroke == Keystroke.parse(nvim, '<prompt:CHCH>') |
||
156 | with pytest.raises(StopIteration): |
||
157 | nvim.call() |
||
158 | |||
159 | # Keypress after timeoutlen |
||
160 | def side_effect(*args): |
||
161 | yield ord('\x08') # ^H |
||
162 | m1.now.return_value += timedelta(milliseconds=1000) |
||
163 | yield 0 |
||
164 | yield ord('\x08') # ^H |
||
165 | |||
166 | m1.now.return_value = now |
||
167 | nvim.call.side_effect = side_effect() |
||
168 | |||
169 | keystroke = keymap.harvest(nvim) |
||
170 | assert keystroke == Keystroke.parse(nvim, '<prompt:CH>') |
||
171 | assert nvim.call() == ord('\x08') # residual |
||
172 | |||
173 | # Timeout without keypress |
||
174 | def side_effect(*args): |
||
175 | m1.now.return_value += timedelta(milliseconds=1000) |
||
176 | yield 0 |
||
177 | yield ord('\x08') # ^H |
||
178 | yield ord('\x08') # ^H |
||
179 | |||
180 | m1.now.return_value = now |
||
181 | nvim.call.side_effect = side_effect() |
||
182 | |||
183 | keystroke = keymap.harvest(nvim) |
||
184 | assert keystroke == Keystroke.parse(nvim, '<prompt:CHCH>') |
||
185 | with pytest.raises(StopIteration): |
||
186 | nvim.call() |
||
187 | |||
188 | # Keypress within timeoutlen but with nowait |
||
189 | keymap.register_from_rules(nvim, [ |
||
190 | ('<C-H>', '<prompt:CH>', True, True), |
||
191 | ('<C-H><C-H>', '<prompt:CHCH>', True), |
||
192 | ]) |
||
193 | def side_effect(*args): |
||
194 | yield ord('\x08') # ^H |
||
195 | m1.now.return_value += timedelta(milliseconds=999) |
||
196 | yield 0 |
||
197 | yield ord('\x08') # ^H |
||
198 | |||
199 | m1.now.return_value = now |
||
200 | nvim.call = MagicMock() |
||
201 | nvim.call.side_effect = side_effect() |
||
202 | |||
203 | keystroke = keymap.harvest(nvim) |
||
204 | assert keystroke == Keystroke.parse(nvim, '<prompt:CH>') |
||
205 | assert nvim.call() == 0 # residual |
||
206 | assert nvim.call() == ord('\x08') # residual |
||
207 | |||
277 |