| Conditions | 16 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 TestStarStruct.test_verifying_unpack() 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 | #!/usr/bin/env python3 |
||
| 243 | def test_verifying_unpack(self): |
||
| 244 | def adder(*args): |
||
| 245 | return sum(args) |
||
| 246 | |||
| 247 | AdderMessage = Message('AdderMessage', [ |
||
| 248 | ('item_a', 'H'), |
||
| 249 | ('item_b', 'B'), |
||
| 250 | ('item_c', 'B'), |
||
| 251 | ('item_d', 'B'), |
||
| 252 | ('item_e', 'B'), |
||
| 253 | # Note, there is no item 'e' in the list of arguments |
||
| 254 | ('function_data', 'I', { |
||
| 255 | (adder, 'item_a', 'item_b', 'item_c', 'item_d') |
||
| 256 | }), |
||
| 257 | ]) |
||
| 258 | |||
| 259 | # Test getting the correct result |
||
| 260 | test_data = { |
||
| 261 | 'item_a': 2, |
||
| 262 | 'item_b': 5, |
||
| 263 | 'item_c': 7, |
||
| 264 | 'item_d': 4, |
||
| 265 | 'item_e': 6, |
||
| 266 | } |
||
| 267 | |||
| 268 | made = AdderMessage.make(test_data) |
||
| 269 | assert made.item_a == 2 |
||
| 270 | assert made.item_b == 5 |
||
| 271 | |||
| 272 | assert made.function_data == 2 + 5 + 7 + 4 |
||
| 273 | |||
| 274 | # Check packing and unpacking |
||
| 275 | packed = AdderMessage.pack(test_data) |
||
| 276 | assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' |
||
| 277 | assert packed == made.pack() |
||
| 278 | |||
| 279 | unpacked = AdderMessage.unpack(packed) |
||
| 280 | assert made == unpacked |
||
| 281 | |||
| 282 | # Now we modify the data we are going to unpack, and we should get an error |
||
| 283 | modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' |
||
| 284 | |||
| 285 | with pytest.raises(ValueError): |
||
| 286 | unpacked = AdderMessage.unpack(modified_packed) |
||
| 287 | |||
| 288 | AdderMessageFalse = Message('AdderMessageFalse', [ |
||
| 289 | ('item_a', 'H'), |
||
| 290 | ('item_b', 'B'), |
||
| 291 | ('item_c', 'B'), |
||
| 292 | ('item_d', 'B'), |
||
| 293 | ('item_e', 'B'), |
||
| 294 | # Note, there is no item 'e' in the list of arguments |
||
| 295 | ('function_data', 'I', { |
||
| 296 | (adder, 'item_a', 'item_b', 'item_c', 'item_d') |
||
| 297 | }, False), |
||
| 298 | ]) |
||
| 299 | |||
| 300 | # Test getting the correct result |
||
| 301 | test_data = { |
||
| 302 | 'item_a': 2, |
||
| 303 | 'item_b': 5, |
||
| 304 | 'item_c': 7, |
||
| 305 | 'item_d': 4, |
||
| 306 | 'item_e': 6, |
||
| 307 | } |
||
| 308 | |||
| 309 | made = AdderMessageFalse.make(test_data) |
||
| 310 | assert made.item_a == 2 |
||
| 311 | assert made.item_b == 5 |
||
| 312 | |||
| 313 | assert made.function_data == 2 + 5 + 7 + 4 |
||
| 314 | |||
| 315 | # Check packing and unpacking |
||
| 316 | packed = AdderMessageFalse.pack(test_data) |
||
| 317 | assert packed == b'\x02\x00\x05\x07\x04\x06\x12\x00\x00\x00' |
||
| 318 | assert packed == made.pack() |
||
| 319 | |||
| 320 | unpacked = AdderMessageFalse.unpack(packed) |
||
| 321 | assert made == unpacked |
||
| 322 | |||
| 323 | # Now we modify the data we are going to unpack, and we should get an error |
||
| 324 | modified_packed = b'\x02\x00\x05\x07\x04\x06\x11\x11\x11\x11' |
||
| 325 | |||
| 326 | # This time it won't fail because we set False for this message |
||
| 327 | unpacked = AdderMessageFalse.unpack(modified_packed) |
||
| 328 | assert unpacked.item_a == 2 |
||
| 329 |