Skip to content

Commit 1417906

Browse files
authored
Merge pull request faif#296 from gyermolenko/more_doctests_
Doctests for Observer and lazy_evaluation
2 parents fc3d072 + 5aed472 commit 1417906

3 files changed

Lines changed: 77 additions & 79 deletions

File tree

patterns/behavioral/observer.py

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -64,52 +64,44 @@ def update(self, subject):
6464

6565
# Example usage...
6666
def main():
67-
data1 = Data('Data 1')
68-
data2 = Data('Data 2')
69-
view1 = DecimalViewer()
70-
view2 = HexViewer()
71-
data1.attach(view1)
72-
data1.attach(view2)
73-
data2.attach(view2)
74-
data2.attach(view1)
75-
76-
print(u"Setting Data 1 = 10")
77-
data1.data = 10
78-
print(u"Setting Data 2 = 15")
79-
data2.data = 15
80-
print(u"Setting Data 1 = 3")
81-
data1.data = 3
82-
print(u"Setting Data 2 = 5")
83-
data2.data = 5
84-
print(u"Detach HexViewer from data1 and data2.")
85-
data1.detach(view2)
86-
data2.detach(view2)
87-
print(u"Setting Data 1 = 10")
88-
data1.data = 10
89-
print(u"Setting Data 2 = 15")
90-
data2.data = 15
91-
92-
93-
if __name__ == '__main__':
94-
main()
95-
96-
97-
OUTPUT = """
98-
Setting Data 1 = 10
99-
DecimalViewer: Subject Data 1 has data 10
100-
HexViewer: Subject Data 1 has data 0xa
101-
Setting Data 2 = 15
102-
HexViewer: Subject Data 2 has data 0xf
103-
DecimalViewer: Subject Data 2 has data 15
104-
Setting Data 1 = 3
105-
DecimalViewer: Subject Data 1 has data 3
106-
HexViewer: Subject Data 1 has data 0x3
107-
Setting Data 2 = 5
108-
HexViewer: Subject Data 2 has data 0x5
109-
DecimalViewer: Subject Data 2 has data 5
110-
Detach HexViewer from data1 and data2.
111-
Setting Data 1 = 10
112-
DecimalViewer: Subject Data 1 has data 10
113-
Setting Data 2 = 15
114-
DecimalViewer: Subject Data 2 has data 15
115-
"""
67+
"""
68+
>>> data1 = Data('Data 1')
69+
>>> data2 = Data('Data 2')
70+
>>> view1 = DecimalViewer()
71+
>>> view2 = HexViewer()
72+
>>> data1.attach(view1)
73+
>>> data1.attach(view2)
74+
>>> data2.attach(view2)
75+
>>> data2.attach(view1)
76+
77+
>>> data1.data = 10
78+
DecimalViewer: Subject Data 1 has data 10
79+
HexViewer: Subject Data 1 has data 0xa
80+
81+
>>> data2.data = 15
82+
HexViewer: Subject Data 2 has data 0xf
83+
DecimalViewer: Subject Data 2 has data 15
84+
85+
>>> data1.data = 3
86+
DecimalViewer: Subject Data 1 has data 3
87+
HexViewer: Subject Data 1 has data 0x3
88+
89+
>>> data2.data = 5
90+
HexViewer: Subject Data 2 has data 0x5
91+
DecimalViewer: Subject Data 2 has data 5
92+
93+
# Detach HexViewer from data1 and data2
94+
>>> data1.detach(view2)
95+
>>> data2.detach(view2)
96+
97+
>>> data1.data = 10
98+
DecimalViewer: Subject Data 1 has data 10
99+
100+
>>> data2.data = 15
101+
DecimalViewer: Subject Data 2 has data 15
102+
"""
103+
104+
105+
if __name__ == "__main__":
106+
import doctest
107+
doctest.testmod()

patterns/creational/lazy_evaluation.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,39 @@ def parents(self):
7070

7171

7272
def main():
73-
Jhon = Person('Jhon', 'Coder')
74-
print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
75-
print(u"Before we access `relatives`:")
76-
print(Jhon.__dict__)
77-
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
78-
print(u"After we've accessed `relatives`:")
79-
print(Jhon.__dict__)
80-
print(Jhon.parents)
81-
print(Jhon.__dict__)
82-
print(Jhon.parents)
83-
print(Jhon.call_count2)
84-
85-
86-
if __name__ == '__main__':
87-
main()
88-
89-
### OUTPUT ###
90-
# Name: Jhon Occupation: Coder
91-
# Before we access `relatives`:
92-
# {'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
93-
# Jhon's relatives: Many relatives.
94-
# After we've accessed `relatives`:
95-
# {'relatives': 'Many relatives.', 'call_count2': 0, 'name': 'Jhon', 'occupation': 'Coder'}
96-
# Father and mother
97-
# {'_lazy__parents': 'Father and mother', 'relatives': 'Many relatives.', 'call_count2': 1, 'name': 'Jhon', 'occupation': 'Coder'} # noqa flake8
98-
# Father and mother
99-
# 1
73+
"""
74+
>>> Jhon = Person('Jhon', 'Coder')
75+
76+
>>> Jhon.name
77+
'Jhon'
78+
>>> Jhon.occupation
79+
'Coder'
80+
81+
# Before we access `relatives`
82+
>>> sorted(Jhon.__dict__.items())
83+
[('call_count2', 0), ('name', 'Jhon'), ('occupation', 'Coder')]
84+
85+
>>> Jhon.relatives
86+
'Many relatives.'
87+
88+
# After we've accessed `relatives`
89+
>>> sorted(Jhon.__dict__.items())
90+
[('call_count2', 0), ..., ('relatives', 'Many relatives.')]
91+
92+
>>> Jhon.parents
93+
'Father and mother'
94+
95+
>>> sorted(Jhon.__dict__.items())
96+
[('_lazy__parents', 'Father and mother'), ('call_count2', 1), ..., ('relatives', 'Many relatives.')]
97+
98+
>>> Jhon.parents
99+
'Father and mother'
100+
101+
>>> Jhon.call_count2
102+
1
103+
"""
104+
105+
106+
if __name__ == "__main__":
107+
import doctest
108+
doctest.testmod(optionflags=doctest.ELLIPSIS)

tests/test_outputs.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import pytest
1010

11-
from patterns.behavioral.observer import main as observer_main
12-
from patterns.behavioral.observer import OUTPUT as observer_output
1311
from patterns.behavioral.publish_subscribe import main as publish_subscribe_main
1412
from patterns.behavioral.publish_subscribe import OUTPUT as publish_subscribe_output
1513
from patterns.behavioral.specification import main as specification_main
@@ -21,7 +19,6 @@
2119
@pytest.mark.skipif(sys.version_info < (3,4),
2220
reason="requires python3.4 or higher")
2321
@pytest.mark.parametrize("main,output", [
24-
(observer_main, observer_output),
2522
(publish_subscribe_main, publish_subscribe_output),
2623
(specification_main, specification_output),
2724
(state_main, state_output),

0 commit comments

Comments
 (0)