Example #1
0
 def testNonOrderedDictMixin(self): #{{{
     '''Non-OrderedDictMixin will always fail equality test'''
     d = self.data
     other = dict(d)
     od = odict(d)
     self.assertNotEqual(od, other)
     other = dict()
     od = odict()
     self.assertNotEqual(od, other)
Example #2
0
 def test_fromkeys(self): #{{{
     '''Test fromkeys'''
     od = odict(self.data)
     exp = odict((k, 42) for k in xrange(10))
     fod = od.fromkeys(xrange(10), 42)
     self.assertTrue(od is not fod)
     self.assertTrue(isinstance(fod, odict))
     self.assertNotEqual(fod, od)
     self.assertEqual(fod, exp)
Example #3
0
 def testCompareKeys(self): #{{{
     '''If equal dictionaries, test equality of _keys vars'''
     d = self.data
     other = odict()
     od = odict()
     self.assertEqual(od, other)
     other = odict(d)
     od = odict(d)
     self.assertEqual(od, other)
     other._keys[:] = []
     self.assertNotEqual(od, other)
Example #4
0
 def test_itervalues(self): #{{{
     '''Return list of values in order key/value pairs were added'''
     d = self.data
     expected = list(v for k, v in d)
     od = odict(d)
     self.assertEqual(list(od.itervalues()), expected)
     self.assertEqual(od.values(), expected)
Example #5
0
 def testStrRepr(self): #{{{
     '''String repr shows key/value pairs in order'''
     d = self.data
     expected = '{%s}'
     inner = ', '.join('%s: %s' %(repr(k), repr(v)) for k, v in d)
     expected = expected %inner
     self.assertEqual(str(odict(d)), expected)
Example #6
0
 def test_update_kw_overrides_args(self): #{{{
     '''Keyword arguments overrides args to update()'''
     od = odict()
     kw = dict(a=42, e=52, c=62)
     od.update(self.data, **kw)
     for k, v in kw.iteritems():
         self.assertEqual(od[k], v)
Example #7
0
 def test_copy(self): #{{{
     '''Make a shallow copy'''
     od = odict(self.data)
     for cfunc in ('copy', '__copy__'):
         new = getattr(od, cfunc)()
         self.assertEqual(od, new)
         self.assertNotEqual(id(od), id(new))
         self.assertTrue(isinstance(new, odict))
Example #8
0
 def test_popitem(self): #{{{
     '''Pop last added item'''
     od = odict(self.data)
     ret = od.popitem()
     self.assertEqual(ret, self.data[-1])
     self.assertEqual(len(od), len(self.data)-1)
     self.assertTrue(ret not in od._keys)
     self.assertEqual(od._keys, [k for k, v in self.data[:-1]])
Example #9
0
 def test_setdefault(self): #{{{
     '''Similar to setitem'''
     od = odict()
     ret = od.setdefault('a', 42)
     self.assertEqual(ret, 42)
     self.assertTrue('a' in od._keys)
     self.assertTrue('a' in od)
     self.assertEqual(od['a'], 42)
Example #10
0
 def test_iterkeys(self): #{{{
     '''Iterate keys in order key/value pairs were added'''
     d = self.data
     od = odict(d)
     expected = list(k for k, v in d)
     self.assertEqual(list(od.iterkeys()), expected)
     self.assertEqual(list(od), expected)
     self.assertEqual(od.keys(), expected)
Example #11
0
 def test_setitem(self): #{{{
     '''Only add to _keys list if key doesn't exist in the list'''
     od = odict()
     self.assertEqual(od._keys, [])
     od['a'] = 1
     self.assertEqual(od._keys, ['a'])
     od['c'] = 2
     od['e'] = 3
     od['a'] = 4
     self.assertEqual(od._keys, ['a', 'c', 'e'])
Example #12
0
 def test_pop(self): #{{{
     '''Popping removes key from _keys'''
     m = dict(self.data)
     od = odict(self.data)
     diff = 0
     for i in ('a', 'd'):
         ret = od.pop(i)
         self.assertEqual(ret, m[i])
         self.assertTrue(i not in od._keys)
         self.assertTrue(i not in od)
         diff += 1
         self.assertEqual(len(od), len(self.data)-diff)
Example #13
0
 def test_setitem_error(self): #{{{
     '''Error setting item does not set _keys list'''
     od = odict()
     self.assertRaisesEx(TypeError, od.__setitem__, [], 1)
     self.assertEqual(od._keys, [])
Example #14
0
 def test_clear(self): #{{{
     '''Clears dict and _keys'''
     od = odict(self.data)
     od.clear()
     self.assertFalse(od)
     self.assertFalse(od._keys)
Example #15
0
 def test_iteritems(self): #{{{
     '''Return list of items in order key/value pairs were added'''
     d = self.data
     od = odict(d)
     self.assertEqual(list(od.iteritems()), d)
     self.assertEqual(od.items(), d)
Example #16
0
 def test_pop_removekey(self): #{{{
     '''Remove key from _keys'''
     od = odict(self.data)
     od._pop_removekey('a')
     self.assertTrue('a' not in od._keys)
Example #17
0
 def test_update_onearg(self): #{{{
     '''Passing more than one arg to update() is bad'''
     od = odict()
     regex = re.compile(r'odict expected at most 1 argument, got 2')
     self.assertRaisesEx(TypeError, od.update, 1, 2, exc=regex)
 def testEmptyDict(self): #{{{
     '''Create an empty dict'''
     od = odict()
     self.assertFalse(od)
     self.assertEqual(len(od), 0)
     self.assertEqual(len(list(dict.__iter__(od))), 0)
Example #19
0
 def test_add_key(self): #{{{
     '''Adds to _keys list'''
     od = odict()
     od._add_key(1)
     self.assertEqual(od._keys, [1])
Example #20
0
 def test_setitem_keycheck(self): #{{{
     '''True if key is not in _keys, False otherwise'''
     od = odict()
     self.assertTrue(od._setitem_keycheck(1))
     od[1] = 'a'
     self.assertFalse(od._setitem_keycheck(1))
 def testCreateVars(self): #{{{
     '''Init creates '_keys' empty list private variable'''
     od = odict()
     self.assertTrue(hasattr(od, '_keys'))
     self.assertTrue(isinstance(od._keys, list))
     self.assertEqual(len(od._keys), 0)