예제 #1
0
def test_when(mockevent):
    called = []
    assert When(Q(module='foo'),
                lambda ev: called.append(ev))(mockevent) is False
    assert called == []

    assert When(Q(module=__name__),
                lambda ev: called.append(ev))(mockevent) is True
    assert called == [mockevent]

    called = []
    assert Q(module=__name__,
             action=lambda ev: called.append(ev))(mockevent) is True
    assert called == [mockevent]

    called = [[], []]
    predicate = (Q(module=__name__, action=lambda ev: called[0].append(ev))
                 | Q(module='foo', action=lambda ev: called[1].append(ev)))
    assert predicate(mockevent) is True
    assert called == [[mockevent], []]

    assert predicate(mockevent) is True
    assert called == [[mockevent, mockevent], []]

    called = [[], []]
    predicate = (
        Q(module=__name__, action=lambda ev: called[0].append(ev))
        & Q(function='mockevent', action=lambda ev: called[1].append(ev)))
    assert predicate(mockevent) is True
    assert called == [[mockevent], [mockevent]]
예제 #2
0
def test_q_expansion():
    assert Q(C(1), C(2), module=3) == And(C(1), C(2), Q(module=3))
    assert Q(C(1), C(2), module=3,
             action=C(4)) == When(And(C(1), C(2), Q(module=3)), C(4))
    assert Q(C(1), C(2), module=3,
             actions=[C(4), C(5)]) == When(And(C(1), C(2), Q(module=3)), C(4),
                                           C(5))
예제 #3
0
def test_trace_merge():
    with hunter.trace(function='a'):
        with hunter.trace(function='b'):
            with hunter.trace(function='c'):
                assert sys.gettrace().handler == When(Q(function='c'), CallPrinter)
            assert sys.gettrace().handler == When(Q(function='b'), CallPrinter)
        assert sys.gettrace().handler == When(Q(function='a'), CallPrinter)
예제 #4
0
def test_trace_merge():
    with hunter.trace(function="a"):
        with hunter.trace(function="b"):
            with hunter.trace(function="c"):
                assert sys.gettrace().handler == When(Q(function="c"),
                                                      CallPrinter)
            assert sys.gettrace().handler == When(Q(function="b"), CallPrinter)
        assert sys.gettrace().handler == When(Q(function="a"), CallPrinter)
예제 #5
0
def test_depth_limit(LineMatcher, tracer_impl, depth):
    buff = StringIO()
    from sample7 import one
    tracer = tracer_impl()
    predicate = When(Q(depth_lt=depth), CallPrinter(stream=buff))
    try:
        tracer.trace(predicate)
        one()
    finally:
        tracer.stop()
    output = buff.getvalue()
    lm = LineMatcher(output.splitlines())
    lm.fnmatch_lines([
        "* call      => one()",
        "* line         for i in range(1):  # one",
        "* line         two()",
        "* call         => two()",
        "* return       <= two: None",
        "* line         for i in range(1):  # one",
        "* return    <= one: None",
    ])
    if depth < 3:
        assert 'three' not in output
    if depth < 4:
        assert 'four' not in output
    if depth < 5:
        assert 'five' not in output
예제 #6
0
def test_predicate_when():
    called = []
    assert When(Q(module=1), lambda ev: called.append(ev))({
        'module': 2
    }) == False
    assert called == []

    assert When(Q(module=1), lambda ev: called.append(ev))({
        'module': 1
    }) == True
    assert called == [{'module': 1}]

    called = []
    assert Q(module=1, action=lambda ev: called.append(ev))({
        'module': 1
    }) == True
    assert called == [{'module': 1}]

    called = [[], []]
    predicate = (Q(module=1, action=lambda ev: called[0].append(ev))
                 | Q(module=2, action=lambda ev: called[1].append(ev)))
    assert predicate({'module': 1}) == True
    assert called == [[{'module': 1}], []]

    assert predicate({'module': 2}) == True
    assert called == [[{'module': 1}], [{'module': 2}]]

    called = [[], []]
    predicate = (Q(module=1, action=lambda ev: called[0].append(ev))
                 & Q(function=2, action=lambda ev: called[1].append(ev)))
    assert predicate({'module': 2}) == False
    assert called == [[], []]

    assert predicate({'module': 1, 'function': 2}) == True
    assert called == [[{
        'module': 1,
        'function': 2
    }], [{
        'module': 1,
        'function': 2
    }]]
예제 #7
0
def test_trace_api_expansion():
    # simple use
    with trace(function="foobar") as t:
        assert t.handler == When(Q(function="foobar"), CallPrinter)

    # "or" by expression
    with trace(module="foo", function="foobar") as t:
        assert t.handler == When(Q(module="foo", function="foobar"),
                                 CallPrinter)

    # pdb.set_trace
    with trace(function="foobar", action=Debugger) as t:
        assert str(t.handler) == str(When(Q(function="foobar"), Debugger))

    # pdb.set_trace on any hits
    with trace(module="foo", function="foobar", action=Debugger) as t:
        assert str(t.handler) == str(
            When(Q(module="foo", function="foobar"), Debugger))

    # pdb.set_trace when function is foobar, otherwise just print when module is foo
    with trace(Q(function="foobar", action=Debugger), module="foo") as t:
        assert str(t.handler) == str(
            When(And(When(Q(function="foobar"), Debugger), Q(module="foo")),
                 CallPrinter))

    # dumping variables from stack
    with trace(Q(function="foobar", action=VarsPrinter("foobar")),
               module="foo") as t:
        assert str(t.handler) == str(
            When(
                And(
                    When(Q(function="foobar"), VarsPrinter("foobar")),
                    Q(module="foo"),
                ), CallPrinter))

    with trace(Q(function="foobar", action=VarsPrinter("foobar",
                                                       "mumbojumbo")),
               module="foo") as t:
        assert str(t.handler) == str(
            When(
                And(
                    When(Q(function="foobar"),
                         VarsPrinter("foobar", "mumbojumbo")),
                    Q(module="foo"),
                ), CallPrinter))

    # multiple actions
    with trace(Q(function="foobar", actions=[VarsPrinter("foobar"), Debugger]),
               module="foo") as t:
        assert str(t.handler) == str(
            When(
                And(
                    When(Q(function="foobar"), VarsPrinter("foobar"),
                         Debugger),
                    Q(module="foo"),
                ), CallPrinter))