Skip to content

Commit bfe7af3

Browse files
authored
Merge pull request faif#316 from gyermolenko/add_more_doctests__
Add more doctests
2 parents 1f0f522 + d9cea61 commit bfe7af3

File tree

5 files changed

+105
-132
lines changed

5 files changed

+105
-132
lines changed

patterns/behavioral/publish_subscribe.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,43 @@ def run(self, msg):
5050

5151

5252
def main():
53-
message_center = Provider()
54-
55-
fftv = Publisher(message_center)
56-
57-
jim = Subscriber("jim", message_center)
58-
jim.subscribe("cartoon")
59-
jack = Subscriber("jack", message_center)
60-
jack.subscribe("music")
61-
gee = Subscriber("gee", message_center)
62-
gee.subscribe("movie")
63-
vani = Subscriber("vani", message_center)
64-
vani.subscribe("movie")
65-
vani.unsubscribe("movie")
66-
67-
fftv.publish("cartoon")
68-
fftv.publish("music")
69-
fftv.publish("ads")
70-
fftv.publish("movie")
71-
fftv.publish("cartoon")
72-
fftv.publish("cartoon")
73-
fftv.publish("movie")
74-
fftv.publish("blank")
75-
76-
message_center.update()
53+
"""
54+
>>> message_center = Provider()
55+
56+
>>> fftv = Publisher(message_center)
57+
58+
>>> jim = Subscriber("jim", message_center)
59+
>>> jim.subscribe("cartoon")
60+
>>> jack = Subscriber("jack", message_center)
61+
>>> jack.subscribe("music")
62+
>>> gee = Subscriber("gee", message_center)
63+
>>> gee.subscribe("movie")
64+
>>> vani = Subscriber("vani", message_center)
65+
>>> vani.subscribe("movie")
66+
>>> vani.unsubscribe("movie")
67+
68+
# Note that no one subscirbed to `ads`
69+
# and that vani changed their mind
70+
71+
>>> fftv.publish("cartoon")
72+
>>> fftv.publish("music")
73+
>>> fftv.publish("ads")
74+
>>> fftv.publish("movie")
75+
>>> fftv.publish("cartoon")
76+
>>> fftv.publish("cartoon")
77+
>>> fftv.publish("movie")
78+
>>> fftv.publish("blank")
79+
80+
>>> message_center.update()
81+
jim got cartoon
82+
jack got music
83+
gee got movie
84+
jim got cartoon
85+
jim got cartoon
86+
gee got movie
87+
"""
7788

7889

7990
if __name__ == "__main__":
80-
main()
81-
82-
83-
OUTPUT = """
84-
jim got cartoon
85-
jack got music
86-
gee got movie
87-
jim got cartoon
88-
jim got cartoon
89-
gee got movie
90-
"""
91+
import doctest
92+
doctest.testmod()

patterns/behavioral/specification.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,23 @@ def is_satisfied_by(self, candidate):
8888

8989

9090
def main():
91-
print('Specification')
92-
andrey = User()
93-
ivan = User(super_user=True)
94-
vasiliy = 'not User instance'
91+
"""
92+
>>> andrey = User()
93+
>>> ivan = User(super_user=True)
94+
>>> vasiliy = 'not User instance'
9595
96-
root_specification = UserSpecification().and_specification(SuperUserSpecification())
96+
>>> root_specification = UserSpecification().and_specification(SuperUserSpecification())
9797
98-
print(root_specification.is_satisfied_by(andrey))
99-
print(root_specification.is_satisfied_by(ivan))
100-
print(root_specification.is_satisfied_by(vasiliy))
98+
# Is specification satisfied by <name>
99+
>>> root_specification.is_satisfied_by(andrey), 'andrey'
100+
(False, 'andrey')
101+
>>> root_specification.is_satisfied_by(ivan), 'ivan'
102+
(True, 'ivan')
103+
>>> root_specification.is_satisfied_by(vasiliy), 'vasiliy'
104+
(False, 'vasiliy')
105+
"""
101106

102107

103108
if __name__ == '__main__':
104-
main()
105-
106-
107-
OUTPUT = """
108-
Specification
109-
False
110-
True
111-
False
112-
"""
109+
import doctest
110+
doctest.testmod()

patterns/behavioral/state.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,27 @@ def scan(self):
6262
self.state.scan()
6363

6464

