Exemplo n.º 1
0
def test_get_signal():
    class SignalTest(object):
        def __init__(self):
            super().__init__()
            self.event_signals = {"testing": []}

    t = SignalTest()
    assert hasattr(t, "event_signals") and "testing" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []

    t.event_signals["testing"].append(print)
    assert get_signal(t, "testing") == [print]

    def blah(*args):
        pass

    t.event_signals["testing"].append(blah)
    assert get_signal(t, "testing") == [print, blah]

    t.event_signals["testing"].remove(print)
    assert get_signal(t, "testing") == [blah]

    t.event_signals["testing"].remove(blah)
    assert get_signal(t, "testing") == []

    print("test_get_signal passed!")
Exemplo n.º 2
0
def test_fire_signal():
    class SignalTest(object):
        def __init__(self):
            super(SignalTest, self).__init__()
            self.event_signals = {"testing": []}

    t = SignalTest()
    assert hasattr(t, "event_signals") and "testing" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []

    test = []

    def testing(value1, value2):
        test.append((value1, value2))

    # Test connect and emit
    t.event_signals["testing"].append(testing)
    fire_signal(t, "testing", "abc", "123")
    assert test == [("abc", "123")]

    fire_signal(t, "testing", "Hello", "World!")
    assert test == [("abc", "123"), ("Hello", "World!")]

    print("test_fire_signal passed!")
Exemplo n.º 3
0
def test_off_signal():
    class SignalTest(object):
        def __init__(self):
            super(SignalTest, self).__init__()
            self.event_signals = {"testing": []}

    t = SignalTest()
    assert hasattr(t, "event_signals") and "testing" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []

    test = []

    def testing(value1, value2):
        test.append((value1, value2))

    # Test connect and emit
    t.event_signals["testing"].append(testing)
    t.event_signals["testing"][0]("abc", "123")
    assert test == [("abc", "123")]

    # Test disconnect
    existed = off_signal(t, "testing", testing)
    assert existed
    assert t.event_signals["testing"] == []

    existed = off_signal(t, "testing", testing)
    assert not existed

    print("test_off_signal passed!")
Exemplo n.º 4
0
def test_on_signal():
    class SignalTest(object):
        def __init__(self):
            super().__init__()
            self.event_signals = {"testing": []}

    t = SignalTest()
    assert hasattr(t, "event_signals") and "testing" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []

    test = []

    def testing(value1, value2):
        test.append((value1, value2))

    # Test connect and emit
    on_signal(t, "testing", testing)
    assert t.event_signals["testing"] == [testing]
    t.event_signals['testing'][0]("abc", "123")
    assert test == [("abc", "123")]

    # Test disconnect
    t.event_signals["testing"].remove(testing)
    assert t.event_signals["testing"] == []

    print("test_on_signal passed!")
Exemplo n.º 5
0
def test_get_signal():
    class SignalTest(object):
        def __init__(self):
            super(SignalTest, self).__init__()
            self.event_signals = {"testing": []}

    t = SignalTest()
    assert hasattr(t, "event_signals") and "testing" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []

    t.event_signals["testing"].append(print)
    assert get_signal(t, "testing") == [print]

    def blah(*args):
        pass

    t.event_signals["testing"].append(blah)
    assert get_signal(t, "testing") == [print, blah]

    t.event_signals["testing"].remove(print)
    assert get_signal(t, "testing") == [blah]

    t.event_signals["testing"].remove(blah)
    assert get_signal(t, "testing") == []

    try:
        get_signal(t, 'signal that does not exist')
        raise AssertionError(
            "get_signal should raise an error if the signal does not exist.")
    except SignalError:
        pass

    print("test_get_signal passed!")
Exemplo n.º 6
0
def test_block_signal():
    class SignalTest(object):
        def __init__(self):
            super(SignalTest, self).__init__()
            self.event_signals = {"testing": [], "test2": []}

    t = SignalTest()
    assert hasattr(
        t, "event_signals"
    ) and "testing" in t.event_signals and "test2" in t.event_signals
    assert get_signal(t, "testing") == []
    assert t.event_signals["testing"] == []
    assert t.event_signals["test2"] == []

    test = []
    test2 = []

    def testing(value1, value2):
        test.append((value1, value2))

    def test2_func(*args):
        test2.append(args)

    # Test connect and emit
    t.event_signals["testing"].append(testing)
    t.event_signals["test2"].append(test2_func)

    fire_signal(t, "testing", "abc", "123")
    assert test == [("abc", "123")]

    block_signals(t)
    fire_signal(t, "testing", "Hello", "World!")
    assert test == [("abc", "123")]

    block_signals(t, block=False)
    fire_signal(t, "testing", "Hello", "World!")
    assert test == [("abc", "123"), ("Hello", "World!")]

    block_signals(t, "test2", True)
    fire_signal(t, "testing", "Hello", "again")
    fire_signal(t, "test2", False)
    assert test == [("abc", "123"), ("Hello", "World!"), ("Hello", "again")]
    assert test2 == [], "Failed to block the right signal"

    block_signals(t, "test2", False)
    fire_signal(t, "testing", "Hello", "again")
    fire_signal(t, "test2", True)
    assert test == [("abc", "123"), ("Hello", "World!"), ("Hello", "again"),
                    ("Hello", "again")]
    assert test2 == [(True, )], "Failed to block the right signal"

    print("test_block_signal passed!")