Conditions | 1 |
Total Lines | 71 |
Code Lines | 45 |
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 pytest |
||
12 | def test_proxy_behaviour(dummy_handle, capsys): |
||
13 | from typing import List |
||
14 | |||
15 | from software_patterns import Proxy |
||
16 | |||
17 | # replicate client code that wants to use the proxy pattern |
||
18 | class ClientSubject(object): |
||
19 | """ "A class with a request instance method.""" |
||
20 | |||
21 | def request(self, *args, **kwargs): |
||
22 | print(dummy_handle(self, *args, **kwargs)) |
||
23 | return type(self).__name__ |
||
24 | |||
25 | # Derive from Proxy |
||
26 | class ClientProxy(Proxy): |
||
27 | def request(self, *args, **kwargs): |
||
28 | |||
29 | # run proxy code before sending request to the "proxied" handler |
||
30 | before_args = list(['before'] + list(args)) |
||
31 | print(dummy_handle(self, *before_args, **kwargs)) |
||
32 | # handle request with the proxied logic |
||
33 | # _ = super().request(*args, **kwargs) |
||
34 | _ = self._proxy_subject.request(*args, **kwargs) |
||
35 | assert _ == 'ClientSubject' |
||
36 | |||
37 | # run proxy code after request to the "proxied" handler |
||
38 | after_args = list(['after'] + list(args)) |
||
39 | print(dummy_handle(self, *after_args, **kwargs)) |
||
40 | return _ |
||
41 | |||
42 | real_subject = ClientSubject() |
||
43 | proxy = ClientProxy(real_subject) |
||
44 | |||
45 | # use proxy in a scenario |
||
46 | |||
47 | # First test what happens without using proxy |
||
48 | args: List = [1, 2] |
||
49 | kwargs = {'k1': 'v1'} |
||
50 | result = real_subject.request(*args, **kwargs) |
||
51 | |||
52 | captured = capsys.readouterr() |
||
53 | assert captured.out == dummy_handle(real_subject, 1, 2, k1='v1') + '\n' |
||
54 | assert ( |
||
55 | captured.out |
||
56 | == f'ClientSubject handle request with args [{", ".join(str(_) for _ in args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' |
||
57 | ) |
||
58 | assert result == type(real_subject).__name__ |
||
59 | assert result == 'ClientSubject' |
||
60 | |||
61 | # Now test what happens using proxy |
||
62 | result = proxy.request(*args, **kwargs) |
||
63 | |||
64 | captured = capsys.readouterr() |
||
65 | |||
66 | assert ( |
||
67 | captured.out |
||
68 | == dummy_handle(*list([proxy, 'before'] + args), **kwargs) |
||
69 | + '\n' |
||
70 | + dummy_handle(*list([real_subject] + args), **kwargs) |
||
71 | + '\n' |
||
72 | + dummy_handle(*list([proxy, 'after'] + args), **kwargs) |
||
73 | + '\n' |
||
74 | ) |
||
75 | assert ( |
||
76 | captured.out |
||
77 | == f'ClientProxy handle request with args [{", ".join(str(_) for _ in ["before"] + args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' |
||
78 | + f'ClientSubject handle request with args [{", ".join(str(_) for _ in args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' |
||
79 | + f'ClientProxy handle request with args [{", ".join(str(_) for _ in ["after"] + args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' |
||
80 | ) |
||
81 | assert result == type(real_subject).__name__ |
||
82 | assert result == 'ClientSubject' |
||
83 | |||
180 |