Skip to content
4 changes: 2 additions & 2 deletions behavioral/command.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import os
from os.path import lexists

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

def rename(self, src, dest):
print('renaming {} to {}'.format(src, dest))
print(u"renaming %s to %s" % (src, dest))
os.rename(src, dest)


Expand Down
19 changes: 10 additions & 9 deletions behavioral/observer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
"""http://code.activestate.com/recipes/131499-observer-pattern/"""


Expand Down Expand Up @@ -45,14 +46,14 @@ def data(self, value):
class HexViewer:

def update(self, subject):
print('HexViewer: Subject %s has data 0x%x' %
print(u'HexViewer: Subject %s has data 0x%x' %
(subject.name, subject.data))


class DecimalViewer:

def update(self, subject):
print('DecimalViewer: Subject %s has data %d' %
print(u'DecimalViewer: Subject %s has data %d' %
(subject.name, subject.data))


Expand All @@ -67,20 +68,20 @@ def main():
data2.attach(view2)
data2.attach(view1)

print("Setting Data 1 = 10")
print(u"Setting Data 1 = 10")
data1.data = 10
print("Setting Data 2 = 15")
print(u"Setting Data 2 = 15")
data2.data = 15
print("Setting Data 1 = 3")
print(u"Setting Data 1 = 3")
data1.data = 3
print("Setting Data 2 = 5")
print(u"Setting Data 2 = 5")
data2.data = 5
print("Detach HexViewer from data1 and data2.")
print(u"Detach HexViewer from data1 and data2.")
data1.detach(view2)
data2.detach(view2)
print("Setting Data 1 = 10")
print(u"Setting Data 1 = 10")
data1.data = 10
print("Setting Data 2 = 15")
print(u"Setting Data 2 = 15")
data2.data = 15


Expand Down
7 changes: 4 additions & 3 deletions behavioral/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def scan(self):
self.pos += 1
if self.pos == len(self.stations):
self.pos = 0
print("Scanning... Station is", self.stations[self.pos], self.name)
print(u"Scanning... Station is %s %s" %
(self.stations[self.pos], self.name))


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

def toggle_amfm(self):
print("Switching to FM")
print(u"Switching to FM")
self.radio.state = self.radio.fmstate


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

def toggle_amfm(self):
print("Switching to AM")
print(u"Switching to AM")
self.radio.state = self.radio.amstate


Expand Down
10 changes: 5 additions & 5 deletions creational/lazy_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
https://github.com/pallets/werkzeug/blob/5a2bf35441006d832ab1ed5a31963cbc366c99ac/werkzeug/utils.py#L35
"""


from __future__ import print_function
import functools


Expand Down Expand Up @@ -52,11 +52,11 @@ def relatives(self):

def main():
Jhon = Person('Jhon', 'Coder')
print("Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
print("Before we access `relatives`:")
print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
print(u"Before we access `relatives`:")
print(Jhon.__dict__)
print("Jhon's relatives: {0}".format(Jhon.relatives))
print("After we've accessed `relatives`:")
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
print(u"After we've accessed `relatives`:")
print(Jhon.__dict__)


Expand Down
32 changes: 16 additions & 16 deletions structural/facade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import time

SLEEP = 0.1
Expand All @@ -10,43 +10,43 @@
class TC1:

def run(self):
print("###### In Test 1 ######")
print(u"###### In Test 1 ######")
time.sleep(SLEEP)
print("Setting up")
print(u"Setting up")
time.sleep(SLEEP)
print("Running test")
print(u"Running test")
time.sleep(SLEEP)
print("Tearing down")
print(u"Tearing down")
time.sleep(SLEEP)
print("Test Finished\n")
print(u"Test Finished\n")


class TC2:

def run(self):
print("###### In Test 2 ######")
print(u"###### In Test 2 ######")
time.sleep(SLEEP)
print("Setting up")
print(u"Setting up")
time.sleep(SLEEP)
print("Running test")
print(u"Running test")
time.sleep(SLEEP)
print("Tearing down")
print(u"Tearing down")
time.sleep(SLEEP)
print("Test Finished\n")
print(u"Test Finished\n")


class TC3:

def run(self):
print("###### In Test 3 ######")
print(u"###### In Test 3 ######")
time.sleep(SLEEP)
print("Setting up")
print(u"Setting up")
time.sleep(SLEEP)
print("Running test")
print(u"Running test")
time.sleep(SLEEP)
print("Tearing down")
print(u"Tearing down")
time.sleep(SLEEP)
print("Test Finished\n")
print(u"Test Finished\n")


# Facade
Expand Down
24 changes: 24 additions & 0 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from structural.decorator import TextTag, BoldWrapper, ItalicWrapper


class TestTextWrapping(unittest.TestCase):

def setUp(self):
self.raw_string = TextTag('raw but not cruel')

def test_italic(self):
self.assertEqual(ItalicWrapper(self.raw_string).render(),
'<i>raw but not cruel</i>')

def test_bold(self):
self.assertEqual(BoldWrapper(self.raw_string).render(),
'<b>raw but not cruel</b>')

def test_mixed_bold_and_italic(self):
self.assertEqual(
BoldWrapper(ItalicWrapper(self.raw_string)).render(),
'<b><i>raw but not cruel</i></b>')

54 changes: 54 additions & 0 deletions tests/test_facade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from structural.facade import TestRunner, TC1, TC2, TC3


class TestRunnerFacilities(unittest.TestCase):

def setUp(self):
self.tc = TC1()
self.average_result_tc1 = "###### In Test 1 ######\n" + \
"Setting up\n" + \
"Running test\n" + \
"Tearing down\n" + \
"Test Finished"

def test_tc1_output(self):
import sys
try:
from io import StringIO
except:
from StringIO import StringIO
out = StringIO()
sys.stdout = out
self.tc.run()
output = out.getvalue().strip()
self.assertEqual(output, self.average_result_tc1)


# ###### In Test 2 ######
# Setting up
# Running test
# Tearing down
# Test Finished

# ###### In Test 3 ######
# Setting up
# Running test
# Tearing down
# Test Finished"""

# def test_bunch_launch(self):
# import sys
# try:
# from io import StringIO
# except:
# from StringIO import StringIO
# out = StringIO()
# sys.stdout = out
# self.runner.runAll()
# output = out.getvalue().strip()
# self.assertEqual(output, self.average_result)


5 changes: 3 additions & 2 deletions tests/test_lazy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import unittest
from creational.lazy_evaluation import Person

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

def test_extended_properties(self):
print("John's relatives: {0}".format(self.John.relatives))
print(u"John's relatives: {0}".format(self.John.relatives))
self.assertDictEqual({'name': 'John', 'occupation': 'Coder',
'relatives': 'Many relatives.'},
self.John.__dict__)

def test_relatives_after_access(self):
print("John's relatives: {0}".format(self.John.relatives))
print(u"John's relatives: {0}".format(self.John.relatives))
self.assertIn('relatives', self.John.__dict__)
52 changes: 52 additions & 0 deletions tests/test_prototype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.prototype import Prototype, PrototypeDispatcher


class TestPrototypeFeatures(unittest.TestCase):

def setUp(self):
self.prototype = Prototype()

def test_cloning_propperty_innate_values(self):
sample_object_1 = self.prototype.clone()
sample_object_2 = self.prototype.clone()
self.assertEqual(sample_object_1.value, sample_object_2.value)

def test_extended_property_values_cloning(self):
sample_object_1 = self.prototype.clone()
sample_object_1.some_value = 'test string'
sample_object_2 = self.prototype.clone()
self.assertRaises(AttributeError, lambda: sample_object_2.some_value)

def test_cloning_propperty_assigned_values(self):
sample_object_1 = self.prototype.clone()
sample_object_2 = self.prototype.clone(value='re-assigned')
self.assertNotEqual(sample_object_1.value, sample_object_2.value)


class TestDispatcherFeatures(unittest.TestCase):

def setUp(self):
self.dispatcher = PrototypeDispatcher()
self.prototype = Prototype()
c = self.prototype.clone()
a = self.prototype.clone(value='a-value', ext_value='E')
b = self.prototype.clone(value='b-value', diff=True)
self.dispatcher.register_object('A', a)
self.dispatcher.register_object('B', b)
self.dispatcher.register_object('C', c)

def test_batch_retrieving(self):
self.assertEqual(len(self.dispatcher.get_objects()), 3)

def test_particular_properties_retrieving(self):
self.assertEqual(self.dispatcher.get_objects()['A'].value, 'a-value')
self.assertEqual(self.dispatcher.get_objects()['B'].value, 'b-value')
self.assertEqual(self.dispatcher.get_objects()['C'].value, 'default')

def test_extended_properties_retrieving(self):
self.assertEqual(self.dispatcher.get_objects()['A'].ext_value, 'E')
self.assertTrue(self.dispatcher.get_objects()['B'].diff)

1 change: 0 additions & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import unittest
from behavioral.state import Radio

Expand Down