-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
57 lines (46 loc) · 1.4 KB
/
inheritance.py
File metadata and controls
57 lines (46 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# inheritance
# Dependancy injection
import unittest
class Robot(object):
"Class to move a real robot"
def fetch(self, tool):
print("Real movement! Fetching")
def move_forward(self, tool):
print("Real movement! move forward")
def move_backward(self, tool):
print("Real movement! move backward")
def replace(self, tool):
print("Real movement! replacing")
class CleaningRobot(Robot):
"Custom robot"
def clean(self, tool, times=10):
super(CleaningRobot, self).fetch(tool)
for i in range(times):
super(CleaningRobot, self).move_forward(tool)
super(CleaningRobot, self).move_backward(tool)
super(CleaningRobot, self).replace(tool)
c = CleaningRobot()
c.clean("map")
class MockRobot(Robot):
"Simulate a real robot"
def __init__(self):
self.tasks = []
def fetch(self, tool):
self.tasks.append('fetching %s' % tool)
def move_forward(self, tool):
self.tasks.append('forward %s' % tool)
def move_backward(self, tool):
self.tasks.append('backward %s' % tool)
def replace(self, tool):
self.tasks.append('replace %s' % tool)
class MockedCleaningRobot(CleaningRobot, MockRobot):
'Inject a mock robot into the CleaningRobot'
class TestCleaningRobot(unittest.TestCase):
def test_clean(self):
t = MockedCleaningRobot()
t.clean('map')
expected = (['fetching map'] +
['forward map', 'backward map'] * 10 +
['replace map'])
self.assertEqual(t.tasks, expected)
unittest.main()