Example #1
0
def test_pop():

    pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
    shuffle(pairs)
    od = OrderedDict(pairs)
    shuffle(pairs)
    while pairs:
        k, v = pairs.pop()
        assert od.pop(k) == v
    with pytest.raises(KeyError):
        od.pop('xyz')
    assert len(od) == 0
    assert od.pop(k, 12345) == 12345

    # make sure pop still works when __missing__ is defined
    class Missing(OrderedDict):
        def __missing__(self, key):
            return 0

    m = Missing(a=1)
    assert m.pop('b', 5) == 5
    assert m.pop('a', 6) == 1
    assert m.pop('a', 6) == 6
    with pytest.raises(KeyError):
        m.pop('a')
Example #2
0
def test_od_pop():
    od = OrderedDict([('a', 'b'), ('c', 'd'), ('foo', 'bar')])

    assert od.pop('a') == 'b'
    assert 'a' not in od
    assert od.keys() == ['c', 'foo']

    assert od.pop('bar', 1) == 1

    with pytest.raises(KeyError):
        od.pop('bar')
def test_od_pop():
    od = OrderedDict([("a", "b"), ("c", "d"), ("foo", "bar")])

    assert od.pop("a") == "b"
    assert "a" not in od
    assert od.keys() == ["c", "foo"]

    assert od.pop("bar", 1) == 1

    with pytest.raises(KeyError):
        od.pop("bar")
def test_pop():

    pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
    shuffle(pairs)
    od = OrderedDict(pairs)
    shuffle(pairs)
    while pairs:
        k, v = pairs.pop()
        assert od.pop(k) == v
    with pytest.raises(KeyError):
        od.pop('xyz')
    assert len(od) == 0
    assert od.pop(k, 12345) == 12345

    # make sure pop still works when __missing__ is defined
    class Missing(OrderedDict):
        def __missing__(self, key):
            return 0
    m = Missing(a=1)
    assert m.pop('b', 5) == 5
    assert m.pop('a', 6) == 1
    assert m.pop('a', 6) == 6
    with pytest.raises(KeyError):
        m.pop('a')