Fix test_while_is_not with python 3.11

The test_while_is_not function relied on strings and literals to test
the 'while_is_not' function. But the while_is_not function uses the 'is'
operator to test the equivalency of 2 objects.
This triggers a bug with python 3.11 where using 'is' with literals is
not advised (it is not recommended since python 3.8 [0]).
The test now uses objects from a specific class to evaluate the
while_is_not function.

[0] https://docs.python.org/3/whatsnew/3.8.html#changes-in-python-behavior

Change-Id: I38a0135aaf73e25aa20a11c0685d5c2a7b587a07
This commit is contained in:
Gregory Thiemonge 2022-12-05 18:18:43 +01:00
parent 012b7c5aa9
commit 5bcac4c7d4
1 changed files with 18 additions and 11 deletions

View File

@ -140,14 +140,21 @@ class IterUtilsTest(test.TestCase):
self.assertRaises(ValueError, iter_utils.while_is_not, 2, 'a')
def test_while_is_not(self):
it = iter(string.ascii_lowercase)
self.assertEqual(['a'],
list(iter_utils.while_is_not(it, 'a')))
it = iter(string.ascii_lowercase)
self.assertEqual(['a', 'b'],
list(iter_utils.while_is_not(it, 'b')))
self.assertEqual(list(string.ascii_lowercase[2:]),
list(iter_utils.while_is_not(it, 'zzz')))
it = iter(string.ascii_lowercase)
self.assertEqual(list(string.ascii_lowercase),
list(iter_utils.while_is_not(it, '')))
class Dummy(object):
def __init__(self, char):
self.char = char
dummy_list = [Dummy(a)
for a in string.ascii_lowercase]
it = iter(dummy_list)
self.assertEqual([dummy_list[0]],
list(iter_utils.while_is_not(it, dummy_list[0])))
it = iter(dummy_list)
self.assertEqual(dummy_list[0:2],
list(iter_utils.while_is_not(it, dummy_list[1])))
self.assertEqual(dummy_list[2:],
list(iter_utils.while_is_not(it, Dummy('zzz'))))
it = iter(dummy_list)
self.assertEqual(dummy_list,
list(iter_utils.while_is_not(it, Dummy(''))))