From 8ff7ae422b341890f66581f2674034fd379a2ab4 Mon Sep 17 00:00:00 2001 From: Alan Tan Date: Fri, 3 Jul 2020 15:09:10 +0800 Subject: [PATCH 1/5] Add doctest for pool --- patterns/creational/pool.py | 42 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/patterns/creational/pool.py b/patterns/creational/pool.py index a58ff8d89..69d229323 100644 --- a/patterns/creational/pool.py +++ b/patterns/creational/pool.py @@ -51,32 +51,34 @@ def __del__(self): def main(): - import queue + """ + >>> import queue - def test_object(queue): - pool = ObjectPool(queue, True) - print('Inside func: {}'.format(pool.item)) + >>> def test_object(queue): + ... pool = ObjectPool(queue, True) + ... print('Inside func: {}'.format(pool.item)) - sample_queue = queue.Queue() + >>> sample_queue = queue.Queue() - sample_queue.put('yam') - with ObjectPool(sample_queue) as obj: - print('Inside with: {}'.format(obj)) - print('Outside with: {}'.format(sample_queue.get())) + >>> sample_queue.put('yam') + >>> with ObjectPool(sample_queue) as obj: + ... print('Inside with: {}'.format(obj)) + Inside with: yam - sample_queue.put('sam') - test_object(sample_queue) - print('Outside func: {}'.format(sample_queue.get())) + >>> print('Outside with: {}'.format(sample_queue.get())) + Outside with: yam + + >>> sample_queue.put('sam') + >>> test_object(sample_queue) + Inside func: sam + + >>> print('Outside func: {}'.format(sample_queue.get())) + Outside func: sam if not sample_queue.empty(): print(sample_queue.get()) - + """ if __name__ == '__main__': - main() - -### OUTPUT ### -# Inside with: yam -# Outside with: yam -# Inside func: sam -# Outside func: sam + import doctest + doctest.testmod() From 76b6c24ad8d7d5aaf0963694f022344cb0f922dc Mon Sep 17 00:00:00 2001 From: Alan Tan Date: Fri, 3 Jul 2020 15:18:37 +0800 Subject: [PATCH 2/5] Add doctest for 3-tier --- patterns/structural/3-tier.py | 58 +++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/patterns/structural/3-tier.py b/patterns/structural/3-tier.py index 5497730c0..b6832cc53 100644 --- a/patterns/structural/3-tier.py +++ b/patterns/structural/3-tier.py @@ -61,32 +61,36 @@ def get_product_information(self, product: str) -> None: def main(): - ui = Ui() - ui.get_product_list() - ui.get_product_information("cheese") - ui.get_product_information("eggs") - ui.get_product_information("milk") - ui.get_product_information("arepas") - + """ + >>> ui = Ui() + >>> ui.get_product_list() + PRODUCT LIST: + (Fetching from Data Store) + milk + eggs + cheese + + + >>> ui.get_product_information("cheese") + (Fetching from Data Store) + PRODUCT INFORMATION: + Name: Cheese, Price: 2.00, Quantity: 10 + + >>> ui.get_product_information("eggs") + (Fetching from Data Store) + PRODUCT INFORMATION: + Name: Eggs, Price: 0.20, Quantity: 100 + + >>> ui.get_product_information("milk") + (Fetching from Data Store) + PRODUCT INFORMATION: + Name: Milk, Price: 1.50, Quantity: 10 + + >>> ui.get_product_information("arepas") + (Fetching from Data Store) + That product 'arepas' does not exist in the records + """ if __name__ == "__main__": - main() - -### OUTPUT ### -# PRODUCT LIST: -# (Fetching from Data Store) -# cheese -# eggs -# milk -# -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Cheese, Price: 2.00, Quantity: 10 -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Eggs, Price: 0.20, Quantity: 100 -# (Fetching from Data Store) -# PRODUCT INFORMATION: -# Name: Milk, Price: 1.50, Quantity: 10 -# (Fetching from Data Store) -# That product "arepas" does not exist in the records + import doctest + doctest.testmod() From 01fc93221e2c82d018e00e87ff12d65161b80d07 Mon Sep 17 00:00:00 2001 From: Alan Tan Date: Fri, 3 Jul 2020 15:22:18 +0800 Subject: [PATCH 3/5] Add doctest for decorator --- patterns/structural/decorator.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/patterns/structural/decorator.py b/patterns/structural/decorator.py index b94c05273..e64b8f8f6 100644 --- a/patterns/structural/decorator.py +++ b/patterns/structural/decorator.py @@ -55,12 +55,18 @@ def render(self): return "{}".format(self._wrapped.render()) +def main(): + """ + >>> simple_hello = TextTag("hello, world!") + >>> special_hello = ItalicWrapper(BoldWrapper(simple_hello)) + + >>> print("before:", simple_hello.render()) + before: hello, world! + + >>> print("after:", special_hello.render()) + after: hello, world! + """ + if __name__ == '__main__': - simple_hello = TextTag("hello, world!") - special_hello = ItalicWrapper(BoldWrapper(simple_hello)) - print("before:", simple_hello.render()) - print("after:", special_hello.render()) - -### OUTPUT ### -# before: hello, world! -# after: hello, world! + import doctest + doctest.testmod() From d730dd0873ff3b3491adab9b3dbd39eac5a18b16 Mon Sep 17 00:00:00 2001 From: Alan Tan Date: Fri, 3 Jul 2020 15:28:20 +0800 Subject: [PATCH 4/5] Add doctest for front_controller --- patterns/structural/front_controller.py | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/patterns/structural/front_controller.py b/patterns/structural/front_controller.py index 346392e4e..4aa30a787 100644 --- a/patterns/structural/front_controller.py +++ b/patterns/structural/front_controller.py @@ -58,17 +58,23 @@ def __init__(self, request): self.type = self.tablet_type -if __name__ == '__main__': - front_controller = RequestController() - front_controller.dispatch_request(Request('mobile')) - front_controller.dispatch_request(Request('tablet')) - - front_controller.dispatch_request(Request('desktop')) - front_controller.dispatch_request('mobile') - - -### OUTPUT ### -# Displaying mobile index page -# Displaying tablet index page -# cant dispatch the request -# request must be a Request object +def main(): + """ + >>> front_controller = RequestController() + + >>> front_controller.dispatch_request(Request('mobile')) + Displaying mobile index page + + >>> front_controller.dispatch_request(Request('tablet')) + Displaying tablet index page + + >>> front_controller.dispatch_request(Request('desktop')) + cant dispatch the request + + >>> front_controller.dispatch_request('mobile') + request must be a Request object + """ + +if __name__ == "__main__": + import doctest + doctest.testmod() From a77c79c17eaffc0dcfe1ad3a98fa34e24275c25a Mon Sep 17 00:00:00 2001 From: Alan Tan Date: Fri, 3 Jul 2020 15:35:46 +0800 Subject: [PATCH 5/5] Add doctest for mvc --- patterns/structural/mvc.py | 63 +++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/patterns/structural/mvc.py b/patterns/structural/mvc.py index 75d3c9e0e..a0e2c7aa7 100644 --- a/patterns/structural/mvc.py +++ b/patterns/structural/mvc.py @@ -110,31 +110,38 @@ def show_item_information(self, item_name): self.view.show_item_information(item_type, item_name, item_info) -if __name__ == "__main__": - - model = ProductModel() - view = ConsoleView() - controller = Controller(model, view) - controller.show_items() - controller.show_item_information("cheese") - controller.show_item_information("eggs") - controller.show_item_information("milk") - controller.show_item_information("arepas") - - -### OUTPUT ### -# PRODUCT LIST: -# cheese -# eggs -# milk -# -# PRODUCT INFORMATION: -# Name: Cheese, Price: 2.00, Quantity: 10 -# -# PRODUCT INFORMATION: -# Name: Eggs, Price: 0.20, Quantity: 100 -# -# PRODUCT INFORMATION: -# Name: Milk, Price: 1.50, Quantity: 10 -# -# That product "arepas" does not exist in the records +def main(): + """ + >>> model = ProductModel() + >>> view = ConsoleView() + >>> controller = Controller(model, view) + + >>> controller.show_items() + PRODUCT LIST: + milk + eggs + cheese + + + >>> controller.show_item_information("cheese") + PRODUCT INFORMATION: + Name: cheese, Price: 2.00, Quantity: 10 + + + >>> controller.show_item_information("eggs") + PRODUCT INFORMATION: + Name: eggs, Price: 0.20, Quantity: 100 + + + >>> controller.show_item_information("milk") + PRODUCT INFORMATION: + Name: milk, Price: 1.50, Quantity: 10 + + + >>> controller.show_item_information("arepas") + That product "arepas" does not exist in the records + """ + +if __name__ == '__main__': + import doctest + doctest.testmod()