Skip to content

Commit d3ff458

Browse files
committed
Doctest Memento
1 parent b50a6e8 commit d3ff458

File tree

1 file changed

+56
-64
lines changed

1 file changed

+56
-64
lines changed

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: can only concatenate str (not "int") to str
132+
133+
>>> print(num_obj)
134+
<NumObj: 2>
135+
"""
136+
137+
138+
if __name__ == "__main__":
139+
import doctest
140+
doctest.testmod(optionflags=doctest.ELLIPSIS)

0 commit comments

Comments
 (0)