Skip to content

Commit e5b253a

Browse files
authored
Merge pull request faif#269 from gyermolenko/test_outputs_behav
Add main function and tests for output for most of behavioral patterns
2 parents 8cc1a0b + ce84871 commit e5b253a

File tree

13 files changed

+232
-147
lines changed

13 files changed

+232
-147
lines changed

patterns/behavioral/catalog.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ def main():
166166

167167

168168
if __name__ == "__main__":
169-
170169
main()
171170

172-
### OUTPUT ###
173-
# executed method 2!
174-
# Value x1
175-
# Value x2
176-
# executed method 1!
171+
172+
OUTPUT = """
173+
executed method 2!
174+
Value x1
175+
Value x2
176+
executed method 1!
177+
"""

patterns/behavioral/chain.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def check_range(request):
9191
return False
9292

9393

94-
if __name__ == "__main__":
94+
def main():
9595
h0 = ConcreteHandler0()
9696
h1 = ConcreteHandler1()
9797
h2 = ConcreteHandler2(FallbackHandler())
@@ -102,13 +102,19 @@ def check_range(request):
102102
for request in requests:
103103
h0.handle(request)
104104

105-
### OUTPUT ###
106-
# request 2 handled in handler 0
107-
# request 5 handled in handler 0
108-
# request 14 handled in handler 1
109-
# request 22 handled in handler 2
110-
# request 18 handled in handler 1
111-
# request 3 handled in handler 0
112-
# end of chain, no handler for 35
113-
# request 27 handled in handler 2
114-
# request 20 handled in handler 2
105+
106+
if __name__ == "__main__":
107+
main()
108+
109+
110+
OUTPUT = """
111+
request 2 handled in handler 0
112+
request 5 handled in handler 0
113+
request 14 handled in handler 1
114+
request 22 handled in handler 2
115+
request 18 handled in handler 1
116+
request 3 handled in handler 0
117+
end of chain, no handler for 35
118+
request 27 handled in handler 2
119+
request 20 handled in handler 2
120+
"""

patterns/behavioral/chaining_method.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ def stop(self):
2626
print('then stop')
2727

2828

29-
if __name__ == '__main__':
30-
29+
def main():
3130
move = Action('move')
3231
person = Person('Jack', move)
3332
person.do_action().amount('5m').stop()
3433

35-
### OUTPUT ###
36-
# Jack move 5m then stop
34+
35+
if __name__ == '__main__':
36+
main()
37+
38+
39+
OUTPUT = """
40+
Jack move 5m then stop
41+
"""

patterns/behavioral/command.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ def main():
5959
if __name__ == "__main__":
6060
main()
6161

62-
### OUTPUT ###
63-
# renaming foo.txt to bar.txt
64-
# renaming bar.txt to baz.txt
65-
# renaming baz.txt to bar.txt
66-
# renaming bar.txt to foo.txt
62+
63+
OUTPUT = """
64+
renaming foo.txt to bar.txt
65+
renaming bar.txt to baz.txt
66+
renaming baz.txt to bar.txt
67+
renaming bar.txt to foo.txt
68+
"""

patterns/behavioral/iterator.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@ def count_to(count):
2323
count_to_two = lambda: count_to(2)
2424
count_to_five = lambda: count_to(5)
2525

26-
print('Counting to two...')
27-
for number in count_to_two():
28-
print(number, end=' ')
2926

30-
print()
27+
def main():
28+
print('Counting to two...')
29+
for number in count_to_two():
30+
print(number, end=' ')
3131

32-
print('Counting to five...')
33-
for number in count_to_five():
34-
print(number, end=' ')
32+
print()
3533

36-
print()
34+
print('Counting to five...')
35+
for number in count_to_five():
36+
print(number, end=' ')
37+
38+
print()
3739

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

patterns/behavioral/mediator.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def setTC(self, tc):
105105
self._tc = tc
106106

107107

108-
if __name__ == '__main__':
108+
def main():
109109
reporter = Reporter()
110110
db = DB()
111111
tm = TestManager()
@@ -124,21 +124,28 @@ def setTC(self, tc):
124124
tc.execute()
125125
tc.tearDown()
126126

