Add new munge_lists function + unit test

This commit is contained in:
John Davis
2023-10-24 09:40:03 -04:00
parent d5493d4ad1
commit c6a0611546
2 changed files with 28 additions and 0 deletions
+11
View File
@@ -1882,3 +1882,14 @@ def enum_values(enum_class):
Values are in member definition order.
"""
return [value.value for value in enum_class.__members__.values()]
def munge_lists(listA: Any, listB: Any) -> List:
"""
Combine two lists into a single list.
Arguments can be None, non-lists, or lists. If an argument is None, it will
not be included in the returned list. If both arguments are None, an empty
list will be returned.
"""
return listify(listA) + listify(listB)
+17
View File
@@ -149,3 +149,20 @@ def test_enum_values():
B = "b"
assert util.enum_values(Stuff) == ["a", "c", "b"]
class NotFalsy:
"""Class requires explicit check for None"""
def __bool__(self):
raise Exception("not implemented")
def test_munge_lists():
foo, bar = NotFalsy(), NotFalsy()
assert util.munge_lists(foo, None) == [foo]
assert util.munge_lists(None, foo) == [foo]
assert util.munge_lists(foo, bar) == [foo, bar]
assert util.munge_lists([foo, bar], None) == [foo, bar]
assert util.munge_lists(None, [foo, bar]) == [foo, bar]
assert util.munge_lists(None, None) == []