Skip to content

Add main function and tests for output for most of behavioral patterns #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions patterns/behavioral/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,12 @@ def main():


if __name__ == "__main__":

main()

### OUTPUT ###
# executed method 2!
# Value x1
# Value x2
# executed method 1!

OUTPUT = """
executed method 2!
Value x1
Value x2
executed method 1!
"""
28 changes: 17 additions & 11 deletions patterns/behavioral/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def check_range(request):
return False


if __name__ == "__main__":
def main():
h0 = ConcreteHandler0()
h1 = ConcreteHandler1()
h2 = ConcreteHandler2(FallbackHandler())
Expand All @@ -102,13 +102,19 @@ def check_range(request):
for request in requests:
h0.handle(request)

### OUTPUT ###
# request 2 handled in handler 0
# request 5 handled in handler 0
# request 14 handled in handler 1
# request 22 handled in handler 2
# request 18 handled in handler 1
# request 3 handled in handler 0
# end of chain, no handler for 35
# request 27 handled in handler 2
# request 20 handled in handler 2

if __name__ == "__main__":
main()


OUTPUT = """
request 2 handled in handler 0
request 5 handled in handler 0
request 14 handled in handler 1
request 22 handled in handler 2
request 18 handled in handler 1
request 3 handled in handler 0
end of chain, no handler for 35
request 27 handled in handler 2
request 20 handled in handler 2
"""
13 changes: 9 additions & 4 deletions patterns/behavioral/chaining_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ def stop(self):
print('then stop')


if __name__ == '__main__':

def main():
move = Action('move')
person = Person('Jack', move)
person.do_action().amount('5m').stop()

### OUTPUT ###
# Jack move 5m then stop

if __name__ == '__main__':
main()


OUTPUT = """
Jack move 5m then stop
"""
12 changes: 7 additions & 5 deletions patterns/behavioral/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def main():
if __name__ == "__main__":
main()

### OUTPUT ###
# renaming foo.txt to bar.txt
# renaming bar.txt to baz.txt
# renaming baz.txt to bar.txt
# renaming bar.txt to foo.txt

OUTPUT = """
renaming foo.txt to bar.txt
renaming bar.txt to baz.txt
renaming baz.txt to bar.txt
renaming bar.txt to foo.txt
"""
34 changes: 21 additions & 13 deletions patterns/behavioral/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,28 @@ def count_to(count):
count_to_two = lambda: count_to(2)
count_to_five = lambda: count_to(5)

print('Counting to two...')
for number in count_to_two():
print(number, end=' ')

print()
def main():
print('Counting to two...')
for number in count_to_two():
print(number, end=' ')

print('Counting to five...')
for number in count_to_five():
print(number, end=' ')
print()

print()
print('Counting to five...')
for number in count_to_five():
print(number, end=' ')

print()

### OUTPUT ###
# Counting to two...
# one two
# Counting to five...
# one two three four five

if __name__ == "__main__":
main()


OUTPUT = """
Counting to two...
one two
Counting to five...
one two three four five
"""
45 changes: 26 additions & 19 deletions patterns/behavioral/mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def setTC(self, tc):
self._tc = tc


if __name__ == '__main__':
def main():
reporter = Reporter()
db = DB()
tm = TestManager()
Expand All @@ -124,21 +124,28 @@ def setTC(self, tc):
tc.execute()
tc.tearDown()

### OUTPUT ###
# Setting up the Test
# Inserting the execution begin status in the Database
# Executing the test
# Tearing down
# Updating the test results in Database
# Reporting the results of Test
# Setting up the Test
# Inserting the execution begin status in the Database
# Reporter Class is preparing to report the results
# Problem in setup. Test not executed.
# Test not executed. No tear down required.
# Setting up the Test
# Inserting the execution begin status in the Database
# Executing the test
# Tearing down
# Updating the test results in Database
# Reporting the results of Test

if __name__ == '__main__':
main()


OUTPUT = """
Setting up the Test
Inserting the execution begin status in the Database
Executing the test
Tearing down
Updating the test results in Database
Reporting the results of Test
Setting up the Test
Inserting the execution begin status in the Database
Executing the test
Tearing down
Updating the test results in Database
Reporting the results of Test
Setting up the Test
Inserting the execution begin status in the Database
Executing the test
Tearing down
Updating the test results in Database
Reporting the results of Test
"""
60 changes: 32 additions & 28 deletions patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def do_stuff(self):
self.increment() # <- will fail and rollback


if __name__ == '__main__':
def main():
num_obj = NumObj(-1)
print(num_obj)

Expand Down Expand Up @@ -114,30 +114,34 @@ def do_stuff(self):
traceback.print_exc(file=sys.stdout)
print(num_obj)