127-
### OUTPUT ###
128-
# Setting up the Test
129-
# Inserting the execution begin status in the Database
130-
# Executing the test
131-
# Tearing down
132-
# Updating the test results in Database
133-
# Reporting the results of Test
134-
# Setting up the Test
135-
# Inserting the execution begin status in the Database
136-
# Reporter Class is preparing to report the results
137-
# Problem in setup. Test not executed.
138-
# Test not executed. No tear down required.
139-
# Setting up the Test
140-
# Inserting the execution begin status in the Database
141-
# Executing the test
142-
# Tearing down
143-
# Updating the test results in Database
144-
# Reporting the results of Test
127+
128+
if __name__ == '__main__':
129+
main()
130+
131+
132+
OUTPUT = """
133+
Setting up the Test
134+
Inserting the execution begin status in the Database
135+
Executing the test
136+
Tearing down
137+
Updating the test results in Database
138+
Reporting the results of Test
139+
Setting up the Test
140+
Inserting the execution begin status in the Database
141+
Executing the test
142+
Tearing down
143+
Updating the test results in Database
144+
Reporting the results of Test
145+
Setting up the Test
146+
Inserting the execution begin status in the Database
147+
Executing the test
148+
Tearing down
149+
Updating the test results in Database
150+
Reporting the results of Test
151+
"""

patterns/behavioral/memento.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def do_stuff(self):
8181
self.increment() # <- will fail and rollback
8282

8383

84-
if __name__ == '__main__':
84+
def main():
8585
num_obj = NumObj(-1)
8686
print(num_obj)
8787

@@ -114,30 +114,34 @@ def do_stuff(self):
114114
traceback.print_exc(file=sys.stdout)
115115
print(num_obj)
116116

117-
118-
### OUTPUT ###
119-
# <NumObj: -1>
120-
# <NumObj: 0>
121-
# <NumObj: 1>
122-
# <NumObj: 2>
123-
# -- committed
124-
# <NumObj: 3>
125-
# <NumObj: 4>
126-
# <NumObj: 5>
127-
# -- rolled back
128-
# <NumObj: 2>
129-
# -- now doing stuff ...
130-
# -> doing stuff failed!
131-
# Traceback (most recent call last):
132-
# File "memento.py", line 97, in <module>
133-
# num_obj.do_stuff()
134-
# File "memento.py", line 52, in transaction
135-
# raise e
136-
# File "memento.py", line 49, in transaction
137-
# return self.method(obj, *args, **kwargs)
138-
# File "memento.py", line 70, in do_stuff
139-
# self.increment() # <- will fail and rollback
140-
# File "memento.py", line 65, in increment
141-
# self.value += 1
142-
# TypeError: Can't convert 'int' object to str implicitly
143-
# <NumObj: 2>
117+
if __name__ == '__main__':
118+
main()
119+
120+
121+
OUTPUT = """
122+
<NumObj: -1>
123+
<NumObj: 0>
124+
<NumObj: 1>
125+
<NumObj: 2>
126+
-- committed
127+
<NumObj: 3>
128+
<NumObj: 4>
129+
<NumObj: 5>
130+
-- rolled back
131+
<NumObj: 2>
132+
-- now doing stuff ...
133+
-> doing stuff failed!
134+
Traceback (most recent call last):
135+
File "patterns/behavioral/memento.py", line 108, in main
136+
num_obj.do_stuff()
137+
File "patterns/behavioral/memento.py", line 63, in transaction
138+
raise e
139+
File "patterns/behavioral/memento.py", line 60, in transaction
140+
return self.method(obj, *args, **kwargs)
141+
File "patterns/behavioral/memento.py", line 81, in do_stuff
142+
self.increment() # <- will fail and rollback
143+
File "patterns/behavioral/memento.py", line 76, in increment
144+
self.value += 1
145+
TypeError: can only concatenate str (not "int") to str
146+
<NumObj: 2>
147+
"""

patterns/behavioral/observer.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,23 @@ def main():
9393
if __name__ == '__main__':
9494
main()
9595

