Skip to content

Commit

Permalink
Fix #134 - wrap str, bytes, and any non-iterable input
Browse files Browse the repository at this point in the history
  • Loading branch information
wolever committed Mar 2, 2023
1 parent 02a289c commit 41471f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
(https://github.com/wolever/parameterized/pull/135; thanks @Ronserruya)
* Work around for bug bpo-40126 in older versions of ``mock``
(https://github.com/wolever/parameterized/pull/129; thanks @alexpizarroj)
* Allow str, bytes, and any non-iterable input to be passed to
``@parameterized`` without wrapping in a tuple
(https://github.com/wolever/parameterized/pull/157)

0.8.1 (2021-01-09)
* Add README and LICENSE to pypi sdist package
Expand Down
5 changes: 4 additions & 1 deletion parameterized/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import inspect
import warnings
from typing import Iterable
from functools import wraps
from types import MethodType as MethodType
from collections import namedtuple
Expand Down Expand Up @@ -208,7 +209,7 @@ def from_decorator(cls, args):
"""
if isinstance(args, param):
return args
elif isinstance(args, string_types):
elif isinstance(args, (str, bytes)) or not isinstance(args, Iterable):
args = (args, )
try:
return cls(*args)
Expand Down Expand Up @@ -635,6 +636,8 @@ def standalone_func(*a):

@classmethod
def to_safe_name(cls, s):
if not isinstance(s, str):
s = str(s)
return str(re.sub("[^a-zA-Z0-9_]+", "_", s))


Expand Down
29 changes: 16 additions & 13 deletions parameterized/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ def expect_exception_matching_regex(tests, expected_exception, expected_regexp):
test_params = [
(42, ),
"foo0",
b"bar",
123,
param("foo1"),
param("foo2", bar=42),
]

expect("standalone", [
"test_naked_function(42, bar=None)",
"test_naked_function('foo0', bar=None)",
"test_naked_function(b'bar', bar=None)",
"test_naked_function(123, bar=None)",
"test_naked_function('foo1', bar=None)",
"test_naked_function('foo2', bar=42)",
"test_naked_function(42, bar=None)",
])

@parameterized(test_params)
Expand All @@ -111,10 +115,12 @@ def test_naked_function(foo, bar=None):

class TestParameterized(object):
expect("generator", [
"test_instance_method(42, bar=None)",
"test_instance_method(b'bar', bar=None)",
"test_instance_method(123, bar=None)",
"test_instance_method('foo0', bar=None)",
"test_instance_method('foo1', bar=None)",
"test_instance_method('foo2', bar=42)",
"test_instance_method(42, bar=None)",
])

@parameterized(test_params)
Expand Down Expand Up @@ -149,7 +155,8 @@ def test_setup(self, count, *a):

def custom_naming_func(custom_tag):
def custom_naming_func(testcase_func, param_num, param):
return testcase_func.__name__ + ('_%s_name_' % custom_tag) + str(param.args[0])
arg = param.args[0]
return testcase_func.__name__ + ('_%s_name_' % custom_tag) + parameterized.to_safe_name(arg)

return custom_naming_func

Expand Down Expand Up @@ -287,10 +294,12 @@ def test_mock_patch_standalone_function(foo, mock_umask):

class TestParamerizedOnTestCase(TestCase):
expect([
"test_on_TestCase(42, bar=None)",
"test_on_TestCase(b'bar', bar=None)",
"test_on_TestCase(123, bar=None)",
"test_on_TestCase('foo0', bar=None)",
"test_on_TestCase('foo1', bar=None)",
"test_on_TestCase('foo2', bar=42)",
"test_on_TestCase(42, bar=None)",
])

@parameterized.expand(test_params)
Expand All @@ -299,6 +308,8 @@ def test_on_TestCase(self, foo, bar=None):

expect([
"test_on_TestCase2_custom_name_42(42, bar=None)",
"test_on_TestCase2_custom_name_b_bar_(b'bar', bar=None)",
"test_on_TestCase2_custom_name_123(123, bar=None)",
"test_on_TestCase2_custom_name_foo0('foo0', bar=None)",
"test_on_TestCase2_custom_name_foo1('foo1', bar=None)",
"test_on_TestCase2_custom_name_foo2('foo2', bar=42)",
Expand All @@ -311,7 +322,7 @@ def test_on_TestCase2(self, foo, bar=None):
frame = stack[1]
frame_locals = frame[0].f_locals
nose_test_method_name = frame_locals['a'][0]._testMethodName
expected_name = "test_on_TestCase2_custom_name_" + str(foo)
expected_name = "test_on_TestCase2_custom_name_" + parameterized.to_safe_name(foo)
assert_equal(nose_test_method_name, expected_name,
"Test Method name '%s' did not get customized to expected: '%s'" %
(nose_test_method_name, expected_name))
Expand Down Expand Up @@ -393,14 +404,6 @@ def test_in_subclass_of_TestCase(self, foo):
else:
raise AssertionError("Expected exception not raised")

def test_helpful_error_on_invalid_parameters():
try:
parameterized([1432141234243])(lambda: None)
except Exception as e:
assert_contains(str(e), "Parameters must be tuples")
else:
raise AssertionError("Expected exception not raised")


def test_helpful_error_on_empty_iterable_input():
try:
Expand Down

0 comments on commit 41471f9

Please sign in to comment.