Skip to content

Commit c4216b2

Browse files
committed
Test outputs starting with two visitor and strategy
1 parent 74d363b commit c4216b2

File tree

5 files changed

+64
-43
lines changed

5 files changed

+64
-43
lines changed

append_output.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#!/bin/bash
22

3+
# This script (given path to a python script as an argument)
4+
# appends python outputs to given file.
5+
36
set -e
47

5-
src=$(sed -n -e '/### OUTPUT ###/,$!p' "$1")
6-
output=$(python "$1" | sed 's/^/# /')
8+
output_marker='OUTPUT = """'
9+
10+
# get everything (excluding part between `output_marker` and the end of the file)
11+
# into `src` var
12+
src=$(sed -n -e "/$output_marker/,\$!p" "$1")
13+
output=$(python "$1")
714

8-
# These are done separately to avoid having to insert a newline, which causes
9-
# problems when the text itself has '\n' in strings
1015
echo "$src" > $1
11-
echo -e "\n### OUTPUT ###" >> $1
16+
echo -e "\n" >> $1
17+
echo "$output_marker" >> $1
1218
echo "$output" >> $1
19+
echo '"""' >> $1

behavioral/strategy.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,21 @@ def on_sale_discount(order):
3636
return order.price * 0.25 + 20
3737

3838

39-
if __name__ == "__main__":
39+
def main():
4040
order0 = Order(100)
4141
order1 = Order(100, discount_strategy=ten_percent_discount)
4242
order2 = Order(1000, discount_strategy=on_sale_discount)
4343
print(order0)
4444
print(order1)
4545
print(order2)
4646

47-
### OUTPUT ###
48-
# <Price: 100, price after discount: 100>
49-
# <Price: 100, price after discount: 90.0>
50-
# <Price: 1000, price after discount: 730.0>
47+
48+
if __name__ == "__main__":
49+
main()
50+
51+
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+
"""

behavioral/visitor.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,22 @@ def visit_B(self, node, *args, **kwargs):
4545
print('visit_B ' + node.__class__.__name__)
4646

4747

48-
a = A()
49-
b = B()
50-
c = C()
51-
visitor = Visitor()
52-
visitor.visit(a)
53-
visitor.visit(b)
54-
visitor.visit(c)
55-
56-
### OUTPUT ###
57-
# generic_visit A
58-
# visit_B B
59-
# visit_B C
48+
def main():
49+
a = A()
50+
b = B()
51+
c = C()
52+
visitor = Visitor()
53+
visitor.visit(a)
54+
visitor.visit(b)
55+
visitor.visit(c)
56+
57+
58+
if __name__ == "__main__":
59+
main()
60+
61+
62+
OUTPUT = """
63+
generic_visit A
64+
visit_B B
65+
visit_B C
66+
"""

tests/test_outputs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from contextlib import redirect_stdout
2+
import io
3+
4+
import pytest
5+
6+
from behavioral.visitor import main as visitor_main
7+
from behavioral.visitor import OUTPUT as visitor_output
8+
from behavioral.strategy import main as strategy_main
9+
from behavioral.strategy import OUTPUT as strategy_output
10+
11+
@pytest.mark.parametrize("main,output", [
12+
(visitor_main, visitor_output),
13+
(strategy_main, strategy_output),
14+
])
15+
def test_output(main, output):
16+
f = io.StringIO()
17+
with redirect_stdout(f):
18+
main()
19+
20+
real_output = f.getvalue().strip()
21+
expected_output = output.strip()
22+
assert real_output == expected_output

tests/test_strategy.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)