From c4216b2bb64892edc63ee8aeef35c4a98a26bde1 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Sat, 12 Jan 2019 20:51:08 +0200 Subject: [PATCH 1/2] Test outputs starting with two visitor and strategy --- append_output.sh | 17 ++++++++++++----- behavioral/strategy.py | 16 +++++++++++----- behavioral/visitor.py | 31 +++++++++++++++++++------------ tests/test_outputs.py | 22 ++++++++++++++++++++++ tests/test_strategy.py | 21 --------------------- 5 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 tests/test_outputs.py delete mode 100644 tests/test_strategy.py diff --git a/append_output.sh b/append_output.sh index a3b7948b9..3e5ea610f 100755 --- a/append_output.sh +++ b/append_output.sh @@ -1,12 +1,19 @@ #!/bin/bash +# This script (given path to a python script as an argument) +# appends python outputs to given file. + set -e -src=$(sed -n -e '/### OUTPUT ###/,$!p' "$1") -output=$(python "$1" | sed 's/^/# /') +output_marker='OUTPUT = """' + +# get everything (excluding part between `output_marker` and the end of the file) +# into `src` var +src=$(sed -n -e "/$output_marker/,\$!p" "$1") +output=$(python "$1") -# These are done separately to avoid having to insert a newline, which causes -# problems when the text itself has '\n' in strings echo "$src" > $1 -echo -e "\n### OUTPUT ###" >> $1 +echo -e "\n" >> $1 +echo "$output_marker" >> $1 echo "$output" >> $1 +echo '"""' >> $1 diff --git a/behavioral/strategy.py b/behavioral/strategy.py index 704623a81..94a5d50eb 100644 --- a/behavioral/strategy.py +++ b/behavioral/strategy.py @@ -36,7 +36,7 @@ def on_sale_discount(order): return order.price * 0.25 + 20 -if __name__ == "__main__": +def main(): order0 = Order(100) order1 = Order(100, discount_strategy=ten_percent_discount) order2 = Order(1000, discount_strategy=on_sale_discount) @@ -44,7 +44,13 @@ def on_sale_discount(order): print(order1) print(order2) -### OUTPUT ### -# -# -# + +if __name__ == "__main__": + main() + + +OUTPUT = """ + + + +""" diff --git a/behavioral/visitor.py b/behavioral/visitor.py index 48e0986b7..bceb64d24 100644 --- a/behavioral/visitor.py +++ b/behavioral/visitor.py @@ -45,15 +45,22 @@ def visit_B(self, node, *args, **kwargs): print('visit_B ' + node.__class__.__name__) -a = A() -b = B() -c = C() -visitor = Visitor() -visitor.visit(a) -visitor.visit(b) -visitor.visit(c) - -### OUTPUT ### -# generic_visit A -# visit_B B -# visit_B C +def main(): + a = A() + b = B() + c = C() + visitor = Visitor() + visitor.visit(a) + visitor.visit(b) + visitor.visit(c) + + +if __name__ == "__main__": + main() + + +OUTPUT = """ +generic_visit A +visit_B B +visit_B C +""" diff --git a/tests/test_outputs.py b/tests/test_outputs.py new file mode 100644 index 000000000..7bcd3775c --- /dev/null +++ b/tests/test_outputs.py @@ -0,0 +1,22 @@ +from contextlib import redirect_stdout +import io + +import pytest + +from behavioral.visitor import main as visitor_main +from behavioral.visitor import OUTPUT as visitor_output +from behavioral.strategy import main as strategy_main +from behavioral.strategy import OUTPUT as strategy_output + +@pytest.mark.parametrize("main,output", [ + (visitor_main, visitor_output), + (strategy_main, strategy_output), +]) +def test_output(main, output): + f = io.StringIO() + with redirect_stdout(f): + main() + + real_output = f.getvalue().strip() + expected_output = output.strip() + assert real_output == expected_output diff --git a/tests/test_strategy.py b/tests/test_strategy.py deleted file mode 100644 index 1e585d3d4..000000000 --- a/tests/test_strategy.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import os -import subprocess -import unittest - - -class StrategyTest(unittest.TestCase): - def test_print_output(self): - """ - Verifies the print output when strategy.py is executed. - The expected_output is equivalent to the output on the command - line when running 'python strategy.py'. - """ - output = subprocess.check_output(["python", "behavioral/strategy.py"]) - expected_output = os.linesep.join( - ['', '', '', ''] - ) - # byte representation required due to EOF returned subprocess - expected_output_as_bytes = expected_output.encode(encoding='UTF-8') - self.assertEqual(output, expected_output_as_bytes) From b88f43d5f0ed22ef90759a1006c1039e664732c8 Mon Sep 17 00:00:00 2001 From: Grygorii Iermolenko Date: Sat, 12 Jan 2019 21:15:21 +0200 Subject: [PATCH 2/2] Skip these tests for python2 because redirect_stdout is not available --- tests/test_outputs.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_outputs.py b/tests/test_outputs.py index 7bcd3775c..084571d32 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -1,5 +1,10 @@ -from contextlib import redirect_stdout +try: + from contextlib import redirect_stdout +except: + pass + import io +import sys import pytest @@ -8,6 +13,8 @@ from behavioral.strategy import main as strategy_main from behavioral.strategy import OUTPUT as strategy_output +@pytest.mark.skipif(sys.version_info < (3,4), + reason="requires python3.4 or higher") @pytest.mark.parametrize("main,output", [ (visitor_main, visitor_output), (strategy_main, strategy_output),