From ce84871b504ad5576ade03dc3ce47eeae98d2b9f Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Wed, 16 Jan 2019 10:43:55 +0200 Subject: [PATCH] Add main function and tests for output for most of behavioral patterns --- patterns/behavioral/catalog.py | 13 ++--- patterns/behavioral/chain.py | 28 ++++++----- patterns/behavioral/chaining_method.py | 13 +++-- patterns/behavioral/command.py | 12 +++-- patterns/behavioral/iterator.py | 34 +++++++++----- patterns/behavioral/mediator.py | 45 ++++++++++-------- patterns/behavioral/memento.py | 60 +++++++++++++----------- patterns/behavioral/observer.py | 38 ++++++++------- patterns/behavioral/publish_subscribe.py | 16 ++++--- patterns/behavioral/specification.py | 17 ++++--- patterns/behavioral/state.py | 30 +++++++----- patterns/behavioral/template.py | 36 ++++++++------ tests/test_outputs.py | 37 +++++++++++++-- 13 files changed, 232 insertions(+), 147 deletions(-) diff --git a/patterns/behavioral/catalog.py b/patterns/behavioral/catalog.py index 35dc69a10..476b0eca1 100644 --- a/patterns/behavioral/catalog.py +++ b/patterns/behavioral/catalog.py @@ -166,11 +166,12 @@ def main(): if __name__ == "__main__": - main() -### OUTPUT ### -# executed method 2! -# Value x1 -# Value x2 -# executed method 1! + +OUTPUT = """ +executed method 2! +Value x1 +Value x2 +executed method 1! +""" diff --git a/patterns/behavioral/chain.py b/patterns/behavioral/chain.py index 24324eea8..8e374d5ec 100644 --- a/patterns/behavioral/chain.py +++ b/patterns/behavioral/chain.py @@ -91,7 +91,7 @@ def check_range(request): return False -if __name__ == "__main__": +def main(): h0 = ConcreteHandler0() h1 = ConcreteHandler1() h2 = ConcreteHandler2(FallbackHandler()) @@ -102,13 +102,19 @@ def check_range(request): for request in requests: h0.handle(request) -### OUTPUT ### -# request 2 handled in handler 0 -# request 5 handled in handler 0 -# request 14 handled in handler 1 -# request 22 handled in handler 2 -# request 18 handled in handler 1 -# request 3 handled in handler 0 -# end of chain, no handler for 35 -# request 27 handled in handler 2 -# request 20 handled in handler 2 + +if __name__ == "__main__": + main() + + +OUTPUT = """ +request 2 handled in handler 0 +request 5 handled in handler 0 +request 14 handled in handler 1 +request 22 handled in handler 2 +request 18 handled in handler 1 +request 3 handled in handler 0 +end of chain, no handler for 35 +request 27 handled in handler 2 +request 20 handled in handler 2 +""" diff --git a/patterns/behavioral/chaining_method.py b/patterns/behavioral/chaining_method.py index 11be01ff4..3a2f397d8 100644 --- a/patterns/behavioral/chaining_method.py +++ b/patterns/behavioral/chaining_method.py @@ -26,11 +26,16 @@ def stop(self): print('then stop') -if __name__ == '__main__': - +def main(): move = Action('move') person = Person('Jack', move) person.do_action().amount('5m').stop() -### OUTPUT ### -# Jack move 5m then stop + +if __name__ == '__main__': + main() + + +OUTPUT = """ +Jack move 5m then stop +""" diff --git a/patterns/behavioral/command.py b/patterns/behavioral/command.py index 23f73b30e..17765e52a 100644 --- a/patterns/behavioral/command.py +++ b/patterns/behavioral/command.py @@ -59,8 +59,10 @@ def main(): if __name__ == "__main__": main() -### OUTPUT ### -# renaming foo.txt to bar.txt -# renaming bar.txt to baz.txt -# renaming baz.txt to bar.txt -# renaming bar.txt to foo.txt + +OUTPUT = """ +renaming foo.txt to bar.txt +renaming bar.txt to baz.txt +renaming baz.txt to bar.txt +renaming bar.txt to foo.txt +""" diff --git a/patterns/behavioral/iterator.py b/patterns/behavioral/iterator.py index 2ee6f98bc..49f8e5697 100644 --- a/patterns/behavioral/iterator.py +++ b/patterns/behavioral/iterator.py @@ -23,20 +23,28 @@ def count_to(count): count_to_two = lambda: count_to(2) count_to_five = lambda: count_to(5) -print('Counting to two...') -for number in count_to_two(): - print(number, end=' ') -print() +def main(): + print('Counting to two...') + for number in count_to_two(): + print(number, end=' ') -print('Counting to five...') -for number in count_to_five(): - print(number, end=' ') + print() -print() + print('Counting to five...') + for number in count_to_five(): + print(number, end=' ') + + print() -### OUTPUT ### -# Counting to two... -# one two -# Counting to five... -# one two three four five + +if __name__ == "__main__": + main() + + +OUTPUT = """ +Counting to two... +one two +Counting to five... +one two three four five +""" diff --git a/patterns/behavioral/mediator.py b/patterns/behavioral/mediator.py index c3f8f5bbb..db23b7bba 100644 --- a/patterns/behavioral/mediator.py +++ b/patterns/behavioral/mediator.py @@ -105,7 +105,7 @@ def setTC(self, tc): self._tc = tc -if __name__ == '__main__': +def main(): reporter = Reporter() db = DB() tm = TestManager() @@ -124,21 +124,28 @@ def setTC(self, tc): tc.execute() tc.tearDown() -### OUTPUT ### -# Setting up the Test -# Inserting the execution begin status in the Database -# Executing the test -# Tearing down -# Updating the test results in Database -# Reporting the results of Test -# Setting up the Test -# Inserting the execution begin status in the Database -# Reporter Class is preparing to report the results -# Problem in setup. Test not executed. -# Test not executed. No tear down required. -# Setting up the Test -# Inserting the execution begin status in the Database -# Executing the test -# Tearing down -# Updating the test results in Database -# Reporting the results of Test + +if __name__ == '__main__': + main() + + +OUTPUT = """ +Setting up the Test +Inserting the execution begin status in the Database +Executing the test +Tearing down +Updating the test results in Database +Reporting the results of Test +Setting up the Test +Inserting the execution begin status in the Database +Executing the test +Tearing down +Updating the test results in Database +Reporting the results of Test +Setting up the Test +Inserting the execution begin status in the Database +Executing the test +Tearing down +Updating the test results in Database +Reporting the results of Test +""" diff --git a/patterns/behavioral/memento.py b/patterns/behavioral/memento.py index 59757c28e..059dce421 100644 --- a/patterns/behavioral/memento.py +++ b/patterns/behavioral/memento.py @@ -81,7 +81,7 @@ def do_stuff(self): self.increment() # <- will fail and rollback -if __name__ == '__main__': +def main(): num_obj = NumObj(-1) print(num_obj) @@ -114,30 +114,34 @@ def do_stuff(self): traceback.print_exc(file=sys.stdout) print(num_obj) - -### OUTPUT ### -# -# -# -# -# -- committed -# -# -# -# -- rolled back -# -# -- now doing stuff ... -# -> doing stuff failed! -# Traceback (most recent call last): -# File "memento.py", line 97, in -# num_obj.do_stuff() -# File "memento.py", line 52, in transaction -# raise e -# File "memento.py", line 49, in transaction -# return self.method(obj, *args, **kwargs) -# File "memento.py", line 70, in do_stuff -# self.increment() # <- will fail and rollback -# File "memento.py", line 65, in increment -# self.value += 1 -# TypeError: Can't convert 'int' object to str implicitly -# +if __name__ == '__main__': + main() + + +OUTPUT = """ + + + + +-- committed + + + +-- rolled back + +-- now doing stuff ... +-> doing stuff failed! +Traceback (most recent call last): + File "patterns/behavioral/memento.py", line 108, in main + num_obj.do_stuff() + File "patterns/behavioral/memento.py", line 63, in transaction + raise e + File "patterns/behavioral/memento.py", line 60, in transaction + return self.method(obj, *args, **kwargs) + File "patterns/behavioral/memento.py", line 81, in do_stuff + self.increment() # <- will fail and rollback + File "patterns/behavioral/memento.py", line 76, in increment + self.value += 1 +TypeError: can only concatenate str (not "int") to str + +""" diff --git a/patterns/behavioral/observer.py b/patterns/behavioral/observer.py index e93e1c514..b93c0c4e9 100644 --- a/patterns/behavioral/observer.py +++ b/patterns/behavioral/observer.py @@ -93,21 +93,23 @@ def main(): if __name__ == '__main__': main() -### OUTPUT ### -# Setting Data 1 = 10 -# DecimalViewer: Subject Data 1 has data 10 -# HexViewer: Subject Data 1 has data 0xa -# Setting Data 2 = 15 -# HexViewer: Subject Data 2 has data 0xf -# DecimalViewer: Subject Data 2 has data 15 -# Setting Data 1 = 3 -# DecimalViewer: Subject Data 1 has data 3 -# HexViewer: Subject Data 1 has data 0x3 -# Setting Data 2 = 5 -# HexViewer: Subject Data 2 has data 0x5 -# DecimalViewer: Subject Data 2 has data 5 -# Detach HexViewer from data1 and data2. -# Setting Data 1 = 10 -# DecimalViewer: Subject Data 1 has data 10 -# Setting Data 2 = 15 -# DecimalViewer: Subject Data 2 has data 15 + +OUTPUT = """ +Setting Data 1 = 10 +DecimalViewer: Subject Data 1 has data 10 +HexViewer: Subject Data 1 has data 0xa +Setting Data 2 = 15 +HexViewer: Subject Data 2 has data 0xf +DecimalViewer: Subject Data 2 has data 15 +Setting Data 1 = 3 +DecimalViewer: Subject Data 1 has data 3 +HexViewer: Subject Data 1 has data 0x3 +Setting Data 2 = 5 +HexViewer: Subject Data 2 has data 0x5 +DecimalViewer: Subject Data 2 has data 5 +Detach HexViewer from data1 and data2. +Setting Data 1 = 10 +DecimalViewer: Subject Data 1 has data 10 +Setting Data 2 = 15 +DecimalViewer: Subject Data 2 has data 15 +""" diff --git a/patterns/behavioral/publish_subscribe.py b/patterns/behavioral/publish_subscribe.py index 514bcffaf..131181e08 100644 --- a/patterns/behavioral/publish_subscribe.py +++ b/patterns/behavioral/publish_subscribe.py @@ -81,10 +81,12 @@ def main(): if __name__ == "__main__": main() -### OUTPUT ### -# jim got cartoon -# jack got music -# gee got movie -# jim got cartoon -# jim got cartoon -# gee got movie + +OUTPUT = """ +jim got cartoon +jack got music +gee got movie +jim got cartoon +jim got cartoon +gee got movie +""" diff --git a/patterns/behavioral/specification.py b/patterns/behavioral/specification.py index 81fb5728b..6198af1f4 100644 --- a/patterns/behavioral/specification.py +++ b/patterns/behavioral/specification.py @@ -90,7 +90,7 @@ def is_satisfied_by(self, candidate): return getattr(candidate, 'super_user', False) -if __name__ == '__main__': +def main(): print('Specification') andrey = User() ivan = User(super_user=True) @@ -102,9 +102,14 @@ def is_satisfied_by(self, candidate): print(root_specification.is_satisfied_by(ivan)) print(root_specification.is_satisfied_by(vasiliy)) + +if __name__ == '__main__': + main() + -### OUTPUT ### -# Specification -# False -# True -# False +OUTPUT = """ +Specification +False +True +False +""" diff --git a/patterns/behavioral/state.py b/patterns/behavioral/state.py index e122463d9..0d567093d 100644 --- a/patterns/behavioral/state.py +++ b/patterns/behavioral/state.py @@ -68,7 +68,7 @@ def scan(self): # Test our radio out -if __name__ == '__main__': +def main(): radio = Radio() actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2 actions *= 2 @@ -76,14 +76,20 @@ def scan(self): for action in actions: action() -### OUTPUT ### -# Scanning... Station is 1380 AM -# Scanning... Station is 1510 AM -# Switching to FM -# Scanning... Station is 89.1 FM -# Scanning... Station is 103.9 FM -# Scanning... Station is 81.3 FM -# Scanning... Station is 89.1 FM -# Switching to AM -# Scanning... Station is 1250 AM -# Scanning... Station is 1380 AM + +if __name__ == '__main__': + main() + + +OUTPUT = """ +Scanning... Station is 1380 AM +Scanning... Station is 1510 AM +Switching to FM +Scanning... Station is 89.1 FM +Scanning... Station is 103.9 FM +Scanning... Station is 81.3 FM +Scanning... Station is 89.1 FM +Switching to AM +Scanning... Station is 1250 AM +Scanning... Station is 1380 AM +""" diff --git a/patterns/behavioral/template.py b/patterns/behavioral/template.py index c9d4eb269..61c581ce7 100644 --- a/patterns/behavioral/template.py +++ b/patterns/behavioral/template.py @@ -49,24 +49,30 @@ def template_function(getter, converter=False, to_save=False): print("`{}` was processed".format(data)) -if __name__ == "__main__": +def main(): template_function(get_text, to_save=True) print("-" * 30) template_function(get_pdf, converter=convert_to_text) print("-" * 30) template_function(get_csv, to_save=True) -### OUTPUT ### -# Got `plain-text` -# Skip conversion -# [SAVE] -# `plain-text` was processed -# ------------------------------ -# Got `pdf` -# [CONVERT] -# `pdf as text` was processed -# ------------------------------ -# Got `csv` -# Skip conversion -# [SAVE] -# `csv` was processed + +if __name__ == "__main__": + main() + + +OUTPUT = """ +Got `plain-text` +Skip conversion +[SAVE] +`plain-text` was processed +------------------------------ +Got `pdf` +[CONVERT] +`pdf as text` was processed +------------------------------ +Got `csv` +Skip conversion +[SAVE] +`csv` was processed +""" diff --git a/tests/test_outputs.py b/tests/test_outputs.py index c68b7120d..d68ea51d6 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -8,16 +8,47 @@ import pytest -from patterns.behavioral.visitor import main as visitor_main -from patterns.behavioral.visitor import OUTPUT as visitor_output +from patterns.behavioral.catalog import main as catalog_main +from patterns.behavioral.catalog import OUTPUT as catalog_output +from patterns.behavioral.chain import main as chain_main +from patterns.behavioral.chain import OUTPUT as chain_output +from patterns.behavioral.chaining_method import main as chaining_method_main +from patterns.behavioral.chaining_method import OUTPUT as chaining_method_output +from patterns.behavioral.command import main as command_main +from patterns.behavioral.command import OUTPUT as command_output +from patterns.behavioral.iterator import main as iterator_main +from patterns.behavioral.iterator import OUTPUT as iterator_output +from patterns.behavioral.observer import main as observer_main +from patterns.behavioral.observer import OUTPUT as observer_output +from patterns.behavioral.publish_subscribe import main as publish_subscribe_main +from patterns.behavioral.publish_subscribe import OUTPUT as publish_subscribe_output +from patterns.behavioral.specification import main as specification_main +from patterns.behavioral.specification import OUTPUT as specification_output +from patterns.behavioral.state import main as state_main +from patterns.behavioral.state import OUTPUT as state_output from patterns.behavioral.strategy import main as strategy_main from patterns.behavioral.strategy import OUTPUT as strategy_output +from patterns.behavioral.template import main as template_main +from patterns.behavioral.template import OUTPUT as template_output +from patterns.behavioral.visitor import main as visitor_main +from patterns.behavioral.visitor import OUTPUT as visitor_output + @pytest.mark.skipif(sys.version_info < (3,4), reason="requires python3.4 or higher") @pytest.mark.parametrize("main,output", [ - (visitor_main, visitor_output), + (catalog_main, catalog_output), + (chain_main, chain_output), + (chaining_method_main, chaining_method_output), + (command_main, command_output), + (iterator_main, iterator_output), + (observer_main, observer_output), + (publish_subscribe_main, publish_subscribe_output), + (specification_main, specification_output), + (state_main, state_output), (strategy_main, strategy_output), + (template_main, template_output), + (visitor_main, visitor_output), ]) def test_output(main, output): f = io.StringIO()