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_from_kwargs_split(): assert From(module=1, depth=2, depth_lt=3) == From(Query(module=1), Query(depth=2, depth_lt=3)) assert repr(From(module=1, depth=2, depth_lt=3)).replace( '<hunter._', '<hunter.' ) == ( "<hunter.predicates.From: condition=<hunter.predicates.Query: query_eq=(('module', 1),)>, " "predicate=<hunter.predicates.Query: query_eq=(('depth', 2),) query_lt=(('depth', 3),)>, watermark=0>" )
def test_hashing(): assert Q(module='a', function='b') in {Q(module='a', function='b')} assert ~Q(module='a', function='b') in {~Q(module='a', function='b')} assert (Q(module='a') | Q(function='b')) in {Q(module='a') | Q(function='b')} assert (Q(module='a') & Q(function='b')) in {Q(module='a') & Q(function='b')} assert Q(module='a', action=id) in {Q(module='a', action=id)} assert From(module='a', depth_gte=2) in {From(module='a', depth_gte=2)} class Foo(object): def __call__(self): pass pytest.raises(TypeError, set, Q(module=object(), action=Foo())) pytest.raises(TypeError, set, From(module=object(), depth_gte=object()))
def test_from(mockevent): pytest.raises((AttributeError, TypeError), From(), 1) assert From()(mockevent) is True called = [] assert From( Q(module='foo') | Q(module='bar'), lambda ev: called.append(ev))(mockevent) is False assert called == [] assert From(Not(Q(module='foo') | Q(module='bar')), lambda ev: called.append(ev))(mockevent) is None assert called called = [] assert From(Q(module=__name__), lambda ev: called.append(ev))(mockevent) is None assert called
def test_from_predicate_line_no_predicate(LineMatcher): buff = StringIO() from sample7 import one with trace(From(Q(fullsource_has='in_five')), action=CallPrinter(stream=buff)): one() output = buff.getvalue() lm = LineMatcher(output.splitlines()) lm.fnmatch_lines([ "* line * for i in range(1): # five", "* line * return i", ]) assert 'four' not in output assert 'three' not in output assert 'two' not in output assert 'one' not in output
def test_from_predicate(LineMatcher): buff = StringIO() from sample7 import one with trace(From(Q(function='five'), CallPrinter(stream=buff))): one() output = buff.getvalue() lm = LineMatcher(output.splitlines()) lm.fnmatch_lines([ "* call => five()", "* line for i in range(1): # five", "* line return i", "* return <= five: 0", ]) assert 'four' not in output assert 'three' not in output assert 'two' not in output assert 'one' not in output
def test_from_predicate_with_subpredicate(LineMatcher): buff = StringIO() from sample7 import one with trace(From(Q(source_has='# two'), Q(depth_lt=1)), action=CallPrinter(stream=buff)): one() output = buff.getvalue() lm = LineMatcher(output.splitlines()) lm.fnmatch_lines([ '* line for i in range(1): # two', '* line three()', '* call => three()', '* return <= three: None', '* line for i in range(1): # two', ]) assert 'five' not in output assert 'four' not in output assert 'one()' not in output assert '# one' not in output assert len(lm.lines) == 5
def test_predicate_reverse_and_or(): class Foobar(object): def __str__(self): return 'Foobar' __repr__ = __str__ def __call__(self, *args, **kwargs): pass foobar = Foobar() assert str(foobar & Q(module=1)) == 'And(Foobar, Query(module=1))' assert str(foobar | Q(module=1)) == 'Or(Foobar, Query(module=1))' assert str(foobar & (Q(module=1) | Q(module=2))) == 'And(Foobar, Or(Query(module=1), Query(module=2)))' assert str(foobar | (Q(module=1) | Q(module=2))) == 'Or(Foobar, Query(module=1), Query(module=2))' assert str(foobar & (Q(module=1) & Q(module=2))) == 'And(Foobar, Query(module=1), Query(module=2))' assert str(foobar | (Q(module=1) & Q(module=2))) == 'Or(Foobar, And(Query(module=1), Query(module=2)))' assert str(foobar & ~Q(module=1)) == 'And(Foobar, Not(Query(module=1)))' assert str(foobar | ~Q(module=1)) == 'Or(Foobar, Not(Query(module=1)))' assert str(foobar & Q(module=1, action=foobar)) == 'And(Foobar, When(Query(module=1), Foobar))' assert str(foobar | Q(module=1, action=foobar)) == 'Or(Foobar, When(Query(module=1), Foobar))' assert str(foobar & ~Q(module=1, action=foobar)) == 'And(Foobar, Not(When(Query(module=1), Foobar)))' assert str(foobar | ~Q(module=1, action=foobar)) == 'Or(Foobar, Not(When(Query(module=1), Foobar)))' assert str(foobar & From(module=1, depth=2)) == 'And(Foobar, From(Query(module=1), Query(depth=2), watermark=0))' assert str(foobar | From(module=1, depth=2)) == 'Or(Foobar, From(Query(module=1), Query(depth=2), watermark=0))' assert str(foobar & ~From(module=1, depth=2)) == 'And(Foobar, Not(From(Query(module=1), Query(depth=2), watermark=0)))' assert str(foobar | ~From(module=1, depth=2)) == 'Or(Foobar, Not(From(Query(module=1), Query(depth=2), watermark=0)))' assert str(Q(module=1) & foobar) == 'And(Query(module=1), Foobar)' assert str(Q(module=1) | foobar) == 'Or(Query(module=1), Foobar)' assert str(~Q(module=1) & foobar) == 'And(Not(Query(module=1)), Foobar)' assert str(~Q(module=1) | foobar) == 'Or(Not(Query(module=1)), Foobar)' assert str(Q(module=1, action=foobar) & foobar) == 'And(When(Query(module=1), Foobar), Foobar)' assert str(Q(module=1, action=foobar) | foobar) == 'Or(When(Query(module=1), Foobar), Foobar)' assert str(~Q(module=1, action=foobar) & foobar) == 'And(Not(When(Query(module=1), Foobar)), Foobar)' assert str(~Q(module=1, action=foobar) | foobar) == 'Or(Not(When(Query(module=1), Foobar)), Foobar)' assert str(From(module=1, depth=2) & foobar) == 'And(From(Query(module=1), Query(depth=2), watermark=0), Foobar)' assert str(From(module=1, depth=2) | foobar) == 'Or(From(Query(module=1), Query(depth=2), watermark=0), Foobar)'