Skip to content

Commit e0e9bfc

Browse files
authored
Merge pull request faif#283 from gyermolenko/doctests_all_around
Doctests all around
2 parents 995cb42 + da85a02 commit e0e9bfc

4 files changed

Lines changed: 103 additions & 135 deletions

File tree

patterns/behavioral/memento.py

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -82,67 +82,59 @@ def do_stuff(self):
8282

8383

8484
def main():
85-
num_obj = NumObj(-1)
86-
print(num_obj)
87-
88-
a_transaction = Transaction(True, num_obj)
89-
try:
90-
for i in range(3):
91-
num_obj.increment()
92-
print(num_obj)
93-
a_transaction.commit()
94-
print('-- committed')
95-
96-
for i in range(3):
97-
num_obj.increment()
98-
print(num_obj)
99-
num_obj.value += 'x' # will fail
100-
print(num_obj)
101-
except Exception:
102-
a_transaction.rollback()
103-
print('-- rolled back')
104-
print(num_obj)
105-
106-
print('-- now doing stuff ...')
107-
try:
108-
num_obj.do_stuff()
109-
except Exception:
110-
print('-> doing stuff failed!')
111-
import sys
112-
import traceback
113-
114-
traceback.print_exc(file=sys.stdout)
115-
print(num_obj)
116-
117-
118-
if __name__ == '__main__':
119-
main()
120-
121-
122-
OUTPUT = """
123-
<NumObj: -1>
124-
<NumObj: 0>
125-
<NumObj: 1>
126-
<NumObj: 2>
127-
-- committed
128-
<NumObj: 3>
129-
<NumObj: 4>
130-
<NumObj: 5>
131-
-- rolled back
132-
<NumObj: 2>
133-
-- now doing stuff ...
134-
-> doing stuff failed!
135-
Traceback (most recent call last):
136-
File "patterns/behavioral/memento.py", line 108, in main
137-
num_obj.do_stuff()
138-
File "patterns/behavioral/memento.py", line 63, in transaction
139-
raise e
140-
File "patterns/behavioral/memento.py", line 60, in transaction
141-
return self.method(obj, *args, **kwargs)
142-
File "patterns/behavioral/memento.py", line 81, in do_stuff
143-
self.increment() # <- will fail and rollback
144-
File "patterns/behavioral/memento.py", line 76, in increment
145-
self.value += 1
146-
TypeError: can only concatenate str (not "int") to str
147-
<NumObj: 2>
148-
"""
85+
"""
86+
>>> num_obj = NumObj(-1)
87+
>>> print(num_obj)
88+
<NumObj: -1>
89+
90+
>>> a_transaction = Transaction(True, num_obj)
91+
92+
>>> try:
93+
... for i in range(3):
94+
... num_obj.increment()
95+
... print(num_obj)
96+
... a_transaction.commit()
97+
... print('-- committed')
98+
... for i in range(3):
99+
... num_obj.increment()
100+
... print(num_obj)
101+
... num_obj.value += 'x' # will fail
102+
... print(num_obj)
103+
... except Exception:
104+
... a_transaction.rollback()
105+
... print('-- rolled back')
106+
<NumObj: 0>
107+
<NumObj: 1>
108+
<NumObj: 2>
109+
-- committed
110+
<NumObj: 3>
111+
<NumObj: 4>
112+
<NumObj: 5>
113+
-- rolled back
114+
115+
>>> print(num_obj)
116+
<NumObj: 2>
117+
118+
>>> print('-- now doing stuff ...')
119+
-- now doing stuff ...
120+
121+
>>> try:
122+
... num_obj.do_stuff()
123+
... except Exception:
124+
... print('-> doing stuff failed!')
125+
... import sys
126+
... import traceback
127+
... traceback.print_exc(file=sys.stdout)
128+
-> doing stuff failed!
129+
Traceback (most recent call last):
130+
...
131+
TypeError: ...str...int...
132+
133+
>>> print(num_obj)
134+
<NumObj: 2>
135+
"""
136+
137+
138+
if __name__ == "__main__":
139+
import doctest
140+
doctest.testmod(optionflags=doctest.ELLIPSIS)

patterns/behavioral/template.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,26 @@ def template_function(getter, converter=False, to_save=False):
5050

5151

5252
def main():
53-
template_function(get_text, to_save=True)
54-
print("-" * 30)
55-
template_function(get_pdf, converter=convert_to_text)
56-
print("-" * 30)
57-
template_function(get_csv, to_save=True)
53+
"""
54+
>>> template_function(get_text, to_save=True)
55+
Got `plain-text`
56+
Skip conversion
57+
[SAVE]
58+
`plain-text` was processed
59+
60+
>>> template_function(get_pdf, converter=convert_to_text)
61+
Got `pdf`
62+
[CONVERT]
63+
`pdf as text` was processed
64+
65+
>>> template_function(get_csv, to_save=True)
66+
Got `csv`
67+
Skip conversion
68+
[SAVE]
69+
`csv` was processed
70+
"""
5871

5972

