8000 Merge pull request #292 from gyermolenko/more_doctests · faif/python-patterns@c960478 · GitHub
[go: up one dir, main page]

Skip to content

Commit c960478

Browse files
authored
Merge pull request #292 from gyermolenko/more_doctests
Doctests for more scripts
2 parents e262696 + 271a61b commit c960478

File tree

8 files changed

+105
-137
lines changed

8 files changed

+105
-137
lines changed

patterns/behavioral/catalog.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +146,24 @@ def main_method(self):
146146

147147
def main():
148148
"""
149-
>>> c = Catalog('param_value_1').main_method()
150-
executed method 1!
151-
>>> Catalog('param_value_2').main_method()
149+
>>> test = Catalog('param_value_2')
150+
>>> test.main_method()
152151
executed method 2!
153-
"""
154152
155-
test = Catalog('param_value_2')
156-
test.main_method()
153+
>>> test = CatalogInstance('param_value_1')
154+
>>> test.main_method()
155+
Value x1
157156
158-
test = CatalogInstance('param_value_1')
159-
test.main_method()
157+
>>> test = CatalogClass('param_value_2')
158+
>>> test.main_method()
159+
Value x2
160160
161-
test = CatalogClass('param_value_2')
162-
test.main_method()
163-
164-
test = CatalogStatic('param_value_1')
165-
test.main_method()
161+
>>> test = CatalogStatic('param_value_1')
162+
>>> test.main_method()
163+
executed method 1!
164+
"""
166165

167166

168167
if __name__ == "__main__":
169-
main()
170-
171-
172-
OUTPUT = """
173-
executed method 2!
174-
Value x1
175-
Value x2
176-
executed method 1!
177-
"""
168+
import doctest
169+
doctest.testmod()

patterns/behavioral/chaining_method.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ def stop(self):
2727

2828

2929
def main():
30-
move = Action('move')
31-
person = Person('Jack', move)
32-
person.do_action().amount('5m').stop()
30+
"""
31+
>>> move = Action('move')
32+
>>> person = Person('Jack', move)
33+
>>> person.do_action().amount('5m').stop()
34+
Jack move 5m then stop
35+
"""
3336

3437

3538
if __name__ == '__main__':
36-
main()
37-
38-
39-
OUTPUT = """
40-
Jack move 5m then stop
41-
"""
39+
import doctest
40+
doctest.testmod()

patterns/behavioral/command.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from __future__ import print_function
1414
import os
15-
from os.path import lexists
1615

1716

1817
class MoveFileCommand(object):
@@ -32,38 +31,38 @@ def rename(self, src, dest):
3231

3332

3433
def main():
35-
command_stack = []
34+
"""
35+
>>> from os.path import lexists
3636
37-
# commands are just pushed into the command stack
38-
command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
39-
command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
37+
>>> command_stack = [
38+
... MoveFileCommand('foo.txt', 'bar.txt'),
39+
... MoveFileCommand('bar.txt', 'baz.txt')
40+
... ]
4041
41-
# verify that none of the target files exist
42-
assert not lexists("foo.txt")
43-
assert not lexists("bar.txt")
44-
assert not lexists("baz.txt")
45-
try:
46-
with open("foo.txt", "w"): # Creating the file
47-
pass
42+
# Verify that none of the target files exist
43+
>>> assert not lexists("foo.txt")
44+
>>> assert not lexists("bar.txt")
45+
>>> assert not lexists("baz.txt")
4846
49-
# they can be executed later on
50-
for cmd in command_stack:
51-
cmd.execute()
47+
# Create empty file
48+
>>> open("foo.txt", "w").close()
5249
53-
# and can also be undone at will
54-
for cmd in reversed(command_stack):
55-
cmd.undo()
56-
finally:
57-
os.unlink("foo.txt")
50+
# Commands can be executed later on
51+
>>> for cmd in command_stack:
52+
... cmd.execute()
53+
renaming foo.txt to bar.txt
54+
renaming bar.txt to baz.txt
5855
56+
# And can also be undone at will
57+
>>> for cmd in reversed(command_stack):
58+
... cmd.undo()
59+
renaming baz.txt to bar.txt
60+
renaming bar.txt to foo.txt
5961
60-
if __name__ == "__main__":
61-
main()
62+
>>> os.unlink("foo.txt")
63+
"""
6264

6365

64-
OUTPUT = """
65-
renaming foo.txt to bar.txt
66-
renaming bar.txt to baz.txt
67-
renaming baz.txt to bar.txt
68-
renaming bar.txt to foo.txt
69-
"""
66+
if __name__ == "__main__":
67+
import doctest
68+
doctest.testmod()

patterns/behavioral/iterator.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ def count_to(count):
2525

2626

2727
def main():
28-
print('Counting to two...')
29-
for number in count_to_two():
30-
print(number, end=' ')
31-
32-
print('\nCounting to five...')
33-
for number in count_to_five():
34-
print(number, end=' ')
28+
"""
29+
# Counting to two...
30+
>>> for number in count_to_two():
31+
... print(number)
32+
one
33+
two
34+
35+
# Counting to five...
36+
>>> for number in count_to_five():
37+
... print(number)
38+
one
39+
two
40+
three
41+
four
42+
five
43+
"""
3544

3645

3746
if __name__ == "__main__":
38-
main()
39-
40-
41-
OUTPUT = """
42-
Counting to two...
43-
one two
44-
Counting to five...
45-
one two three four five
46-
""" # noqa
47+
import doctest
48+
doctest.testmod()

patterns/behavioral/mediator.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@ def __str__(self):
3434

3535

3636
def main():
37-
molly = User('Molly')
38-
mark = User('Mark')
39-
ethan = User('Ethan')
37+
"""
38+
>>> molly = User('Molly')
39+
>>> mark = User('Mark')
40+
>>> ethan = User('Ethan')
4041
41-
molly.say("Hi Team! Meeting at 3 PM today.")
42-
mark.say("Roger that!")
43-
ethan.say("Alright.")
42+
>>> molly.say("Hi Team! Meeting at 3 PM today.")
43+
[Molly says]: Hi Team! Meeting at 3 PM today.
44+
>>> mark.say("Roger that!")
45+
[Mark says]: Roger that!
46+
>>> ethan.say("Alright.")
47+
[Ethan says]: Alright.
48+
"""
4449

4550

4651
if __name__ == '__main__':
47-
main()
48-
49-
50-
OUTPUT = """
51-
[Molly says]: Hi Team! Meeting at 3 PM today.
52-
[Mark says]: Roger that!
53-
[Ethan says]: Alright.
54-
""" # noqa
52+
import doctest
53+
doctest.testmod()

patterns/behavioral/strategy.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,18 @@ def on_sale_discount(order):
3737

3838

3939
def main():
40-
order0 = Order(100)
41-
order1 = Order(100, discount_strategy=ten_percent_discount)
42-
order2 = Order(1000, discount_strategy=on_sale_discount)
43-
print(order0)
44-
print(order1)
45-
print(order2)
40+
"""
41+
>>> Order(100)
42+
<Price: 100, price after discount: 100>
4643
44+
>>> Order(100, discount_strategy=ten_percent_discount)
45+
<Price: 100, price after discount: 90.0>
4746
48-
if __name__ == "__main__":
49-
main()
47+
>>> Order(1000, discount_strategy=on_sale_discount)
48+
<Price: 1000, price after discount: 730.0>
49+
"""
5050

5151

52-
OUTPUT = """
53-
<Price: 100, price after discount: 100>
54-
<Price: 100, price after discount: 90.0>
55-
<Price: 1000, price after discount: 730.0>
56-
"""
52+
if __name__ == "__main__":
53+
import doctest
54+
doctest.testmod()

patterns/behavioral/visitor.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ def visit_B(self, node, *args, **kwargs):
5656

5757

5858
def main():
59-
a = A()
60-
b = B()
61-
c = C()
62-
visitor = Visitor()
63-
visitor.visit(a)
64-
visitor.visit(b)
65-
visitor.visit(c)
59+
"""
60+
>>> a, b, c = A(), B(), C()
61+
>>> visitor = Visitor()
6662
63+
>>> visitor.visit(a)
64+
generic_visit A
6765
68-
if __name__ == "__main__":
69-
main()
66+
>>> visitor.visit(b)
67+
visit_B B
7068
69+
>>> visitor.visit(c)
70+
visit_B C
71+
"""
7172

72-
OUTPUT = """
73-
generic_visit A
74-
visit_B B
75-
visit_B C
76-
"""
73+
74+
if __name__ == "__main__":
75+
import doctest
76+
doctest.testmod()

tests/test_outputs.py

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

99
import pytest
1010

11-
from patterns.behavioral.catalog import main as catalog_main
12-
from patterns.behavioral.catalog import OUTPUT as catalog_output
13-
from patterns.behavioral.chaining_method import main as chaining_method_main
14-
from patterns.behavioral.chaining_method import OUTPUT as chaining_method_output
15-
from patterns.behavioral.command import main as command_main
16-
from patterns.behavioral.command import OUTPUT as command_output
17-
from patterns.behavioral.iterator import main as iterator_main
18-
from patterns.behavioral.iterator import OUTPUT as iterator_output
19-
from patterns.behavioral.mediator import main as mediator_main
20-
from patterns.behavioral.mediator import OUTPUT as mediator_output
2111
from patterns.behavioral.observer import main as observer_main
2212
from patterns.behavioral.observer import OUTPUT as observer_output
2313
from patterns.behavioral.publish_subscribe import main as publish_subscribe_main
@@ -26,26 +16,15 @@
2616
from patterns.behavioral.specification import OUTPUT as specification_output
2717
from patterns.behavioral.state import main as state_main
2818
from patterns.behavioral.state import OUTPUT as state_output
29-
from patterns.behavioral.strategy import main as strategy_main
30-
from patterns.behavioral.strategy import OUTPUT as strategy_output
31-
from patterns.behavioral.visitor import main as visitor_main
32-
from patterns.behavioral.visitor import OUTPUT as visitor_output
3319

3420

3521
@pytest.mark.skipif(sys.version_info < (3,4),
3622
reason="requires python3.4 or higher")
3723
@pytest.mark.parametrize("main,output", [
38-
(catalog_main, catalog_output),
39-
(chaining_method_main, chaining_method_output),
40-
(command_main, command_output),
41-
(iterator_main, iterator_output),
42-
(mediator_main, mediator_output),
4324
(observer_main, observer_output),
4425
(publish_subscribe_main, publish_subscribe_output),
4526
(specification_main, specification_output),
4627
(state_main, state_output),
47-
(strategy_main, strategy_output),
48-
(visitor_main, visitor_output),
4928
])
5029
def test_output(main, output):
5130
f = io.StringIO()

0 commit comments

Comments
 (0)
0