Skip to content

Commit

Permalink
Merge pull request #33 from domidimi/master
Browse files Browse the repository at this point in the history
docstring: Add decorator to format the docstrings
  • Loading branch information
txels committed Aug 11, 2015
2 parents 9e9998b + 26b7752 commit 7ca1ad2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def feed_data(func, new_name, *args, **kwargs):
def wrapper(self):
return func(self, *args, **kwargs)
wrapper.__name__ = new_name
# Try to call format on the docstring
if func.__doc__:
try:
wrapper.__doc__ = func.__doc__.format(*args, **kwargs)
except (IndexError, KeyError):
# Maybe the user has added some of the formating strings
# unintentionally in the docstring. Do not raise an exception as it
# could be that he is not aware of the formating feature.
pass
return wrapper


Expand Down
21 changes: 21 additions & 0 deletions test/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,24 @@ def test_dicts_extracted_into_kwargs(self, first, second, third):
@data(u'ascii', u'non-ascii-\N{SNOWMAN}')
def test_unicode(self, value):
self.assertIn(value, (u'ascii', u'non-ascii-\N{SNOWMAN}'))

@data(3, 4, 12, 23)
def test_larger_than_two_with_doc(self, value):
"""Larger than two with value {0}"""
self.assertTrue(larger_than_two(value))

@data(3, 4, 12, 23)
def test_doc_missing_args(self, value):
"""Missing args with value {0} and {1}"""
self.assertTrue(larger_than_two(value))

@data(3, 4, 12, 23)
def test_doc_missing_kargs(self, value):
"""Missing kargs with value {value} {value2}"""
self.assertTrue(larger_than_two(value))

@data([3, 2], [4, 3], [5, 3])
@unpack
def test_list_extracted_with_doc(self, first_value, second_value):
"""Extract into args with first value {} and second value {}"""
self.assertTrue(first_value > second_value)

0 comments on commit 7ca1ad2

Please sign in to comment.