Skip to content

Commit

Permalink
Merge pull request #18 from elyezer/master
Browse files Browse the repository at this point in the history
Do not allow dots on test names
  • Loading branch information
txels committed Mar 20, 2014
2 parents ead6ec4 + b934bd5 commit 1b25f2a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
6 changes: 4 additions & 2 deletions ddt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import json
import os
import re
from functools import wraps

__version__ = '0.7.1'
Expand Down Expand Up @@ -67,12 +68,13 @@ def mk_test_name(name, value):
"""
try:
return "{0}_{1}".format(name, value)
test_name = "{0}_{1}".format(name, value)
except UnicodeEncodeError:
# fallback for python2
return "{0}_{1}".format(
test_name = "{0}_{1}".format(
name, value.encode('ascii', 'backslashreplace')
)
return re.sub('\W|^(?=\d)', '_', test_name)


def ddt(cls):
Expand Down
34 changes: 30 additions & 4 deletions test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def test_something(self, value):
return value


@ddt
class DummyInvalidIdentifier():
"""
Dummy class to test the data decorator receiving values invalid characters
indentifiers
"""

@data('32v2 g #Gmw845h$W b53wi.')
def test_data_with_invalid_identifier(self, value):
return value


@ddt
class FileDataDummy(object):
"""
Expand Down Expand Up @@ -212,9 +224,9 @@ def test_hello(self, val):
pass

assert_is_not_none(getattr(mytest, 'test_hello_ascii'))
assert_is_not_none(getattr(mytest, 'test_hello_non-ascii-\\u2603'))
assert_is_not_none(getattr(mytest, 'test_hello_non_ascii__u2603'))
assert_is_not_none(
getattr(mytest, """test_hello_{u'\\u2603': 'data'}"""))
getattr(mytest, """test_hello__u__u2603____data__"""))

elif six.PY3:

Expand All @@ -225,6 +237,20 @@ def test_hello(self, val):
pass

assert_is_not_none(getattr(mytest, 'test_hello_ascii'))
assert_is_not_none(getattr(mytest, 'test_hello_non-ascii-\N{SNOWMAN}'))
assert_is_not_none(getattr(mytest, 'test_hello_non_ascii__'))
assert_is_not_none(
getattr(mytest, """test_hello_{'\N{SNOWMAN}': 'data'}"""))
getattr(mytest, """test_hello________data__"""))


def test_feed_data_with_invalid_identifier():
"""
Test that data is fed to the decorated tests
"""
tests = list(filter(is_test, DummyInvalidIdentifier.__dict__))
assert_equal(len(tests), 1)

obj = DummyInvalidIdentifier()
method = getattr(obj, tests[0])
assert_equal(method.__name__,
'test_data_with_invalid_identifier_32v2_g__Gmw845h_W_b53wi_')
assert_equal(method(), '32v2 g #Gmw845h$W b53wi.')

0 comments on commit 1b25f2a

Please sign in to comment.