|
@@ 85-104 (lines=20) @@
|
| 82 |
|
assert result == 'ClientSubject' |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
def test_simple_proxy(): |
| 86 |
|
from typing import Callable |
| 87 |
|
|
| 88 |
|
from software_patterns import Proxy |
| 89 |
|
|
| 90 |
|
RemoteCall = Callable[[int], int] |
| 91 |
|
|
| 92 |
|
def remote_call(x): |
| 93 |
|
return x + 1 |
| 94 |
|
|
| 95 |
|
# Code that the developer writes |
| 96 |
|
VALUE = 10 |
| 97 |
|
|
| 98 |
|
class ClientProxy(Proxy[RemoteCall]): |
| 99 |
|
def __call__(self, x: int): |
| 100 |
|
return self._proxy_subject(x + VALUE) |
| 101 |
|
|
| 102 |
|
proxy: ClientProxy = ClientProxy(remote_call) |
| 103 |
|
|
| 104 |
|
assert remote_call(2) == 2 + 1 |
| 105 |
|
assert proxy(2) == 2 + VALUE + 1 |
| 106 |
|
|
| 107 |
|
|
|
@@ 108-130 (lines=23) @@
|
| 105 |
|
assert proxy(2) == 2 + VALUE + 1 |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
def test_proxy_as_instance(): |
| 109 |
|
from typing import Callable |
| 110 |
|
|
| 111 |
|
from software_patterns import Proxy |
| 112 |
|
|
| 113 |
|
RemoteCall = Callable[[int], int] |
| 114 |
|
|
| 115 |
|
def remote_call(x): |
| 116 |
|
return x + 1 |
| 117 |
|
|
| 118 |
|
# Code that the developer writes |
| 119 |
|
VALUE = 10 |
| 120 |
|
|
| 121 |
|
class ClientProxy(Proxy[RemoteCall]): |
| 122 |
|
def __call__(self, x: int): |
| 123 |
|
return self._proxy_subject(x + VALUE) |
| 124 |
|
|
| 125 |
|
proxy: ClientProxy = ClientProxy(remote_call) |
| 126 |
|
|
| 127 |
|
assert remote_call(2) == 2 + 1 |
| 128 |
|
assert proxy(2) == 2 + VALUE + 1 |
| 129 |
|
|
| 130 |
|
assert hash(proxy) == hash(remote_call) |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
def test_mapping_proxy(): |