Allow a callable to be passed to snoop()

Fixes: #80
This commit is contained in:
Mike Bayer 2019-05-02 13:57:44 -06:00 committed by Ram Rachum
parent b8dec67a4f
commit 20e746ac92
2 changed files with 36 additions and 0 deletions

View file

@ -27,6 +27,9 @@ def get_write_and_truncate_functions(output):
def truncate():
with open(output, 'w') as output_file:
pass
elif callable(output):
write = output
truncate = None
else:
assert isinstance(output, utils.WritableStream)
def write(s):

View file

@ -43,6 +43,39 @@ def test_string_io():
)
def test_callable():
string_io = io.StringIO()
def write(msg):
string_io.write(msg)
@pysnooper.snoop(write)
def my_function(foo):
x = 7
y = 8
return y + x
result = my_function('baba')
assert result == 15
output = string_io.getvalue()
assert_output(
output,
(
VariableEntry('foo', value_regex="u?'baba'"),
CallEntry('def my_function(foo):'),
LineEntry('x = 7'),
VariableEntry('x', '7'),
LineEntry('y = 8'),
VariableEntry('y', '8'),
LineEntry('return y + x'),
ReturnEntry('return y + x'),
ReturnValueEntry('15'),
)
)
def test_variables():
class Foo(object):