def test_str_repr(): assert repr(Q(module='a', function='b')).endswith( "predicates.Query: query_eq=(('function', 'b'), ('module', 'a'))>") assert str(Q(module='a', function='b')) == "Query(function='b', module='a')" assert repr(Q( module='a')).endswith("predicates.Query: query_eq=(('module', 'a'),)>") assert str(Q(module='a')) == "Query(module='a')" assert "predicates.When: condition=<hunter." in repr( Q(module='a', action=C('foo'))) assert "predicates.Query: query_eq=(('module', 'a'),)>, actions=('foo',)>" in repr( Q(module='a', action=C('foo'))) assert str(Q(module='a', action=C('foo'))) == "When(Query(module='a'), 'foo')" assert "predicates.Not: predicate=<hunter." in repr(~Q(module='a')) assert "predicates.Query: query_eq=(('module', 'a'),)>>" in repr(~Q( module='a')) assert str(~Q(module='a')) == "Not(Query(module='a'))" assert "predicates.Or: predicates=(<hunter." in repr( Q(module='a') | Q(module='b')) assert "predicates.Query: query_eq=(('module', 'a'),)>, " in repr( Q(module='a') | Q(module='b')) assert repr(Q(module='a') | Q(module='b')).endswith( "predicates.Query: query_eq=(('module', 'b'),)>)>") assert str(Q(module='a') | Q(module='b')) == "Or(Query(module='a'), Query(module='b'))" assert "predicates.And: predicates=(<hunter." in repr( Q(module='a') & Q(module='b')) assert "predicates.Query: query_eq=(('module', 'a'),)>," in repr( Q(module='a') & Q(module='b')) assert repr(Q(module='a') & Q(module='b')).endswith( "predicates.Query: query_eq=(('module', 'b'),)>)>") assert str(Q(module='a') & Q(module='b')) == "And(Query(module='a'), Query(module='b'))" assert repr(From(module='a', depth_lte=2)).replace( '<hunter._', '<hunter.' ) == ( "<hunter.predicates.From: condition=<hunter.predicates.Query: query_eq=(('module', 'a'),)>, " "predicate=<hunter.predicates.Query: query_lte=(('depth', 2),)>, watermark=0>" ) assert str(From(module='a', depth_gte=2) ) == "From(Query(module='a'), Query(depth_gte=2), watermark=0)" assert repr(Backlog(module='a', action=CodePrinter, size=2)).replace( '<hunter._', '<hunter.' ).startswith( "<hunter.predicates.Backlog: condition=<hunter.predicates.Query: query_eq=(('module', 'a'),)>, " "size=2, stack=10, vars=False, action=CodePrinter") assert repr(Debugger()) == "Debugger(klass=<class 'pdb.Pdb'>, kwargs={})" assert str(Debugger()) == "Debugger(klass=<class 'pdb.Pdb'>, kwargs={})" assert repr(Manhole()) == 'Manhole(options={})' assert str(Manhole()) == 'Manhole(options={})'
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", globals=True, 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 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[]]", ])