Ejemplo n.º 1
0
def test_nested_indexing():
    """test that we can index a nested list with nl[1, 2, 3] syntax."""
    ne_list = NestableEventedList(NEST)
    # 110 -> '110' -> (1, 1, 0)
    indices = [tuple(int(x) for x in str(n)) for n in flatten(NEST)]
    for index in indices:
        assert ne_list[index] == int("".join(map(str, index)))
Ejemplo n.º 2
0
def test_nested_events(meth, group_index):
    ne_list = NestableEventedList(NEST)
    ne_list.events = Mock(wraps=ne_list.events)

    method_name, args, expected_events = meth
    method = getattr(ne_list[group_index], method_name)
    if method_name == 'index' and group_index != (1, 1):
        # the expected value only occurs in index (1, 1)
        with pytest.raises(ValueError):
            method(*args)
    else:
        # make sure we can call the method without error
        method(*args)

    # make sure the correct event type and number was emitted
    for call, expected in zip(ne_list.events.call_args_list, expected_events):
        event = call.args[0]
        assert event.type == expected
        if group_index == ():
            # in the root group, the index will be an int relative to root
            assert isinstance(event.index, int)
        else:
            assert event.index[:-1] == group_index
Ejemplo n.º 3
0
def test_arbitrary_child_events():
    """Test that any object that supports the events protocol bubbles events.

    If you add an object that implements the ``SupportsEvents`` Protocol
    (i.e. has an attribute ``events`` that is an ``EmitterGroup``), to a
    ``NestableEventedList``, then the parent container will re-emit those
    events (and this works recursively up to the root container).  The
    index/indices of each child(ren) that bubbled the event will be added
    to the event.

    See docstring of :ref:`NestableEventedList` for more info.
    """

    class E:
        events = EmitterGroup(test=None)

    # create a random object that emits events
    e_obj = E()
    # and two nestable evented lists
    root = NestableEventedList()
    b = NestableEventedList()
    # collect all events emitted by the root list
    observed = []
    root.events.connect(lambda e: observed.append(e))

    # now append a list to root
    root.append(b)
    # and append the event-emitter object to the nested list
    b.append(e_obj)
    # then have the deeply nested event-emitter actually emit an event
    e_obj.events.test(value="hi")

    # look at the (type, index, and value) of all of the events emitted by root
    # and make sure they match expectations
    obs = [(e.type, e.index, getattr(e, 'value', None)) for e in observed]
    expected = [
        ('inserting', 0, None),  # before we inserted b into root
        ('inserted', 0, b),  # after b was inserted into root
        ('inserting', (0, 0), None),  # before we inserted e_obj into b
        ('inserted', (0, 0), e_obj),  # after e_obj was inserted into b
        ('test', (0, 0), 'hi'),  # when e_obj emitted an event called "test"
    ]
    for o, e in zip(obs, expected):
        assert o == e
Ejemplo n.º 4
0
def test_nested_move_multiple(sources, dest, expectation):
    """Test that moving multiple indices works and emits right events."""
    ne_list = NestableEventedList([0, 1, [20, [210, 211], 22], 3, 4])
    ne_list.events = Mock(wraps=ne_list.events)
    ne_list.move_multiple(sources, dest)
    ne_list.events.reordered.assert_called_with(value=expectation)
Ejemplo n.º 5
0
def test_setting_nested_slice():
    ne_list = NestableEventedList(NEST)
    ne_list[(1, 1, 1, slice(2))] = [9, 10]
    assert tuple(ne_list[1, 1, 1]) == (9, 10, 1112)