### OUTPUT ###
# <NumObj: -1>
# <NumObj: 0>
# <NumObj: 1>
# <NumObj: 2>
# -- committed
# <NumObj: 3>
# <NumObj: 4>
# <NumObj: 5>
# -- rolled back
# <NumObj: 2>
# -- now doing stuff ...
# -> doing stuff failed!
# Traceback (most recent call last):
# File "memento.py", line 97, in <module>
# num_obj.do_stuff()
# File "memento.py", line 52, in transaction
# raise e
# File "memento.py", line 49, in transaction
# return self.method(obj, *args, **kwargs)
# File "memento.py", line 70, in do_stuff
# self.increment() # <- will fail and rollback
# File "memento.py", line 65, in increment
# self.value += 1
# TypeError: Can't convert 'int' object to str implicitly
# <NumObj: 2>
if __name__ == '__main__':
main()


OUTPUT = """
<NumObj: -1>
<NumObj: 0>
<NumObj: 1>
<NumObj: 2>
-- committed
<NumObj: 3>
<NumObj: 4>
<NumObj: 5>
-- rolled back
<NumObj: 2>
-- now doing stuff ...
-> doing stuff failed!
Traceback (most recent call last):
File "patterns/behavioral/memento.py", line 108, in main
num_obj.do_stuff()
File "patterns/behavioral/memento.py", line 63, in transaction
raise e
File "patterns/behavioral/memento.py", line 60, in transaction
return self.method(obj, *args, **kwargs)
File "patterns/behavioral/memento.py", line 81, in do_stuff
self.increment() # <- will fail and rollback
File "patterns/behavioral/memento.py", line 76, in increment
self.value += 1
TypeError: can only concatenate str (not "int") to str
<NumObj: 2>
"""
38 changes: 20 additions & 18 deletions patterns/behavioral/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ def main():
if __name__ == '__main__':
main()

### OUTPUT ###
# Setting Data 1 = 10
# DecimalViewer: Subject Data 1 has data 10
# HexViewer: Subject Data 1 has data 0xa
# Setting Data 2 = 15
# HexViewer: Subject Data 2 has data 0xf
# DecimalViewer: Subject Data 2 has data 15
# Setting Data 1 = 3
# DecimalViewer: Subject Data 1 has data 3
# HexViewer: Subject Data 1 has data 0x3
# Setting Data 2 = 5
# HexViewer: Subject Data 2 has data 0x5
# DecimalViewer: Subject Data 2 has data 5
# Detach HexViewer from data1 and data2.
# Setting Data 1 = 10
# DecimalViewer: Subject Data 1 has data 10
# Setting Data 2 = 15
# DecimalViewer: Subject Data 2 has data 15

OUTPUT = """
Setting Data 1 = 10
DecimalViewer: Subject Data 1 has data 10
HexViewer: Subject Data 1 has data 0xa
Setting Data 2 = 15
HexViewer: Subject Data 2 has data 0xf
DecimalViewer: Subject Data 2 has data 15
Setting Data 1 = 3
DecimalViewer: Subject Data 1 has data 3
HexViewer: Subject Data 1 has data 0x3
Setting Data 2 = 5
HexViewer: Subject Data 2 has data 0x5
DecimalViewer: Subject Data 2 has data 5
Detach HexViewer from data1 and data2.
Setting Data 1 = 10
DecimalViewer: Subject Data 1 has data 10
Setting Data 2 = 15
DecimalViewer: Subject Data 2 has data 15
"""
16 changes: 9 additions & 7 deletions patterns/behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ def main():
if __name__ == "__main__":
main()

### OUTPUT ###
# jim got cartoon
# jack got music
# gee got movie
# jim got cartoon
# jim got cartoon
# gee got movie

OUTPUT = """
jim got cartoon
jack got music
gee got movie
jim got cartoon
jim got cartoon
gee got movie
"""
17 changes: 11 additions & 6 deletions patterns/behavioral/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def is_satisfied_by(self, candidate):
return getattr(candidate, 'super_user', False)


if __name__ == '__main__':
def main():
print('Specification')
andrey = User()
ivan = User(super_user=True)
Expand All @@ -102,9 +102,14 @@ def is_satisfied_by(self, candidate):
print(root_specification.is_satisfied_by(ivan))
print(root_specification.is_satisfied_by(vasiliy))


if __name__ == '__main__':
main()


### OUTPUT ###
# Specification
# False
# True
# False
OUTPUT = """
Specification
False
True
False
"""
30 changes: 18 additions & 12 deletions patterns/behavioral/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,28 @@ def scan(self):


# Test our radio out
if __name__ == '__main__':
def main():
radio = Radio()
actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2
actions *= 2

for action in actions:
action()

### OUTPUT ###
# Scanning... Station is 1380 AM
# Scanning... Station is 1510 AM
# Switching to FM
# Scanning... Station is 89.1 FM
# Scanning... Station is 103.9 FM
# Scanning... Station is 81.3 FM
# Scanning... Station is 89.1 FM
# Switching to AM
# Scanning... Station is 1250 AM
# Scanning... Station is 1380 AM

if __name__ == '__main__':
main()


OUTPUT = """
Scanning... Station is 1380 AM
Scanning... Station is 1510 AM
Switching to FM
Scanning... Station is 89.1 FM
Scanning... Station is 103.9 FM
Scanning... Station is 81.3 FM
Scanning... Station is 89.1 FM
Switching to AM
Scanning... Station is 1250 AM
Scanning... Station is 1380 AM
"""
Loading