mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 02:14:04 +00:00
Add indentation tests with new assert_sample_output
This commit is contained in:
parent
6553cd50f0
commit
3b8de8826e
6 changed files with 138 additions and 1 deletions
|
|
@ -0,0 +1,3 @@
|
|||
import pytest
|
||||
|
||||
pytest.register_assert_rewrite('tests.utils')
|
||||
0
tests/samples/__init__.py
Normal file
0
tests/samples/__init__.py
Normal file
43
tests/samples/indentation.py
Normal file
43
tests/samples/indentation.py
Normal file
|
|
@ -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
|
||||
'''
|
||||
61
tests/samples/recursion.py
Normal file
61
tests/samples/recursion.py
Normal file
|
|
@ -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
|
||||
'''
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue