Skip to content

Commit f097a2d

Browse files
authored
Merge pull request faif#317 from gyermolenko/more_doctests_v5
More doctests
2 parents bfe7af3 + b62a0a8 commit f097a2d

File tree

4 files changed

+58
-45
lines changed

4 files changed

+58
-45
lines changed

patterns/creational/builder.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,23 @@ def construct_building(cls):
9191
return building
9292

9393

94-
# Client
95-
if __name__ == "__main__":
96-
house = House()
97-
print(house)
98-
flat = Flat()
99-
print(flat)
94+
def main():
95+
"""
96+
>>> house = House()
97+
>>> house
98+
Floor: One | Size: Big
99+
100+
>>> flat = Flat()
101+
>>> flat
102+
Floor: More than One | Size: Small
100103
101104
# Using an external constructor function:
102-
complex_house = construct_building(ComplexHouse)
103-
print(complex_house)
105+
>>> complex_house = construct_building(ComplexHouse)
106+
>>> complex_house
107+
Floor: One | Size: Big and fancy
108+
"""
104109

105-
### OUTPUT ###
106-
# Floor: One | Size: Big
107-
# Floor: More than One | Size: Small
108-
# Floor: One | Size: Big and fancy
110+
111+
if __name__ == "__main__":
112+
import doctest
113+
doctest.testmod()

patterns/creational/prototype.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,22 @@ def unregister_object(self, name):
5252

5353

5454
def main():
55-
dispatcher = PrototypeDispatcher()
56-
prototype = Prototype()
55+
"""
56+
>>> dispatcher = PrototypeDispatcher()
57+
>>> prototype = Prototype()
5758
58-
d = prototype.clone()
59-
a = prototype.clone(value='a-value', category='a')
60-
b = prototype.clone(value='b-value', is_checked=True)
61-
dispatcher.register_object('objecta', a)
62-
dispatcher.register_object('objectb', b)
63-
dispatcher.register_object('default', d)
64-
print([{n: p.value} for n, p in dispatcher.get_objects().items()])
59+
>>> d = prototype.clone()
60+
>>> a = prototype.clone(value='a-value', category='a')
61+
>>> b = prototype.clone(value='b-value', is_checked=True)
62+
>>> dispatcher.register_object('objecta', a)
63+
>>> dispatcher.register_object('objectb', b)
64+
>>> dispatcher.register_object('default', d)
6565
66+
>>> [{n: p.value} for n, p in dispatcher.get_objects().items()]
67+
[{'objecta': 'a-value'}, {'objectb': 'b-value'}, {'default': 'default'}]
68+
"""
6669

67-
if __name__ == '__main__':
68-
main()
6970

70-
### OUTPUT ###
71-
# [{'objectb': 'b-value'}, {'default': 'default'}, {'objecta': 'a-value'}]
71+
if __name__ == '__main__':
72+
import doctest
73+
doctest.testmod()

patterns/other/graph_search.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ def find_shortest_path(self, start, end, path=None):
4646
return shortest
4747

4848

49-
# example of graph usage
50-
graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}
49+
def main():
50+
"""
51+
# example of graph usage
52+
>>> graph = {'A': ['B', 'C'], 'B': ['C', 'D'], 'C': ['D'], 'D': ['C'], 'E': ['F'], 'F': ['C']}
5153
52-
# initialization of new graph search object
53-
graph1 = GraphSearch(graph)
54+
# initialization of new graph search object
55+
>>> graph1 = GraphSearch(graph)
5456
57+
>>> print(graph1.find_path('A', 'D'))
58+
['A', 'B', 'C', 'D']
59+
>>> print(graph1.find_all_path('A', 'D'))
60+
[['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']]
61+
>>> print(graph1.find_shortest_path('A', 'D'))
62+
['A', 'B', 'D']
63+
"""
5564

56-
print(graph1.find_path('A', 'D'))
57-
print(graph1.find_all_path('A', 'D'))
58-
print(graph1.find_shortest_path('A', 'D'))
5965

60-
### OUTPUT ###
61-
# ['A', 'B', 'C', 'D']
62-
# [['A', 'B', 'C', 'D'], ['A', 'B', 'D'], ['A', 'C', 'D']]
63-
# ['A', 'B', 'D']
66+
if __name__ == "__main__":
67+
import doctest
68+
doctest.testmod()

patterns/structural/bridge.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ def scale(self, pct):
3737

3838

3939
def main():
40-
shapes = (CircleShape(1, 2, 3, DrawingAPI1()), CircleShape(5, 7, 11, DrawingAPI2()))
40+
"""
41+
>>> shapes = (CircleShape(1, 2, 3, DrawingAPI1()), CircleShape(5, 7, 11, DrawingAPI2()))
4142
42-
for shape in shapes:
43-
shape.scale(2.5)
44-
shape.draw()
43+
>>> for shape in shapes:
44+
... shape.scale(2.5)
45+
... shape.draw()
46+
API1.circle at 1:2 radius 7.5
47+
API2.circle at 5:7 radius 27.5
48+
"""
4549

4650

4751
if __name__ == '__main__':
48-
main()
49-
50-
### OUTPUT ###
51-
# API1.circle at 1:2 radius 7.5
52-
# API2.circle at 5:7 radius 27.5
52+
import doctest
53+
doctest.testmod()

0 commit comments

Comments
 (0)