def test_custom_action(): calls = [] with trace(action=lambda event: calls.append(event.function), kind='return'): def foo(): return 1 foo() assert 'foo' in calls
def test_locals(): out = StringIO() with hunter.trace(lambda event: event.locals.get("node") == "Foobar", module="test_hunter", function="foo", action=CodePrinter(stream=out)): def foo(): a = 1 node = "Foobar" node += "x" a += 2 return a foo() assert out.getvalue().endswith('node += "x"\n')
def test_locals(): out = StringIO() with hunter.trace(lambda event: event.locals.get('node') == 'Foobar', module=__name__, function='foo', action=CodePrinter(stream=out)): def foo(): a = 1 node = 'Foobar' node += 'x' a += 2 return a foo() assert out.getvalue().endswith("node += 'x'\n")
def test_wraps(LineMatcher): calls = [] @hunter.wrap(action=lambda event: calls.append( "%06s calls=%s depth=%s %s" % (event.kind, event.calls, event.depth, event.fullsource))) def foo(): return 1 foo() lm = LineMatcher(calls) lm.fnmatch_lines([ ' call calls=0 depth=0 @hunter.wrap*', ' line calls=1 depth=1 return 1\n', 'return calls=1 depth=1 return 1\n', ])
def test_fullsource_decorator_issue(LineMatcher): out = StringIO() with trace(kind='call', action=CodePrinter(stream=out)): foo = bar = lambda x: x @foo @bar def foo(): return 1 foo() lm = LineMatcher(out.getvalue().splitlines()) lm.fnmatch_lines([ '* call @foo', '* | @bar', '* | def foo():', ])
def test_wraps(LineMatcher): calls = [] @hunter.wrap(action=lambda event: calls.append('%6r calls=%r depth=%r %s' % (event.kind, event.calls, event.depth, event.fullsource))) def foo(): return 1 foo() lm = LineMatcher(calls) for line in calls: print(repr(line)) lm.fnmatch_lines([ "'call' calls=0 depth=0 @hunter.wrap*", "'line' calls=1 depth=1 return 1\n", "'return' calls=1 depth=0 return 1\n", ]) for call in calls: assert 'tracer.stop()' not in call
def test_debugger(LineMatcher): out = StringIO() calls = [] class FakePDB: def __init__(self, foobar=1): calls.append(foobar) def set_trace(self, frame): calls.append(frame.f_code.co_name) with hunter.trace(lambda event: event.locals.get('node') == 'Foobar', module='test_hunter', function='foo', actions=[ CodePrinter, VarsPrinter('a', 'node', 'foo', 'test_debugger', stream=out), Debugger(klass=FakePDB, foobar=2) ]): def foo(): a = 1 node = 'Foobar' node += 'x' a += 2 return a foo() print(out.getvalue()) assert calls == [2, 'foo'] lm = LineMatcher(out.getvalue().splitlines()) pprint(lm.lines) lm.fnmatch_lines_random([ "* [[]test_debugger => <function test_debugger at *[]]", "* [[]node => 'Foobar'[]]", "* [[]a => 1[]]", ])
def main(): foo()
def bar(): a = 1 foo() a = 4