def test_clear(self): from cftt.common.base import _DecodedDict test1 = _DecodedDict(dict((('あ', 1), ('a', 2)))) test2 = _DecodedDict() self.assertNotEqual(test1._elements, test2._elements) test1.clear() self.assertEqual(test1._elements, test2._elements)
def test__init__(self): from cftt.common.base import _DecodedDict test = _DecodedDict() self.assertEqual(test._elements, {}) test = _DecodedDict(((1, 2), ('a', 3))) self.assertEqual(test._elements, {u'1': 2, u'a': 3}) test = _DecodedDict(((1, 2), ('a', 3)), i=1, j=2) self.assertEqual(test._elements, {u'1': 2, u'a': 3, u'i': 1, u'j': 2})
def test_pop(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 2)))) decoded = 'あ'.decode('utf-8') self.assertEqual(test.pop(['a', 'x'], 8), 8) self.assertEqual(test._elements, {decoded: 1, 'a': 2}) self.assertEqual(test.pop(['a'], 9), [2])
def test__ne__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 2)))) self.assertFalse(test.__ne__(dict((('あ', 1), ('a', 2))))) self.assertTrue(test.__ne__(dict((('あ', 2), ('a', 2))))) self.assertFalse(test != dict((('あ', 1), ('a', 2)))) self.assertTrue(test != dict((('あ', 2), ('a', 2))))
def test__contains__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 2)))) self.assertTrue('a' in test) self.assertTrue(test.__contains__('a')) self.assertTrue('あ' in test) self.assertFalse('b' in test) self.assertTrue(test.__contains__(('a', 'あ'))) self.assertFalse(test.__contains__(('a', 'b')))
def test_items(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 2)))) decoded = 'あ'.decode('utf-8') self.assertEqual(set(test.items()), set(((decoded, 1), ('a', 2)))) del test['あ'] self.assertEqual(set(test.items()), set((('a', 2), ))) test.items()[0] = ('a', 0) self.assertEqual(test._elements, {'a': 2})
def test_popitem(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 'エイ')))) self.assertEqual(len(test._elements), 2) i1 = test.popitem() self.assertEqual(len(test._elements), 1) i2 = test.popitem() self.assertEqual(len(test._elements), 0) self.assertNotEqual(i1, i2) self.assertRaises(KeyError, test.popitem)
def test__call__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(a=1, b=2) self.assertEqual(test._elements, dict(a=1, b=2)) self.assertEqual(test('a'), 1) self.assertEqual(test(['a', 'b']), [1, 2]) self.assertEqual(test(('b', 'b')), (2, 2)) self.assertEqual(test(set(('b', 'a'))), dict(a=1, b=2)) self.assertEqual(test('b', 'a')._elements, dict(a=1, b='a')) self.assertEqual(test(dict(c=0))._elements, dict(a=1, b='a', c=0)) self.assertEqual(test(dict(c=None))._elements, dict(a=1, b='a'))
def test__delitem__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(a=1, b=2, c=3, d=4, e=5, f=6) self.assertEqual(test._elements, dict(a=1, b=2, c=3, d=4, e=5, f=6)) del test['a'] self.assertEqual(test._elements, dict(b=2, c=3, d=4, e=5, f=6)) del test['a'] self.assertEqual(test._elements, dict(b=2, c=3, d=4, e=5, f=6)) del test[('b', 'c')] self.assertEqual(test._elements, dict(d=4, e=5, f=6)) del test[{'k': 'f'}] self.assertEqual(test._elements, dict(d=4, e=5))
def test__setitem__(self): from cftt.common.base import _DecodedDict test = _DecodedDict({'a': 1, 'b': (1, 2, 3), 'あ': 3}) decoded = 'あ'.decode('utf-8') self.assertEqual(test._elements['a'], 1) self.assertNotIn('c', test._elements) test[{'k1': 'a', 'k2': 'c'}] = 4 self.assertEqual(test._elements['a'], 4) self.assertEqual(test._elements['c'], 4) test['c'] = None self.assertNotIn('c', test._elements) test['あ'] = 5 self.assertEqual(test._elements[decoded], 5) test['a'] = ('あ', 'い') self.assertEqual(test._elements['a'], (decoded, 'い'.decode('utf-8')))
def test_setdefault(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 'エイ')))) decoded1 = 'あ'.decode('utf-8') decoded2 = 'エイ'.decode('utf-8') self.assertEqual(decoded2, test._elements[u'a']) self.assertEqual(decoded2, test.setdefault('a')) self.assertEqual(1, test._elements[decoded1]) self.assertEqual(1, test.setdefault(decoded1)) self.assertEqual(1, test.setdefault('あ')) self.assertEqual(2, test.setdefault('x', 2)) self.assertEqual(2, test._elements[u'x']) self.assertEqual([3, 3, 3], test.setdefault(['x', 'y', 'z'], 3)) self.assertEqual(3, test._elements['z']) self.assertIsNone(test.setdefault('w'))
def test__getitem__(self): from cftt.common.base import _DecodedDict test = _DecodedDict({'a': 1, 'b': (1, 2, 3), 'あ': 3}) decoded = 'あ'.decode('utf-8') self.assertEqual(test['a'], 1) self.assertEqual(test[u'a'], 1) self.assertEqual(test['b'], (1, 2, 3)) self.assertEqual(test[('a', 'b')], (1, (1, 2, 3))) self.assertEqual(test[decoded], 3) self.assertEqual(test['あ'], 3) self.assertEqual(test[set(('a', 'あ'))], {'a': 1, decoded: 3}) self.assertEqual(test[{'k1': 'a', 'k2': 'あ'}], {'k1': 1, 'k2': 3}) self.assertEqual(test[{ 'k': ('a', ['a'], set(('a', 'あ'))) }], {'k': (1, [1], { 'a': 1, decoded: 3 })})
def test__len__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(a=1, b=2) self.assertEqual(len(test), 2) del test['a'] self.assertEqual(len(test), 1)
def test__iter__(self): from cftt.common.base import _DecodedDict test = _DecodedDict(a=1, b=2) for x, y in zip(test, dict(a=1, b=2)): self.assertEqual(x, y)
def test_get(self): from cftt.common.base import _DecodedDict test = _DecodedDict(dict((('あ', 1), ('a', 2)))) self.assertEqual(test.get('a'), test['a']) self.assertNotEqual(test.get('x', 0), test['x'])