Esempio n. 1
0
def test_od_setdefault():
    od = OrderedDict([('a', 'b')])

    assert od.setdefault('foo') is None
    assert od.keys() == ['a', 'foo']

    assert od.setdefault('bar', 'baz') == 'baz'
    assert od.keys() == ['a', 'foo', 'bar']

    assert od.setdefault('a') == 'b'
    assert od == {'a': 'b', 'foo': None, 'bar': 'baz'}
def test_od_setdefault():
    od = OrderedDict([("a", "b")])

    assert od.setdefault("foo") is None
    assert od.keys() == ["a", "foo"]

    assert od.setdefault("bar", "baz") == "baz"
    assert od.keys() == ["a", "foo", "bar"]

    assert od.setdefault("a") == "b"
    assert od == {"a": "b", "foo": None, "bar": "baz"}
def test_setdefault():

    pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
    shuffle(pairs)
    od = OrderedDict(pairs)
    pair_order = list(od.items())
    assert od.setdefault('a', 10) == 3
    # make sure order didn't change
    assert list(od.items()) == pair_order
    assert od.setdefault('x', 10) == 10
    # make sure 'x' is added to the end
    assert list(od.items())[-1] == ('x', 10)

    # make sure setdefault still works when __missing__ is defined
    class Missing(OrderedDict):
        def __missing__(self, key):
            return 0
    assert Missing().setdefault(5, 9) == 9
Esempio n. 4
0
def test_setdefault():

    pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
    shuffle(pairs)
    od = OrderedDict(pairs)
    pair_order = list(od.items())
    assert od.setdefault('a', 10) == 3
    # make sure order didn't change
    assert list(od.items()) == pair_order
    assert od.setdefault('x', 10) == 10
    # make sure 'x' is added to the end
    assert list(od.items())[-1] == ('x', 10)

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

    assert Missing().setdefault(5, 9) == 9