Skip to content

Commit 040f0d0

Browse files
authored
Merge pull request faif#262 from gyermolenko/test_visitor_outputs
Test outputs starting with visitor and strategy
2 parents 16898f8 + b88f43d commit 040f0d0

File tree

5 files changed

+71
-43
lines changed

5 files changed

+71
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
try:
2+
from contextlib import redirect_stdout
3+
except:
4+
pass
5+
6+
import io
7+
import sys
8+
9+
import pytest
10+
11+
from behavioral.visitor import main as visitor_main
12+
from behavioral.visitor import OUTPUT as visitor_output
13+
from behavioral.strategy import main as strategy_main
14+
from behavioral.strategy import OUTPUT as strategy_output
15+
16+
@pytest.mark.skipif(sys.version_info < (3,4),
17+
reason="requires python3.4 or higher")
18+
@pytest.mark.parametrize("main,output", [
19+
(visitor_main, visitor_output),
20+
(strategy_main, strategy_output),
21+
])
22+
def test_output(main, output):
23+
f = io.StringIO()
24+
with redirect_stdout(f):
25+
main()
26+
27+
real_output = f.getvalue().strip()
28+
expected_output = output.strip()
29+
assert real_output == expected_output

tests/test_strategy.py

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

0 commit comments

Comments
 (0)