forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_adapter.py
82 lines (67 loc) · 2.61 KB
/
test_adapter.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
from adapter import Dog, Cat, Human, Car, Adapter
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
class ClassTest(unittest.TestCase):
@classmethod
def setUpClass(self):
self.dog = Dog()
self.cat = Cat()
self.human = Human()
self.car = Car()
def test_dog_shall_bark(self):
noise = self.dog.bark()
expected_noise = "woof!"
self.assertEqual(noise, expected_noise)
def test_cat_shall_meow(self):
noise = self.cat.meow()
expected_noise = "meow!"
self.assertEqual(noise, expected_noise)
def test_human_shall_speak(self):
noise = self.human.speak()
expected_noise = "'hello'"
self.assertEqual(noise, expected_noise)
def test_car_shall_make_loud_noise(self):
noise = self.car.make_noise(1)
expected_noise = "vroom!"
self.assertEqual(noise, expected_noise)
def test_car_shall_make_very_loud_noise(self):
noise = self.car.make_noise(10)
expected_noise = "vroom!!!!!!!!!!"
self.assertEqual(noise, expected_noise)
class AdapterTest(unittest.TestCase):
def test_dog_adapter_shall_make_noise(self):
dog = Dog()
dog_adapter = Adapter(dog, make_noise=dog.bark)
noise = dog_adapter.make_noise()
expected_noise = "woof!"
self.assertEqual(noise, expected_noise)
def test_cat_adapter_shall_make_noise(self):
cat = Cat()
cat_adapter = Adapter(cat, make_noise=cat.meow)
noise = cat_adapter.make_noise()
expected_noise = "meow!"
self.assertEqual(noise, expected_noise)
def test_human_adapter_shall_make_noise(self):
human = Human()
human_adapter = Adapter(human, make_noise=human.speak)
noise = human_adapter.make_noise()
expected_noise = "'hello'"
self.assertEqual(noise, expected_noise)
def test_car_adapter_shall_make_loud_noise(self):
car = Car()
car_adapter = Adapter(car, make_noise=car.make_noise)
noise = car_adapter.make_noise(1)
expected_noise = "vroom!"
self.assertEqual(noise, expected_noise)
def test_car_adapter_shall_make_very_loud_noise(self):
car = Car()
car_adapter = Adapter(car, make_noise=car.make_noise)
noise = car_adapter.make_noise(10)
expected_noise = "vroom!!!!!!!!!!"
self.assertEqual(noise, expected_noise)
if __name__ == "__main__":
unittest.main()