-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Implement a method to create a list from the input
- Loading branch information
1 parent
1313df6
commit e935122
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
def listify(obj): | ||
""" | ||
Make sure the given object is a list | ||
:param obj: Any object - either None, a list of objects or a single object | ||
:return: The given object formatted as list | ||
""" | ||
if obj is None: | ||
# When the object is None, an empty list will be returned | ||
return [] | ||
elif isinstance(obj, list): | ||
# When the object is already a list, that list will be returned | ||
return obj | ||
else: | ||
# When a single object is passed to the method, a list with the | ||
# object as single item will be returned | ||
return [obj] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
from unittest.mock import Mock | ||
|
||
from pastepwn.util.listify import listify | ||
|
||
|
||
class ListifyTest(unittest.TestCase): | ||
|
||
def test_None(self): | ||
self.assertEqual([], listify(None), "Listify did not return empty list!") | ||
|
||
def test_list(self): | ||
obj = Mock() | ||
obj2 = Mock() | ||
obj3 = Mock() | ||
obj_list = [obj, obj2, obj3] | ||
|
||
self.assertEqual(obj_list, listify(obj_list), "Listify did not return the given list!") | ||
|
||
def test_single(self): | ||
obj = Mock() | ||
self.assertEqual([obj], listify(obj), "Listify did not return single object as list!") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |