Skip to content

Commit 27f8a9b

Browse files
authored
Merge pull request faif#179 from 0--key/dev
Prototype and prototype dispatcher test suites
2 parents 8414931 + 02422bc commit 27f8a9b

File tree

10 files changed

+170
-38
lines changed

10 files changed

+170
-38
lines changed

behavioral/command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
3+
from __future__ import print_function
44
import os
55
from os.path import lexists
66

@@ -18,7 +18,7 @@ def undo(self):
1818
self.rename(self.dest, self.src)
1919

2020
def rename(self, src, dest):
21-
print('renaming {} to {}'.format(src, dest))
21+
print(u"renaming %s to %s" % (src, dest))
2222
os.rename(src, dest)
2323

2424

behavioral/observer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from __future__ import print_function
34
"""http://code.activestate.com/recipes/131499-observer-pattern/"""
45

56

@@ -45,14 +46,14 @@ def data(self, value):
4546
class HexViewer:
4647

4748
def update(self, subject):
48-
print('HexViewer: Subject %s has data 0x%x' %
49+
print(u'HexViewer: Subject %s has data 0x%x' %
4950
(subject.name, subject.data))
5051

5152

5253
class DecimalViewer:
5354

5455
def update(self, subject):
55-
print('DecimalViewer: Subject %s has data %d' %
56+
print(u'DecimalViewer: Subject %s has data %d' %
5657
(subject.name, subject.data))
5758

5859

@@ -67,20 +68,20 @@ def main():
6768
data2.attach(view2)
6869
data2.attach(view1)
6970

70-
print("Setting Data 1 = 10")
71+
print(u"Setting Data 1 = 10")
7172
data1.data = 10
72-
print("Setting Data 2 = 15")
73+
print(u"Setting Data 2 = 15")
7374
data2.data = 15
74-
print("Setting Data 1 = 3")
75+
print(u"Setting Data 1 = 3")
7576
data1.data = 3
76-
print("Setting Data 2 = 5")
77+
print(u"Setting Data 2 = 5")
7778
data2.data = 5
78-
print("Detach HexViewer from data1 and data2.")
79+
print(u"Detach HexViewer from data1 and data2.")
7980
data1.detach(view2)
8081
data2.detach(view2)
81-
print("Setting Data 1 = 10")
82+
print(u"Setting Data 1 = 10")
8283
data1.data = 10
83-
print("Setting Data 2 = 15")
84+
print(u"Setting Data 2 = 15")
8485
data2.data = 15
8586

8687

behavioral/state.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def scan(self):
1515
self.pos += 1
1616
if self.pos == len(self.stations):
1717
self.pos = 0
18-
print("Scanning... Station is", self.stations[self.pos], self.name)
18+
print(u"Scanning... Station is %s %s" %
19+
(self.stations[self.pos], self.name))
1920

2021

2122
class AmState(State):
@@ -27,7 +28,7 @@ def __init__(self, radio):
2728
self.name = "AM"
2829

2930
def toggle_amfm(self):
30-
print("Switching to FM")
31+
print(u"Switching to FM")
3132
self.radio.state = self.radio.fmstate
3233

3334

@@ -40,7 +41,7 @@ def __init__(self, radio):
4041
self.name = "FM"
4142

4243
def toggle_amfm(self):
43-
print("Switching to AM")
44+
print(u"Switching to AM")
4445
self.radio.state = self.radio.amstate
4546

4647

creational/lazy_evaluation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
https://github.com/pallets/werkzeug/blob/5a2bf35441006d832ab1ed5a31963cbc366c99ac/werkzeug/utils.py#L35
2020
"""
2121

22-
22+
from __future__ import print_function
2323
import functools
2424

2525

@@ -52,11 +52,11 @@ def relatives(self):
5252

5353
def main():
5454
Jhon = Person('Jhon', 'Coder')
55-
print("Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
56-
print("Before we access `relatives`:")
55+
print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
56+
print(u"Before we access `relatives`:")
5757
print(Jhon.__dict__)
58-
print("Jhon's relatives: {0}".format(Jhon.relatives))
59-
print("After we've accessed `relatives`:")
58+
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
59+
print(u"After we've accessed `relatives`:")
6060
print(Jhon.__dict__)
6161

6262

structural/facade.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
3+
from __future__ import print_function
44
import time
55

66
SLEEP = 0.1
@@ -10,43 +10,43 @@
1010
class TC1:
1111

1212
def run(self):
13-
print("###### In Test 1 ######")
13+
print(u"###### In Test 1 ######")
1414
time.sleep(SLEEP)
15-
print("Setting up")
15+
print(u"Setting up")
1616
time.sleep(SLEEP)
17-
print("Running test")
17+
print(u"Running test")
1818
time.sleep(SLEEP)
19-
print("Tearing down")
19+
print(u"Tearing down")
2020
time.sleep(SLEEP)
21-
print("Test Finished\n")
21+
print(u"Test Finished\n")
2222

2323

2424
class TC2:
2525

2626
def run(self):
27-
print("###### In Test 2 ######")
27+
print(u"###### In Test 2 ######")
2828
time.sleep(SLEEP)
29-
print("Setting up")
29+
print(u"Setting up")
3030
time.sleep(SLEEP)
31-
print("Running test")
31+
print(u"Running test")
3232
time.sleep(SLEEP)
33-
print("Tearing down")
33+
print(u"Tearing down")
3434
time.sleep(SLEEP)
35-
print("Test Finished\n")
35+
print(u"Test Finished\n")
3636

3737

3838
class TC3:
3939

4040
def run(self):
41-
print("###### In Test 3 ######")
41+
print(u"###### In Test 3 ######")
4242
time.sleep(SLEEP)
43-
print("Setting up")
43+
print(u"Setting up")
4444
time.sleep(SLEEP)
45-
print("Running test")
45+
print(u"Running test")
4646
time.sleep(SLEEP)
47-
print("Tearing down")
47+
print(u"Tearing down")
4848
time.sleep(SLEEP)
49-
print("Test Finished\n")
49+
print(u"Test Finished\n")
5050

5151

5252
# Facade

tests/test_decorator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
from structural.decorator import TextTag, BoldWrapper, ItalicWrapper
5+
6+
7+
class TestTextWrapping(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.raw_string = TextTag('raw but not cruel')
11+
12+
def test_italic(self):
13+
self.assertEqual(ItalicWrapper(self.raw_string).render(),
14+
'<i>raw but not cruel</i>')
15+
16+
def test_bold(self):
17+
self.assertEqual(BoldWrapper(self.raw_string).render(),
18+
'<b>raw but not cruel</b>')
19+
20+
def test_mixed_bold_and_italic(self):
21+
self.assertEqual(
22+
BoldWrapper(ItalicWrapper(self.raw_string)).render(),
23+
'<b><i>raw but not cruel</i></b>')
24+

tests/test_facade.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
from structural.facade import TestRunner, TC1, TC2, TC3
5+
6+
7+
class TestRunnerFacilities(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.tc = TC1()
11+
self.average_result_tc1 = "###### In Test 1 ######\n" + \
12+
"Setting up\n" + \
13+
"Running test\n" + \
14+
"Tearing down\n" + \
15+
"Test Finished"
16+
17+
def test_tc1_output(self):
18+
import sys
19+
try:
20+
from io import StringIO
21+
except:
22+
from StringIO import StringIO
23+
out = StringIO()
24+
sys.stdout = out
25+
self.tc.run()
26+
output = out.getvalue().strip()
27+
self.assertEqual(output, self.average_result_tc1)
28+
29+
30+
# ###### In Test 2 ######
31+
# Setting up
32+
# Running test
33+
# Tearing down
34+
# Test Finished
35+
36+
# ###### In Test 3 ######
37+
# Setting up
38+
# Running test
39+
# Tearing down
40+
# Test Finished"""
41+
42+
# def test_bunch_launch(self):
43+
# import sys
44+
# try:
45+
# from io import StringIO
46+
# except:
47+
# from StringIO import StringIO
48+
# out = StringIO()
49+
# sys.stdout = out
50+
# self.runner.runAll()
51+
# output = out.getvalue().strip()
52+
# self.assertEqual(output, self.average_result)
53+
54+

tests/test_lazy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
from __future__ import print_function
34
import unittest
45
from creational.lazy_evaluation import Person
56

@@ -17,11 +18,11 @@ def test_relatives_not_in_properties(self):
1718
self.assertNotIn('relatives', self.John.__dict__)
1819

1920
def test_extended_properties(self):
20-
print("John's relatives: {0}".format(self.John.relatives))
21+
print(u"John's relatives: {0}".format(self.John.relatives))
2122
self.assertDictEqual({'name': 'John', 'occupation': 'Coder',
2223
'relatives': 'Many relatives.'},
2324
self.John.__dict__)
2425

2526
def test_relatives_after_access(self):
26-
print("John's relatives: {0}".format(self.John.relatives))
27+
print(u"John's relatives: {0}".format(self.John.relatives))
2728
self.assertIn('relatives', self.John.__dict__)

tests/test_prototype.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
from creational.prototype import Prototype, PrototypeDispatcher
5+
6+
7+
class TestPrototypeFeatures(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.prototype = Prototype()
11+
12+
def test_cloning_propperty_innate_values(self):
13+
sample_object_1 = self.prototype.clone()
14+
sample_object_2 = self.prototype.clone()
15+
self.assertEqual(sample_object_1.value, sample_object_2.value)
16+
17+
def test_extended_property_values_cloning(self):
18+
sample_object_1 = self.prototype.clone()
19+
sample_object_1.some_value = 'test string'
20+
sample_object_2 = self.prototype.clone()
21+
self.assertRaises(AttributeError, lambda: sample_object_2.some_value)
22+
23+
def test_cloning_propperty_assigned_values(self):
24+
sample_object_1 = self.prototype.clone()
25+
sample_object_2 = self.prototype.clone(value='re-assigned')
26+
self.assertNotEqual(sample_object_1.value, sample_object_2.value)
27+
28+
29+
class TestDispatcherFeatures(unittest.TestCase):
30+
31+
def setUp(self):
32+
self.dispatcher = PrototypeDispatcher()
33+
self.prototype = Prototype()
34+
c = self.prototype.clone()
35+
a = self.prototype.clone(value='a-value', ext_value='E')
36+
b = self.prototype.clone(value='b-value', diff=True)
37+
self.dispatcher.register_object('A', a)
38+
self.dispatcher.register_object('B', b)
39+
self.dispatcher.register_object('C', c)
40+
41+
def test_batch_retrieving(self):
42+
self.assertEqual(len(self.dispatcher.get_objects()), 3)
43+
44+
def test_particular_properties_retrieving(self):
45+
self.assertEqual(self.dispatcher.get_objects()['A'].value, 'a-value')
46+
self.assertEqual(self.dispatcher.get_objects()['B'].value, 'b-value')
47+
self.assertEqual(self.dispatcher.get_objects()['C'].value, 'default')
48+
49+
def test_extended_properties_retrieving(self):
50+
self.assertEqual(self.dispatcher.get_objects()['A'].ext_value, 'E')
51+
self.assertTrue(self.dispatcher.get_objects()['B'].diff)
52+

tests/test_state.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
import sys
43
import unittest
54
from behavioral.state import Radio
65

0 commit comments

Comments
 (0)