From 3b8de8826ea2f177cbcdeb0277d193c108505012 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Sat, 11 May 2019 08:27:53 +0200 Subject: [PATCH] Add indentation tests with new assert_sample_output --- tests/__init__.py | 3 ++ tests/samples/__init__.py | 0 tests/samples/indentation.py | 43 +++++++++++++++++++++++++ tests/samples/recursion.py | 61 ++++++++++++++++++++++++++++++++++++ tests/test_pysnooper.py | 7 +++++ tests/utils.py | 25 ++++++++++++++- 6 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 tests/samples/__init__.py create mode 100644 tests/samples/indentation.py create mode 100644 tests/samples/recursion.py diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..db60abd 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +import pytest + +pytest.register_assert_rewrite('tests.utils') diff --git a/tests/samples/__init__.py b/tests/samples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/samples/indentation.py b/tests/samples/indentation.py new file mode 100644 index 0000000..ed254ca --- /dev/null +++ b/tests/samples/indentation.py @@ -0,0 +1,43 @@ +import pysnooper + + +@pysnooper.snoop(depth=2) +def main(): + f2() + + +def f2(): + f3() + + +def f3(): + f4() + + +@pysnooper.snoop(depth=2) +def f4(): + f5() + + +def f5(): + pass + + +expected_output = ''' +21:10:42.298924 call 5 def main(): +21:10:42.299158 line 6 f2() + 21:10:42.299205 call 9 def f2(): + 21:10:42.299246 line 10 f3() + 21:10:42.299305 call 18 def f4(): + 21:10:42.299348 line 19 f5() + 21:10:42.299386 call 22 def f5(): + 21:10:42.299424 line 23 pass + 21:10:42.299460 return 23 pass + Return value:.. None + 21:10:42.299509 return 19 f5() + Return value:.. None + 21:10:42.299577 return 10 f3() + Return value:.. None +21:10:42.299627 return 6 f2() +Return value:.. None +''' diff --git a/tests/samples/recursion.py b/tests/samples/recursion.py new file mode 100644 index 0000000..b4a78f3 --- /dev/null +++ b/tests/samples/recursion.py @@ -0,0 +1,61 @@ +import pysnooper + + +@pysnooper.snoop(depth=2) +def factorial(x): + if x <= 1: + return 1 + return mul(x, factorial(x - 1)) + + +def mul(a, b): + return a * b + + +def main(): + factorial(4) + +expected_output = ''' +Starting var:.. x = 4 +20:28:17.875295 call 5 def factorial(x): +20:28:17.875509 line 6 if x <= 1: +20:28:17.875550 line 8 return mul(x, factorial(x - 1)) + Starting var:.. x = 3 + 20:28:17.875624 call 5 def factorial(x): + 20:28:17.875668 line 6 if x <= 1: + 20:28:17.875703 line 8 return mul(x, factorial(x - 1)) + Starting var:.. x = 2 + 20:28:17.875771 call 5 def factorial(x): + 20:28:17.875813 line 6 if x <= 1: + 20:28:17.875849 line 8 return mul(x, factorial(x - 1)) + Starting var:.. x = 1 + 20:28:17.875913 call 5 def factorial(x): + 20:28:17.875953 line 6 if x <= 1: + 20:28:17.875987 line 7 return 1 + 20:28:17.876021 return 7 return 1 + Return value:.. 1 + Starting var:.. a = 2 + Starting var:.. b = 1 + 20:28:17.876111 call 11 def mul(a, b): + 20:28:17.876151 line 12 return a * b + 20:28:17.876190 return 12 return a * b + Return value:.. 2 + 20:28:17.876235 return 8 return mul(x, factorial(x - 1)) + Return value:.. 2 + Starting var:.. a = 3 + Starting var:.. b = 2 + 20:28:17.876320 call 11 def mul(a, b): + 20:28:17.876359 line 12 return a * b + 20:28:17.876397 return 12 return a * b + Return value:.. 6 + 20:28:17.876442 return 8 return mul(x, factorial(x - 1)) + Return value:.. 6 + Starting var:.. a = 4 + Starting var:.. b = 6 + 20:28:17.876525 call 11 def mul(a, b): + 20:28:17.876563 line 12 return a * b + 20:28:17.876601 return 12 return a * b + Return value:.. 24 +20:28:17.876646 return 8 return mul(x, factorial(x - 1)) +Return value:.. 24 +''' diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index c1112fc..729d6ca 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -12,6 +12,7 @@ import pytest import pysnooper from pysnooper.variables import needs_parentheses +from tests.utils import assert_sample_output from .utils import (assert_output, VariableEntry, CallEntry, LineEntry, ReturnEntry, OpcodeEntry, ReturnValueEntry, ExceptionEntry) @@ -932,3 +933,9 @@ def test_truncate(): else: assert truncated == 'aaaaaaaa...aaaaaaaaa' assert len(truncated) == max_length + + +def test_indentation(): + from .samples import indentation, recursion + assert_sample_output(indentation) + assert_sample_output(recursion) diff --git a/tests/utils.py b/tests/utils.py index 40d6781..e62a9d5 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -6,7 +6,7 @@ import abc import inspect from pysnooper.third_party.six.moves import zip_longest -from python_toolbox import caching +from python_toolbox import caching, sys_tools import pysnooper.pycompat @@ -263,3 +263,26 @@ def assert_output(output, expected_entries, prefix=None): if any_mismatch: raise OutputFailure(result) + + +def assert_sample_output(module): + with sys_tools.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + module.main() + + time = '21:10:42.298924' + time_pattern = re.sub(r'\d', r'\\d', time) + + def normalise(out): + return re.sub(time_pattern, time, out).strip() + + output = output_capturer.string_io.getvalue() + + try: + assert ( + normalise(output) == + normalise(module.expected_output) + ) + except AssertionError: + print('\n\nActual Output:\n\n' + output) # to copy paste into expected_output + raise # show pytest diff (may need -vv flag to see in full)