6073
if __name__ == "__main__":
61-
main()
62-
63-
64-
OUTPUT = """
65-
Got `plain-text`
66-
Skip conversion
67-
[SAVE]
68-
`plain-text` was processed
69-
------------------------------
70-
Got `pdf`
71-
[CONVERT]
72-
`pdf as text` was processed
73-
------------------------------
74-
Got `csv`
75-
Skip conversion
76-
[SAVE]
77-
`csv` was processed
78-
"""
74+
import doctest
75+
doctest.testmod()

patterns/structural/adapter.py

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -70,71 +70,53 @@ class Adapter(object):
7070
Usage:
7171
dog = Dog()
7272
dog = Adapter(dog, make_noise=dog.bark)
73+
"""
74+
75+
def __init__(self, obj, **adapted_methods):
76+
"""We set the adapted methods in the object's dict"""
77+
self.obj = obj
78+
self.__dict__.update(adapted_methods)
79+
80+
def __getattr__(self, attr):
81+
"""All non-adapted calls are passed to the object"""
82+
return getattr(self.obj, attr)
7383

84+
def original_dict(self):
85+
"""Print original object dict"""
86+
return self.obj.__dict__
87+
88+
89+
def main():
90+
"""
7491
>>> objects = []
7592
>>> dog = Dog()
7693
>>> print(dog.__dict__)
7794
{'name': 'Dog'}
95+
7896
>>> objects.append(Adapter(dog, make_noise=dog.bark))
97+
98+
>>> objects[0].__dict__['obj'], objects[0].__dict__['make_noise']
99+
(<...Dog object at 0x...>, <bound method Dog.bark of <...Dog object at 0x...>>)
100+
79101
>>> print(objects[0].original_dict())
80102
{'name': 'Dog'}
103+
81104
>>> cat = Cat()
82105
>>> objects.append(Adapter(cat, make_noise=cat.meow))
83106
>>> human = Human()
84107
>>> objects.append(Adapter(human, make_noise=human.speak))
85108
>>> car = Car()
86-
>>> car_noise = lambda: car.make_noise(3)
87-
>>> objects.append(Adapter(car, make_noise=car_noise))
109+
>>> objects.append(Adapter(car, make_noise=lambda: car.make_noise(3)))
88110
89111
>>> for obj in objects:
90-
... print('A {} goes {}'.format(obj.name, obj.make_noise()))
112+
... print("A {0} goes {1}".format(obj.name, obj.make_noise()))
91113
A Dog goes woof!
92114
A Cat goes meow!
93115
A Human goes 'hello'
94116
A Car goes vroom!!!
95117
"""
96118

97-
def __init__(self, obj, **adapted_methods):
98-
"""We set the adapted methods in the object's dict"""
99-
self.obj = obj
100-
self.__dict__.update(adapted_methods)
101-
102-
def __getattr__(self, attr):
103-
"""All non-adapted calls are passed to the object"""
104-
return getattr(self.obj, attr)
105-
106-
def original_dict(self):
107-
"""Print original object dict"""
108-
return self.obj.__dict__
109-
110-
111-
def main():
112-
113-
objects = []
114-
dog = Dog()
115-
print(dog.__dict__)
116-
objects.append(Adapter(dog, make_noise=dog.bark))
117-
print(objects[0].__dict__)
118-
print(objects[0].original_dict())
119-
cat = Cat()
120-
objects.append(Adapter(cat, make_noise=cat.meow))
121-
human = Human()
122-
objects.append(Adapter(human, make_noise=human.speak))
123-
car = Car()
124-
objects.append(Adapter(car, make_noise=lambda: car.make_noise(3)))
125-
126-
for obj in objects:
127-
print("A {0} goes {1}".format(obj.name, obj.make_noise()))
128-
129119

130120
if __name__ == "__main__":
131-
main()
132-
133-
### OUTPUT ###
134-
# {'name': 'Dog'}
135-
# {'make_noise': <bound method Dog.bark of <__main__.Dog object at 0x7f631ba3fb00>>, 'obj': <__main__.Dog object at 0x7f631ba3fb00>} # noqa flake8
136-
# {'name': 'Dog'}
137-
# A Dog goes woof!
138-
# A Cat goes meow!
139-
# A Human goes 'hello'
140-
# A Car goes vroom!!!
121+
import doctest
122+
doctest.testmod(optionflags=doctest.ELLIPSIS)

tests/test_outputs.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
from patterns.behavioral.state import OUTPUT as state_output
3131
from patterns.behavioral.strategy import main as strategy_main
3232
from patterns.behavioral.strategy import OUTPUT as strategy_output
33-
from patterns.behavioral.template import main as template_main
34-
from patterns.behavioral.template import OUTPUT as template_output
3533
from patterns.behavioral.visitor import main as visitor_main
3634
from patterns.behavioral.visitor import OUTPUT as visitor_output
3735

@@ -50,7 +48,6 @@
5048
(specification_main, specification_output),
5149
(state_main, state_output),
5250
(strategy_main, strategy_output),
53-
(template_main, template_output),
5451
(visitor_main, visitor_output),
5552
])
5653
def test_output(main, output):

0 commit comments

Comments
 (0)