Merge "ListType preserves the order of the input"
This commit is contained in:
commit
5a5d485c1d
@ -162,12 +162,15 @@ class ListType(wtypes.UserType):
|
||||
"""Validate and convert the input to a ListType.
|
||||
|
||||
: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(',')]
|
||||
# filter() to remove empty items
|
||||
# set() to remove duplicated items
|
||||
return list(set(filter(None, items)))
|
||||
items = []
|
||||
for v in six.text_type(value).split(','):
|
||||
v_norm = v.strip().lower()
|
||||
if v_norm and v_norm not in items:
|
||||
items.append(v_norm)
|
||||
return items
|
||||
|
||||
@staticmethod
|
||||
def frombasetype(value):
|
||||
|
@ -277,14 +277,14 @@ class TestListType(base.TestCase):
|
||||
|
||||
def test_list_type(self):
|
||||
v = types.ListType()
|
||||
self.assertItemsEqual(['foo', 'bar'], v.validate('foo,bar'))
|
||||
self.assertItemsEqual(['cat', 'meow'], v.validate("cat , meow"))
|
||||
self.assertItemsEqual(['spongebob', 'squarepants'],
|
||||
v.validate("SpongeBob,SquarePants"))
|
||||
self.assertItemsEqual(['foo', 'bar'],
|
||||
v.validate("foo, ,,bar"))
|
||||
self.assertItemsEqual(['foo', 'bar'],
|
||||
v.validate("foo,foo,foo,bar"))
|
||||
self.assertEqual(['foo', 'bar'], v.validate('foo,bar'))
|
||||
self.assertNotEqual(['bar', 'foo'], v.validate('foo,bar'))
|
||||
|
||||
self.assertEqual(['cat', 'meow'], v.validate("cat , meow"))
|
||||
self.assertEqual(['spongebob', 'squarepants'],
|
||||
v.validate("SpongeBob,SquarePants"))
|
||||
self.assertEqual(['foo', 'bar'], v.validate("foo, ,,bar"))
|
||||
self.assertEqual(['foo', 'bar'], v.validate("foo,foo,foo,bar"))
|
||||
self.assertIsInstance(v.validate('foo,bar'), list)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user