Skip to content

Commit

Permalink
#3 test_example.py - added tests for @unpack decorator for unpacking …
Browse files Browse the repository at this point in the history
…lists, tuples and dictionary

      ddt.py - added unpack decorator that sets UNPACK_ATTR for the decorated function
  • Loading branch information
azamattokhtaev committed Feb 8, 2014
1 parent 365ad64 commit c55bbf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 13 additions & 2 deletions ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
# store the path to JSON file
FILE_ATTR = '%file_path'

UNPACK_ATTR = '%unpack'

def unpack(func):
"""
Method decorator to add unpack feature
"""
setattr(func, UNPACK_ATTR, True)
return func

def data(*values):
"""
Expand Down Expand Up @@ -122,8 +130,11 @@ def _raise_ve(*args):
if hasattr(func, DATA_ATTR):
for v in getattr(func, DATA_ATTR):
test_name = mk_test_name(name, getattr(v, "__name__", v))
if type(v) is tuple:
setattr(cls, test_name, feed_data(func, *v))
if hasattr(func, UNPACK_ATTR):
if type(v) is tuple or type(v) is list:
setattr(cls, test_name, feed_data(func, *v))
else:
setattr(cls, test_name, feed_data(func, **v))
else:
setattr(cls, test_name, feed_data(func, v))
delattr(cls, name)
Expand Down
14 changes: 13 additions & 1 deletion test/test_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from ddt import ddt, data, file_data
from ddt import ddt, data, file_data, unpack
from test.mycode import larger_than_two, has_three_elements, is_a_greeting


Expand Down Expand Up @@ -40,9 +40,21 @@ def test_file_data_list(self, value):
self.assertTrue(is_a_greeting(value))

@data((3, 2), (4, 3), (5, 3))
@unpack
def test_tuples_extracted_into_multiple_arguments(self, first_value, second_value):
self.assertTrue(first_value > second_value)

@data([3, 2], [4, 3], [5, 3])
@unpack
def test_list_extracted_into_multiple_arguments(self, first_value, second_value):
self.assertTrue(first_value > second_value)

@unpack
@data({'first': 1, 'second': 3, 'third': 2}, {'first': 4, 'second': 6, 'third': 5})
def test_extraction_into_kwargs(self, first, second, third):
self.assertTrue(first < third < second)


@data(u'ascii', u'non-ascii-\N{SNOWMAN}')
def test_unicode(self, value):
self.assertIn(value, (u'ascii', u'non-ascii-\N{SNOWMAN}'))

0 comments on commit c55bbf5

Please sign in to comment.