diff --git a/behavioral/command.py b/behavioral/command.py index 31e47a0e..7950940d 100644 --- a/behavioral/command.py +++ b/behavioral/command.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +from __future__ import print_function import os from os.path import lexists @@ -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) diff --git a/behavioral/observer.py b/behavioral/observer.py index 8f1ea578..11a0a577 100644 --- a/behavioral/observer.py +++ b/behavioral/observer.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import print_function """http://code.activestate.com/recipes/131499-observer-pattern/""" @@ -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)) @@ -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 diff --git a/behavioral/state.py b/behavioral/state.py index 915affe0..801fe840 100644 --- a/behavioral/state.py +++ b/behavioral/state.py @@ -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): @@ -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 @@ -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 diff --git a/creational/lazy_evaluation.py b/creational/lazy_evaluation.py index 4806c750..51d36d22 100644 --- a/creational/lazy_evaluation.py +++ b/creational/lazy_evaluation.py @@ -19,7 +19,7 @@ https://github.com/pallets/werkzeug/blob/5a2bf35441006d832ab1ed5a31963cbc366c99ac/werkzeug/utils.py#L35 """ - +from __future__ import print_function import functools @@ -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__) diff --git a/structural/facade.py b/structural/facade.py index b314b1b3..571f078b 100644 --- a/structural/facade.py +++ b/structural/facade.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +from __future__ import print_function import time SLEEP = 0.1 @@ -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 diff --git a/tests/test_decorator.py b/tests/test_decorator.py new file mode 100644 index 00000000..9f43e5b8 --- /dev/null +++ b/tests/test_decorator.py @@ -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(), + 'raw but not cruel') + + def test_bold(self): + self.assertEqual(BoldWrapper(self.raw_string).render(), + 'raw but not cruel') + + def test_mixed_bold_and_italic(self): + self.assertEqual( + BoldWrapper(ItalicWrapper(self.raw_string)).render(), + 'raw but not cruel') + diff --git a/tests/test_facade.py b/tests/test_facade.py new file mode 100644 index 00000000..5b32d03c --- /dev/null +++ b/tests/test_facade.py @@ -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) + + diff --git a/tests/test_lazy.py b/tests/test_lazy.py index 9e23aae6..4f70502c 100644 --- a/tests/test_lazy.py +++ b/tests/test_lazy.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import print_function import unittest from creational.lazy_evaluation import Person @@ -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__) diff --git a/tests/test_prototype.py b/tests/test_prototype.py new file mode 100644 index 00000000..4b896154 --- /dev/null +++ b/tests/test_prototype.py @@ -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) + diff --git a/tests/test_state.py b/tests/test_state.py index 8a5ec54a..1f5f8c8c 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -1,6 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import sys import unittest from behavioral.state import Radio