2015-10-08 16:26:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import unittest
|
|
|
|
from nose import tools
|
|
|
|
|
|
|
|
from kitchen.pycompat24.sets import add_builtin_set
|
|
|
|
add_builtin_set()
|
|
|
|
|
|
|
|
from kitchen import collections
|
|
|
|
|
|
|
|
def test_strict_dict_get_set():
|
|
|
|
'''Test getting and setting items in StrictDict'''
|
|
|
|
d = collections.StrictDict()
|
2016-07-08 23:18:01 +00:00
|
|
|
d['a'] = 1
|
|
|
|
d[b'a'] = 2
|
|
|
|
tools.assert_not_equal(d['a'], d[b'a'])
|
|
|
|
tools.eq_(len(d), 2)
|
|
|
|
|
|
|
|
d['\xf1'] = 1
|
|
|
|
d[b'\xf1'] = 2
|
|
|
|
d['\xf1'.encode('utf-8')] = 3
|
|
|
|
tools.eq_(d['\xf1'], 1)
|
|
|
|
tools.eq_(d[b'\xf1'], 2)
|
|
|
|
tools.eq_(d['\xf1'.encode('utf-8')], 3)
|
|
|
|
tools.eq_(len(d), 5)
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
class TestStrictDict(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.d = collections.StrictDict()
|
2016-07-08 23:18:01 +00:00
|
|
|
self.d['a'] = 1
|
|
|
|
self.d[b'a'] = 2
|
|
|
|
self.d['\xf1'] = 1
|
|
|
|
self.d[b'\xf1'] = 2
|
|
|
|
self.d['\xf1'.encode('utf-8')] = 3
|
|
|
|
self.keys = ['a', b'a', '\xf1', b'\xf1', '\xf1'.encode('utf-8')]
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
del(self.d)
|
|
|
|
|
|
|
|
def _compare_lists(self, list1, list2, debug=False):
|
2016-07-08 23:18:01 +00:00
|
|
|
'''We have a mixture of bytes and unicode so we have to compare these
|
|
|
|
lists manually and inefficiently
|
2015-10-08 16:26:18 +00:00
|
|
|
'''
|
|
|
|
def _compare_lists_helper(compare_to, dupes, idx, length):
|
|
|
|
if i not in compare_to:
|
|
|
|
return False
|
|
|
|
for n in range(1, length + 1):
|
|
|
|
if i not in dupes[n][idx]:
|
|
|
|
dupes[n][idx].add(i)
|
|
|
|
return True
|
|
|
|
if len(list1) != len(list2):
|
|
|
|
return False
|
|
|
|
|
|
|
|
list1_dupes = dict([(i, (set(), set(), set())) for i in range(1, len(list1)+1)])
|
|
|
|
list2_dupes = dict([(i, (set(), set(), set())) for i in range(1, len(list1)+1)])
|
|
|
|
|
2016-07-08 23:18:01 +00:00
|
|
|
list1_u = [l for l in list1 if isinstance(l, str)]
|
|
|
|
list1_b = [l for l in list1 if isinstance(l, bytes)]
|
|
|
|
list1_o = [l for l in list1 if not (isinstance(l, (str, bytes)))]
|
2015-10-08 16:26:18 +00:00
|
|
|
|
2016-07-08 23:18:01 +00:00
|
|
|
list2_u = [l for l in list2 if isinstance(l, str)]
|
|
|
|
list2_b = [l for l in list2 if isinstance(l, bytes)]
|
|
|
|
list2_o = [l for l in list2 if not (isinstance(l, (str, bytes)))]
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
for i in list1:
|
2016-07-08 23:18:01 +00:00
|
|
|
if isinstance(i, str):
|
2015-10-08 16:26:18 +00:00
|
|
|
if not _compare_lists_helper(list2_u, list1_dupes, 0, len(list1)):
|
|
|
|
return False
|
2016-07-08 23:18:01 +00:00
|
|
|
elif isinstance(i, bytes):
|
2015-10-08 16:26:18 +00:00
|
|
|
if not _compare_lists_helper(list2_b, list1_dupes, 1, len(list1)):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not _compare_lists_helper(list2_o, list1_dupes, 2, len(list1)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if list1_dupes[2][0] or list1_dupes[2][1] or list1_dupes[2][2]:
|
|
|
|
for i in list2:
|
2016-07-08 23:18:01 +00:00
|
|
|
if isinstance(i, str):
|
2015-10-08 16:26:18 +00:00
|
|
|
if not _compare_lists_helper(list1_u, list2_dupes, 0, len(list1)):
|
|
|
|
return False
|
2016-07-08 23:18:01 +00:00
|
|
|
elif isinstance(i, bytes):
|
2015-10-08 16:26:18 +00:00
|
|
|
if not _compare_lists_helper(list1_b, list2_dupes, 1, len(list1)):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not _compare_lists_helper(list1_o, list2_dupes, 2, len(list1)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
for i in range(2, len(list1)+1):
|
|
|
|
for n in list1_dupes[i]:
|
|
|
|
if n not in list2_dupes[i]:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def test__compare_list(self):
|
|
|
|
'''*sigh* this test support function is so complex we need to test it'''
|
|
|
|
tools.ok_(self._compare_lists(['a', 'b', 'c'], ['c', 'a', 'b']))
|
|
|
|
tools.ok_(not self._compare_lists(['b', 'c'], ['c', 'a', 'b']))
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(not self._compare_lists([b'a', 'b'], ['a', 'b']))
|
|
|
|
tools.ok_(not self._compare_lists(['a', b'b'], [b'a', 'b']))
|
2015-10-08 16:26:18 +00:00
|
|
|
tools.ok_(self._compare_lists(['a', 'b', 1], ['a', 1, 'b']))
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(self._compare_lists([b'a', b'b'], [b'a', b'b']))
|
|
|
|
tools.ok_(self._compare_lists([b'a', 'b'], [b'a', 'b']))
|
|
|
|
tools.ok_(not self._compare_lists([b'a', 'b'], [b'a', b'b']))
|
|
|
|
tools.ok_(self._compare_lists([b'a', 'b', 'b', 'c', b'a'], [b'a', b'a', 'b', 'c', 'b']))
|
|
|
|
tools.ok_(not self._compare_lists([b'a', 'b', 'b', 'c', 'a'], [b'a', b'a', 'b', 'c', 'b']))
|
|
|
|
tools.ok_(not self._compare_lists([b'a', 'b', 'b', 'c', b'a'], [b'a', 'b', 'b', 'c', 'b']))
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
def test_strict_dict_len(self):
|
|
|
|
'''StrictDict len'''
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.eq_(len(self.d), 5)
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
def test_strict_dict_del(self):
|
|
|
|
'''StrictDict del'''
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.eq_(len(self.d), 5)
|
|
|
|
del(self.d['\xf1'])
|
|
|
|
tools.assert_raises(KeyError, self.d.__getitem__, '\xf1')
|
|
|
|
tools.eq_(len(self.d), 4)
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
def test_strict_dict_iter(self):
|
|
|
|
'''StrictDict iteration'''
|
|
|
|
keys = []
|
|
|
|
for k in self.d:
|
|
|
|
keys.append(k)
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(self._compare_lists(keys, self.keys),
|
|
|
|
msg='keys != self.key: %s != %s' % (keys, self.keys))
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
keys = []
|
2016-07-08 23:18:01 +00:00
|
|
|
for k in self.d.keys():
|
2015-10-08 16:26:18 +00:00
|
|
|
keys.append(k)
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(self._compare_lists(keys, self.keys),
|
|
|
|
msg='keys != self.key: %s != %s' % (keys, self.keys))
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
keys = [k for k in self.d]
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(self._compare_lists(keys, self.keys),
|
|
|
|
msg='keys != self.key: %s != %s' % (keys, self.keys))
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
keys = []
|
2016-07-08 23:18:01 +00:00
|
|
|
for k in list(self.d.keys()):
|
2015-10-08 16:26:18 +00:00
|
|
|
keys.append(k)
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(self._compare_lists(keys, self.keys),
|
|
|
|
msg='keys != self.key: %s != %s' % (keys, self.keys))
|
2015-10-08 16:26:18 +00:00
|
|
|
|
|
|
|
def test_strict_dict_contains(self):
|
|
|
|
'''StrictDict contains function'''
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(b'b' not in self.d)
|
2015-10-08 16:26:18 +00:00
|
|
|
tools.ok_('b' not in self.d)
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(b'\xf1' in self.d)
|
2015-10-08 16:26:18 +00:00
|
|
|
tools.ok_('\xf1' in self.d)
|
2016-07-08 23:18:01 +00:00
|
|
|
tools.ok_(b'a' in self.d)
|
2015-10-08 16:26:18 +00:00
|
|
|
tools.ok_('a' in self.d)
|
|
|
|
|
2016-07-08 23:18:01 +00:00
|
|
|
del(self.d['\xf1'])
|
|
|
|
tools.ok_('\xf1' not in self.d)
|
|
|
|
tools.ok_(b'\xf1' in self.d)
|
2015-10-08 16:26:18 +00:00
|
|
|
|
2016-07-08 23:18:01 +00:00
|
|
|
del(self.d[b'a'])
|
|
|
|
tools.ok_('a' in self.d)
|
|
|
|
tools.ok_(b'a' not in self.d)
|