96-
### OUTPUT ###
97-
# Setting Data 1 = 10
98-
# DecimalViewer: Subject Data 1 has data 10
99-
# HexViewer: Subject Data 1 has data 0xa
100-
# Setting Data 2 = 15
101-
# HexViewer: Subject Data 2 has data 0xf
102-
# DecimalViewer: Subject Data 2 has data 15
103-
# Setting Data 1 = 3
104-
# DecimalViewer: Subject Data 1 has data 3
105-
# HexViewer: Subject Data 1 has data 0x3
106-
# Setting Data 2 = 5
107-
# HexViewer: Subject Data 2 has data 0x5
108-
# DecimalViewer: Subject Data 2 has data 5
109-
# Detach HexViewer from data1 and data2.
110-
# Setting Data 1 = 10
111-
# DecimalViewer: Subject Data 1 has data 10
112-
# Setting Data 2 = 15
113-
# DecimalViewer: Subject Data 2 has data 15
96+
97+
OUTPUT = """
98+
Setting Data 1 = 10
99+
DecimalViewer: Subject Data 1 has data 10
100+
HexViewer: Subject Data 1 has data 0xa
101+
Setting Data 2 = 15
102+
HexViewer: Subject Data 2 has data 0xf
103+
DecimalViewer: Subject Data 2 has data 15
104+
Setting Data 1 = 3
105+
DecimalViewer: Subject Data 1 has data 3
106+
HexViewer: Subject Data 1 has data 0x3
107+
Setting Data 2 = 5
108+
HexViewer: Subject Data 2 has data 0x5
109+
DecimalViewer: Subject Data 2 has data 5
110+
Detach HexViewer from data1 and data2.
111+
Setting Data 1 = 10
112+
DecimalViewer: Subject Data 1 has data 10
113+
Setting Data 2 = 15
114+
DecimalViewer: Subject Data 2 has data 15
115+
"""

patterns/behavioral/publish_subscribe.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ def main():
8181
if __name__ == "__main__":
8282
main()
8383

84-
### OUTPUT ###
85-
# jim got cartoon
86-
# jack got music
87-
# gee got movie
88-
# jim got cartoon
89-
# jim got cartoon
90-
# gee got movie
84+
85+
OUTPUT = """
86+
jim got cartoon
87+
jack got music
88+
gee got movie
89+
jim got cartoon
90+
jim got cartoon
91+
gee got movie
92+
"""

patterns/behavioral/specification.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def is_satisfied_by(self, candidate):
9090
return getattr(candidate, 'super_user', False)
9191

9292

93-
if __name__ == '__main__':
93+
def main():
9494
print('Specification')
9595
andrey = User()
9696
ivan = User(super_user=True)
@@ -102,9 +102,14 @@ def is_satisfied_by(self, candidate):
102102
print(root_specification.is_satisfied_by(ivan))
103103
print(root_specification.is_satisfied_by(vasiliy))
104104

105+
106+
if __name__ == '__main__':
107+
main()
108+
105109

106-
### OUTPUT ###
107-
# Specification
108-
# False
109-
# True
110-
# False
110+
OUTPUT = """
111+
Specification
112+
False
113+
True
114+
False
115+
"""

patterns/behavioral/state.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,28 @@ def scan(self):
6868

6969

7070
# Test our radio out
71-
if __name__ == '__main__':
71+
def main():
7272
radio = Radio()
7373
actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
7474
actions *= 2
7575

7676
for action in actions:
7777
action()
7878

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
79+
80+
if __name__ == '__main__':
81+
main()
82+
83+
84+
OUTPUT = """
85+
Scanning... Station is 1380 AM
86+
Scanning... Station is 1510 AM
87+
Switching to FM
88+
Scanning... Station is 89.1 FM
89+
Scanning... Station is 103.9 FM
90+
Scanning... Station is 81.3 FM
91+
Scanning... Station is 89.1 FM
92+
Switching to AM
93+
Scanning... Station is 1250 AM
94+
Scanning... Station is 1380 AM
95+
"""

0 commit comments

Comments
 (0)