Merge "ListType preserves the order of the input"

This commit is contained in:
Jenkins 2017-10-10 23:28:03 +00:00 committed by Gerrit Code Review
commit 5a5d485c1d
2 changed files with 16 additions and 13 deletions

View File

@ -162,12 +162,15 @@ class ListType(wtypes.UserType):
"""Validate and convert the input to a ListType. """Validate and convert the input to a ListType.
:param value: A comma separated string of values :param value: A comma separated string of values
:returns: A list of unique values, whose order is not guaranteed. :returns: A list of unique values (lower-cased), maintaining the
same order
""" """
items = [v.strip().lower() for v in six.text_type(value).split(',')] items = []
# filter() to remove empty items for v in six.text_type(value).split(','):
# set() to remove duplicated items v_norm = v.strip().lower()
return list(set(filter(None, items))) if v_norm and v_norm not in items:
items.append(v_norm)
return items
@staticmethod @staticmethod
def frombasetype(value): def frombasetype(value):

View File

@ -277,14 +277,14 @@ class TestListType(base.TestCase):
def test_list_type(self): def test_list_type(self):
v = types.ListType() v = types.ListType()
self.assertItemsEqual(['foo', 'bar'], v.validate('foo,bar')) self.assertEqual(['foo', 'bar'], v.validate('foo,bar'))
self.assertItemsEqual(['cat', 'meow'], v.validate("cat , meow")) self.assertNotEqual(['bar', 'foo'], v.validate('foo,bar'))
self.assertItemsEqual(['spongebob', 'squarepants'],
self.assertEqual(['cat', 'meow'], v.validate("cat , meow"))
self.assertEqual(['spongebob', 'squarepants'],
v.validate("SpongeBob,SquarePants")) v.validate("SpongeBob,SquarePants"))
self.assertItemsEqual(['foo', 'bar'], self.assertEqual(['foo', 'bar'], v.validate("foo, ,,bar"))
v.validate("foo, ,,bar")) self.assertEqual(['foo', 'bar'], v.validate("foo,foo,foo,bar"))
self.assertItemsEqual(['foo', 'bar'],
v.validate("foo,foo,foo,bar"))
self.assertIsInstance(v.validate('foo,bar'), list) self.assertIsInstance(v.validate('foo,bar'), list)