Beispiel #1
0
 def test_fromkeys(self):
     x = ordereddict.fromkeys([1,2,3,4,5,6])
     assert len(x) == 6
     assert x[6] is None
     x = ordereddict.fromkeys((1,2,3,4,5), 'abc')
     assert len(x) == 5
     assert x[5] == 'abc'
     assert x.keys() == [1,2,3,4,5]
Beispiel #2
0
 def test_fromkeys(self):
     x = ordereddict.fromkeys([1, 2, 3, 4, 5, 6])
     assert len(x) == 6
     assert x[6] is None
     x = ordereddict.fromkeys((1, 2, 3, 4, 5), 'abc')
     assert len(x) == 5
     assert x[5] == 'abc'
     assert x.keys() == [1, 2, 3, 4, 5]
Beispiel #3
0
    def test_fromkeys(self):
        od1 = ordereddict([
            ('a', None),
            ('b', None),
            ('c', None),
        ])
        self.assertEqual(ordereddict.fromkeys('abc'), od1)
        d = ordereddict()
        self.assert_(not (d.fromkeys('abc') is d))
        self.assertEqual(d.fromkeys('abc'), od1)
        self.assertEqual(d.fromkeys((4, 5), 0), ordereddict(((4, 0), (5, 0))))
        self.assertEqual(d.fromkeys([]), {})

        def g():
            yield 1

        self.assertEqual(d.fromkeys(g()), ordereddict(((1, None), )))
        self.assertRaises(TypeError, {}.fromkeys, 3)

        class dictlike(ordereddict):
            pass

        self.assertEqual(dictlike.fromkeys('a'), {'a': None})
        self.assertEqual(dictlike().fromkeys('a'), {'a': None})
        self.assert_(type(dictlike.fromkeys('a')) is dictlike)
        self.assert_(type(dictlike().fromkeys('a')) is dictlike)

        class mydict(ordereddict):
            def __new__(cls):
                return UserDict.UserDict()

        ud = mydict.fromkeys('ab')
        self.assertEqual(ud, {'a': None, 'b': None})
        self.assert_(isinstance(ud, UserDict.UserDict))
        self.assertRaises(TypeError, dict.fromkeys)

        class Exc(Exception):
            pass

        class baddict1(dict):
            def __init__(self):
                raise Exc()

        self.assertRaises(Exc, baddict1.fromkeys, [1])

        class BadSeq(object):
            def __iter__(self):
                return self

            def next(self):
                raise Exc()

        self.assertRaises(Exc, ordereddict.fromkeys, BadSeq())

        class baddict2(ordereddict):
            def __setitem__(self, key, value):
                raise Exc()

        self.assertRaises(Exc, baddict2.fromkeys, [1])
Beispiel #4
0
    def test_fromkeys(self):
        od1 = ordereddict([('a', None),('b', None),('c', None),])
        self.assertEqual(ordereddict.fromkeys('abc'), od1)
        d = ordereddict()
        self.assert_(not(d.fromkeys('abc') is d))
        self.assertEqual(d.fromkeys('abc'), od1)
        self.assertEqual(d.fromkeys((4,5),0), ordereddict(((4,0), (5,0))))
        self.assertEqual(d.fromkeys([]), {})
        def g():
            yield 1
        self.assertEqual(d.fromkeys(g()), ordereddict(((1,None),)))
        self.assertRaises(TypeError, {}.fromkeys, 3)
        class dictlike(ordereddict): pass
        self.assertEqual(dictlike.fromkeys('a'), {'a':None})
        self.assertEqual(dictlike().fromkeys('a'), {'a':None})
        self.assert_(type(dictlike.fromkeys('a')) is dictlike)
        self.assert_(type(dictlike().fromkeys('a')) is dictlike)
        class mydict(ordereddict):
            def __new__(cls):
                return UserDict.UserDict()
        ud = mydict.fromkeys('ab')
        self.assertEqual(ud, {'a':None, 'b':None})
        self.assert_(isinstance(ud, UserDict.UserDict))
        self.assertRaises(TypeError, dict.fromkeys)

        class Exc(Exception): pass

        class baddict1(dict):
            def __init__(self):
                raise Exc()

        self.assertRaises(Exc, baddict1.fromkeys, [1])

        class BadSeq(object):
            def __iter__(self):
                return self
            def next(self):
                raise Exc()

        self.assertRaises(Exc, ordereddict.fromkeys, BadSeq())

        class baddict2(ordereddict):
            def __setitem__(self, key, value):
                raise Exc()

        self.assertRaises(Exc, baddict2.fromkeys, [1])