Skip to content

Commit

Permalink
TASK: Implement a method to create a list from the input
Browse files Browse the repository at this point in the history
  • Loading branch information
d-Rickyy-b committed Oct 10, 2019
1 parent 1313df6 commit e935122
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pastepwn/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
from .dictwrapper import DictWrapper
from .threadingutils import start_thread, join_threads
from .templatingengine import TemplatingEngine
from .listify import listify

__all__ = ('Request', 'DictWrapper', 'start_thread', 'join_threads', 'TemplatingEngine')
__all__ = ('Request',
'DictWrapper',
'start_thread',
'join_threads',
'TemplatingEngine',
'listify')
19 changes: 19 additions & 0 deletions pastepwn/util/listify.py
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]
27 changes: 27 additions & 0 deletions pastepwn/util/tests/listify_test.py
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()

0 comments on commit e935122

Please sign in to comment.