コード例 #1
0
def test_callback_closure():
    checklist = []

    WatchableThing = expose_as("WatchableThing", Thing, "func")
    wt = WatchableThing()
    spectator = watch(wt)

    callbacks_called = [0, 0]

    def callback(inst, call):
        callbacks_called[0] += 1

        def closure(value):
            callbacks_called[1] += 1
            assert checklist[-1] == Data(name=call.name,
                                         value=value,
                                         before=call)

        return closure

    spectator.callback("func", callback)

    check_answer(checklist, wt, "func", 1, 2, c=3)
    check_answer(checklist, wt, "func", 1, 2, d=3)
    check_answer(checklist, wt, "func", 1, 2, 3, 4, 5)
    check_answer(checklist, wt, "func", 1, 2, d=3, f=4)
    assert callbacks_called == [4, 4]
コード例 #2
0
def test_beforeback_afterback():
    checklist = []

    WatchableThing = expose_as("WatchableThing", Thing, "func")
    wt = WatchableThing()
    spectator = watch(wt)

    callbacks_called = [0, 0]

    # callback stores call information
    def beforeback(inst, call):
        callbacks_called[0] += 1
        return call

    def afterback(inst, answer):
        callbacks_called[1] += 1
        assert checklist[-1] == answer

    spectator.callback("func", before=beforeback, after=afterback)

    check_answer(checklist, wt, "func", 1, 2, c=3)
    check_answer(checklist, wt, "func", 1, 2, d=3)
    check_answer(checklist, wt, "func", 1, 2, 3, 4, 5)
    check_answer(checklist, wt, "func", 1, 2, d=3, f=4)
    assert callbacks_called == [4, 4]
コード例 #3
0
def test_watch():
    WatchableThing = expose_as("WatchableThing", Thing, "func")
    wt = WatchableThing()
    spectator = watch(wt)

    assert isinstance(spectator, Spectator)
    assert hasattr(wt, "_instance_spectator")
    assert wt._instance_spectator is spectator

    WatchableList = expose_as("WatchableList", list, "append")
    wl, spectator = watched(WatchableList, [1, 2, 3])

    assert wl == [1, 2, 3]
    assert isinstance(spectator, Spectator)
    assert hasattr(wl, "_instance_spectator")
    assert wl._instance_spectator is spectator