From 4a6aed773b24857969b1380d7d2492d1fb5a2bd3 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Wed, 1 May 2019 13:26:06 +0300 Subject: [PATCH 1/2] Doctest for observer --- patterns/behavioral/observer.py | 90 +++++++++++++++------------------ tests/test_outputs.py | 3 -- 2 files changed, 41 insertions(+), 52 deletions(-) diff --git a/patterns/behavioral/observer.py b/patterns/behavioral/observer.py index b93c0c4e9..b5990475f 100644 --- a/patterns/behavioral/observer.py +++ b/patterns/behavioral/observer.py @@ -64,52 +64,44 @@ def update(self, subject): # Example usage... def main(): - data1 = Data('Data 1') - data2 = Data('Data 2') - view1 = DecimalViewer() - view2 = HexViewer() - data1.attach(view1) - data1.attach(view2) - data2.attach(view2) - data2.attach(view1) - - print(u"Setting Data 1 = 10") - data1.data = 10 - print(u"Setting Data 2 = 15") - data2.data = 15 - print(u"Setting Data 1 = 3") - data1.data = 3 - print(u"Setting Data 2 = 5") - data2.data = 5 - print(u"Detach HexViewer from data1 and data2.") - data1.detach(view2) - data2.detach(view2) - print(u"Setting Data 1 = 10") - data1.data = 10 - print(u"Setting Data 2 = 15") - data2.data = 15 - - -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 -""" + """ + >>> data1 = Data('Data 1') + >>> data2 = Data('Data 2') + >>> view1 = DecimalViewer() + >>> view2 = HexViewer() + >>> data1.attach(view1) + >>> data1.attach(view2) + >>> data2.attach(view2) + >>> data2.attach(view1) + + >>> data1.data = 10 + DecimalViewer: Subject Data 1 has data 10 + HexViewer: Subject Data 1 has data 0xa + + >>> data2.data = 15 + HexViewer: Subject Data 2 has data 0xf + DecimalViewer: Subject Data 2 has data 15 + + >>> data1.data = 3 + DecimalViewer: Subject Data 1 has data 3 + HexViewer: Subject Data 1 has data 0x3 + + >>> data2.data = 5 + HexViewer: Subject Data 2 has data 0x5 + DecimalViewer: Subject Data 2 has data 5 + + # Detach HexViewer from data1 and data2 + >>> data1.detach(view2) + >>> data2.detach(view2) + + >>> data1.data = 10 + DecimalViewer: Subject Data 1 has data 10 + + >>> data2.data = 15 + DecimalViewer: Subject Data 2 has data 15 + """ + + +if __name__ == "__main__": + import doctest + doctest.testmod() diff --git a/tests/test_outputs.py b/tests/test_outputs.py index 5c3e0bcf4..7d9ff067e 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -8,8 +8,6 @@ import pytest -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 @@ -21,7 +19,6 @@ @pytest.mark.skipif(sys.version_info < (3,4), reason="requires python3.4 or higher") @pytest.mark.parametrize("main,output", [ - (observer_main, observer_output), (publish_subscribe_main, publish_subscribe_output), (specification_main, specification_output), (state_main, state_output), From 5aed472a9d2edae1b4b1c7c6630efc8c37682765 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Wed, 1 May 2019 14:25:53 +0300 Subject: [PATCH 2/2] Doctest for lazy_evaluation --- patterns/creational/lazy_evaluation.py | 63 +++++++++++++++----------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/patterns/creational/lazy_evaluation.py b/patterns/creational/lazy_evaluation.py index 62919a601..a2f17d58e 100644 --- a/patterns/creational/lazy_evaluation.py +++ b/patterns/creational/lazy_evaluation.py @@ -70,30 +70,39 @@ def parents(self): def main(): - Jhon = Person('Jhon', 'Coder') - print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation)) - print(u"Before we access `relatives`:") - print(Jhon.__dict__) - print(u"Jhon's relatives: {0}".format(Jhon.relatives)) - print(u"After we've accessed `relatives`:") - print(Jhon.__dict__) - print(Jhon.parents) - print(Jhon.__dict__) - print(Jhon.parents) - print(Jhon.call_count2) - - -if __name__ == '__main__': - main() - -### OUTPUT ### -# Name: Jhon Occupation: Coder -# Before we access `relatives`: -# {'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'} -# Jhon's relatives: Many relatives. -# After we've accessed `relatives`: -# {'relatives': 'Many relatives.', 'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'} -# Father and mother -# {'_lazy__parents': 'Father and mother', 'relatives': 'Many relatives.', 'call_count2': 1, 'name': 'Jhon', 'occupation': 'Coder'} # noqa flake8 -# Father and mother -# 1 + """ + >>> Jhon = Person('Jhon', 'Coder') + + >>> Jhon.name + 'Jhon' + >>> Jhon.occupation + 'Coder' + + # Before we access `relatives` + >>> sorted(Jhon.__dict__.items()) + [('call_count2', 0), ('name', 'Jhon'), ('occupation', 'Coder')] + + >>> Jhon.relatives + 'Many relatives.' + + # After we've accessed `relatives` + >>> sorted(Jhon.__dict__.items()) + [('call_count2', 0), ..., ('relatives', 'Many relatives.')] + + >>> Jhon.parents + 'Father and mother' + + >>> sorted(Jhon.__dict__.items()) + [('_lazy__parents', 'Father and mother'), ('call_count2', 1), ..., ('relatives', 'Many relatives.')] + + >>> Jhon.parents + 'Father and mother' + + >>> Jhon.call_count2 + 1 + """ + + +if __name__ == "__main__": + import doctest + doctest.testmod(optionflags=doctest.ELLIPSIS)