65-
# Test our radio out
6665
def main():
67-
radio = Radio()
68-
actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
69-
actions *= 2
70-
71-
for action in actions:
72-
action()
66+
"""
67+
>>> radio = Radio()
68+
>>> actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
69+
>>> actions *= 2
70+
71+
>>> for action in actions:
72+
... action()
73+
Scanning... Station is 1380 AM
74+
Scanning... Station is 1510 AM
75+
Switching to FM
76+
Scanning... Station is 89.1 FM
77+
Scanning... Station is 103.9 FM
78+
Scanning... Station is 81.3 FM
79+
Scanning... Station is 89.1 FM
80+
Switching to AM
81+
Scanning... Station is 1250 AM
82+
Scanning... Station is 1380 AM
83+
"""
7384

7485

7586
if __name__ == '__main__':
76-
main()
77-
78-
79-
OUTPUT = """
80-
Scanning... Station is 1380 AM
81-
Scanning... Station is 1510 AM
82-
Switching to FM
83-
Scanning... Station is 89.1 FM
84-
Scanning... Station is 103.9 FM
85-
Scanning... Station is 81.3 FM
86-
Scanning... Station is 89.1 FM
87-
Switching to AM
88-
Scanning... Station is 1250 AM
89-
Scanning... Station is 1380 AM
90-
"""
87+
import doctest
88+
doctest.testmod()

patterns/creational/borg.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
added to the instance's attribute dictionary, but, since the attribute
2020
dictionary itself is shared (which is __shared_state), all other
2121
attributes will also be shared.
22-
For this reason, when the attribute self.state is modified using
23-
instance rm2, the value of self.state in instance rm1 also changes. The
24-
same happens if self.state is modified using rm3, which is an
25-
instance from a subclass.
26-
Notice that even though they share attributes, the instances are not
27-
the same, as seen by their ids.
2822
2923
*Where is the pattern used practically?
3024
Sharing state is useful in applications like managing database connections:
@@ -53,37 +47,44 @@ class YourBorg(Borg):
5347
pass
5448

5549

56-
if __name__ == '__main__':
57-
rm1 = Borg()
58-
rm2 = Borg()
50+
def main():
51+
"""
52+
>>> rm1 = Borg()
53+
>>> rm2 = Borg()
5954
60-
rm1.state = 'Idle'
61-
rm2.state = 'Running'
55+
>>> rm1.state = 'Idle'
56+
>>> rm2.state = 'Running'
6257
63-
print('rm1: {0}'.format(rm1))
64-
print('rm2: {0}'.format(rm2))
58+
>>> print('rm1: {0}'.format(rm1))
59+
rm1: Running
60+
>>> print('rm2: {0}'.format(rm2))
61+
rm2: Running
6562
66-
rm2.state = 'Zombie'
63+
# When the `state` attribute is modified from instance `rm2`,
64+
# the value of `state` in instance `rm1` also changes
65+
>>> rm2.state = 'Zombie'
6766
68-
print('rm1: {0}'.format(rm1))
69-
print('rm2: {0}'.format(rm2))
67+
>>> print('rm1: {0}'.format(rm1))
68+
rm1: Zombie
69+
>>> print('rm2: {0}'.format(rm2))
70+
rm2: Zombie
7071
71-
print('rm1 id: {0}'.format(id(rm1)))
72-
print('rm2 id: {0}'.format(id(rm2)))
72+
# Even though `rm1` and `rm2` share attributes, the instances are not the same
73+
>>> rm1 is rm2
74+
False
7375
74-
rm3 = YourBorg()
76+
# Shared state is also modified from a subclass instance `rm3`
77+
>>> rm3 = YourBorg()
7578
76-
print('rm1: {0}'.format(rm1))
77-
print('rm2: {0}'.format(rm2))
78-
print('rm3: {0}'.format(rm3))
79+
>>> print('rm1: {0}'.format(rm1))
80+
rm1: Init
81+
>>> print('rm2: {0}'.format(rm2))
82+
rm2: Init
83+
>>> print('rm3: {0}'.format(rm3))
84+
rm3: Init
85+
"""
7986

80-
### OUTPUT ###
81-
# rm1: Running
82-
# rm2: Running
83-
# rm1: Zombie
84-
# rm2: Zombie
85-
# rm1 id: 140732837899224
86-
# rm2 id: 140732837899296
87-
# rm1: Init
88-
# rm2: Init
89-
# rm3: Init
87+
88+
if __name__ == "__main__":
89+
import doctest
90+
doctest.testmod()

tests/test_outputs.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)