PySnooper/tests/test_infinite_depth_support.py
Elijah Qi 70f69a5cb7 Add infinite depth support for function call tracing
This commit adds a new feature that enables infinite depth support for function call tracing in pysnooper. The test_infinite_depth_support test case has been added to verify the correct behavior of the feature. The test case covers the case of tracing a recursive function call with unlimited depth and checks the output against the expected values. The feature works by setting the depth parameter to float('inf') in the pysnooper.snoop function.
2023-04-15 23:46:27 -04:00

110 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2023 Elijah Qi and Liuqing Yang.
# This program is distributed under the MIT license.
import io
import textwrap
import threading
import types
import sys
from pysnooper.utils import truncate
import pytest
import pysnooper
from pysnooper import pycompat
from pysnooper.variables import needs_parentheses
from .utils import (assert_output, assert_sample_output, VariableEntry,
CallEntry, LineEntry, ReturnEntry, OpcodeEntry,
ReturnValueEntry, ExceptionEntry, ExceptionValueEntry,
SourcePathEntry, CallEndedByExceptionEntry,
ElapsedTimeEntry)
from . import mini_toolbox
@pytest.mark.parametrize("normalize", (True, False))
def test_infinite_depth_support(normalize):
string_io = io.StringIO()
def func1(x):
return x
def func(x):
func1(x)
def foo(x):
func(x)
def recursive_function(x):
if x == 0:
return 1
foo(x)
recursive_function(x - 1)
with pysnooper.snoop(string_io, depth=float('inf'), normalize=normalize, color=False):
recursive_function(1)
output = string_io.getvalue()
assert_output(
output,
(
SourcePathEntry(),
VariableEntry(),
VariableEntry(),
VariableEntry(),
VariableEntry(),
VariableEntry(),
VariableEntry(),
LineEntry("recursive_function(1)"),
VariableEntry("x", "1"),
VariableEntry("foo"),
VariableEntry("recursive_function"),
CallEntry("def recursive_function(x):"),
LineEntry("if x == 0:"),
LineEntry("foo(x)"),
VariableEntry("x", "1"),
VariableEntry("func"),
CallEntry("def foo(x):"),
LineEntry("func(x)"),
VariableEntry("x", "1"),
VariableEntry("func1"),
CallEntry("def func(x):"),
LineEntry("func1(x)"),
VariableEntry("x", "1"),
CallEntry("def func1(x):"),
LineEntry("return x"),
ReturnEntry(),
ReturnValueEntry("1"),
ReturnEntry(),
ReturnValueEntry("None"),
ReturnEntry(),
ReturnValueEntry("None"),
LineEntry("recursive_function(x - 1)"),
VariableEntry("x", "0"),
VariableEntry("foo"),
VariableEntry("recursive_function"),
CallEntry("def recursive_function(x):"),
LineEntry("if x == 0:"),
LineEntry("return 1"),
ReturnEntry(),
ReturnValueEntry("1"),
ReturnEntry(),
ReturnValueEntry("None"),
LineEntry("with pysnooper.snoop(string_io, depth=float('inf'), normalize=normalize, color=False):"),
ElapsedTimeEntry(),
),
normalize=normalize,
)