Skip to content

Commit b91d374

Browse files
committed
Merge branch 'IuryAlves-master'
2 parents ac91aea + 262edd3 commit b91d374

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

catalog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _instance_method_2(self):
7575

7676
def main_method(self):
7777
"""
78-
will execute either _instance_method_1 or _instance_method_1
78+
will execute either _instance_method_1 or _instance_method_2
7979
depending on self.param value
8080
"""
8181
self._instance_method_choices[self.param].__get__(self)()
@@ -111,7 +111,7 @@ def _class_method_2(cls):
111111

112112
def main_method(self):
113113
"""
114-
will execute either _instance_method_1 or _instance_method_1
114+
will execute either _class_method_1 or _class_method_2
115115
depending on self.param value
116116
"""
117117
self._class_method_choices[self.param].__get__(None, self.__class__)()
@@ -143,7 +143,7 @@ def _static_method_2():
143143

144144
def main_method(self):
145145
"""
146-
will execute either _instance_method_1 or _instance_method_1
146+
will execute either _static_method_1 or _static_method_2
147147
depending on self.param value
148148
"""
149149
self._static_method_choices[self.param].__get__(None, self.__class__)()

delegation_pattern.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Reference: https://en.wikipedia.org/wiki/Delegation_pattern
6+
Author: https://github.com/IuryAlves
7+
"""
8+
9+
10+
class Delegator(object):
11+
"""
12+
>>> delegator = Delegator(Delegate())
13+
>>> delegator.do_something("nothing")
14+
'Doing nothing'
15+
>>> delegator.do_anything()
16+
17+
"""
18+
def __init__(self, delegate):
19+
self.delegate = delegate
20+
21+
def __getattr__(self, name):
22+
def wrapper(*args, **kwargs):
23+
if hasattr(self.delegate, name):
24+
attr = getattr(self.delegate, name)
25+
if callable(attr):
26+
return attr(*args, **kwargs)
27+
return wrapper
28+
29+
30+
class Delegate(object):
31+
32+
def do_something(self, something):
33+
return "Doing %s" % something
34+
35+
36+
if __name__ == '__main__':
37+
import doctest
38+
doctest.testmod()

mvc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def __iter__(self):
66
raise NotImplementedError
77

88
def get(self, item):
9-
"""Returns an an object with a .items() call method
9+
"""Returns an object with a .items() call method
1010
that iterates over key,value pairs of its information."""
1111
raise NotImplementedError
1212

test_bridge.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from bridge import DrawingAPI1, DrawingAPI2, CircleShape
5+
from sys import version_info
6+
7+
if version_info < (2, 7):
8+
import unittest2 as unittest
9+
else:
10+
import unittest
11+
12+
from unittest.mock import patch
13+
14+
class BridgeTest(unittest.TestCase):
15+
16+
def test_bridge_shall_draw_with_concrete_implementation(cls):
17+
ci1 = DrawingAPI1()
18+
ci2 = DrawingAPI2()
19+
with patch.object(ci1, 'draw_circle') as mock_ci1_draw_circle,\
20+
patch.object(ci2, 'draw_circle') as mock_ci2_draw_circle:
21+
sh1 = CircleShape(1, 2, 3, ci1)
22+
sh1.draw()
23+
cls.assertEqual(mock_ci1_draw_circle.call_count, 1)
24+
sh2 = CircleShape(1, 2, 3, ci2)
25+
sh2.draw()
26+
cls.assertEqual(mock_ci2_draw_circle.call_count, 1)
27+
28+
def test_bridge_shall_scale_with_own_implementation(cls):
29+
ci = DrawingAPI1()
30+
sh = CircleShape(1, 2, 3, ci)
31+
sh.scale(2)
32+
cls.assertEqual(sh._radius, 6)
33+
with patch.object(sh, 'scale') as mock_sh_scale_circle:
34+
sh.scale(2)
35+
cls.assertEqual(mock_sh_scale_circle.call_count, 1)
36+
37+
if __name__ == "__main__":
38+
unittest.main()
39+

test_flyweight.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from flyweight import Card
5+
from sys import version_info
6+
7+
if version_info < (2, 7):
8+
import unittest2 as unittest
9+
else:
10+
import unittest
11+
12+
class TestCard(unittest.TestCase):
13+
14+
def test_instances_shall_reference_same_object(self):
15+
c1 = Card('9', 'h')
16+
c2 = Card('9', 'h')
17+
self.assertEqual(c1, c2)
18+
self.assertEqual(id(c1), id(c2))
19+
20+
def test_instances_with_different_suit_shall_reference_different_objects(self):
21+
c1 = Card('9', 'a')
22+
c2 = Card('9', 'b')
23+
self.assertNotEqual(id(c1), id(c2))
24+
25+
def test_instances_with_different_values_shall_reference_different_objects(self):
26+
c1 = Card('9', 'h')
27+
c2 = Card('A', 'h')
28+
self.assertNotEqual(id(c1), id(c2))
29+
30+
def test_instances_shall_share_additional_attributes(self):
31+
expected_attribute_name = 'attr'
32+
expected_attribute_value = 'value of attr'
33+
c1 = Card('9', 'h')
34+
c1.attr = expected_attribute_value
35+
c2 = Card('9', 'h')
36+
self.assertEqual(hasattr(c2, expected_attribute_name), True)
37+
self.assertEqual(c2.attr, expected_attribute_value)
38+
39+
if __name__ == "__main__":
40+
unittest.main()
41+

0 commit comments

Comments
 (0)