Changeset - bb391ccef19e
[Not reviewed]
default
0 1 0
timeless@gmail.com - 10 years ago 2016-05-03 14:10:32
timeless@gmail.com
spelling: precisely
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/parameterized.py
Show inline comments
 
@@ -36,97 +36,97 @@ def _terrible_magic_get_defining_classes
 
    parents, _ = parents.rsplit(")", 1)
 
    return eval("[" + parents + "]", frame[0].f_globals, frame[0].f_locals)
 

	
 

	
 
def parameterized(input):
 
    """ Parameterize a test case:
 
        >>> add1_tests = [(1, 2), (2, 3)]
 
        >>> class TestFoo(object):
 
        ...     @parameterized(add1_tests)
 
        ...     def test_add1(self, input, expected):
 
        ...         assert_equal(add1(input), expected)
 
        >>> @parameterized(add1_tests)
 
        ... def test_add1(input, expected):
 
        ...     assert_equal(add1(input), expected)
 
        >>>
 
        """
 

	
 
    if not hasattr(input, "__iter__"):
 
        raise ValueError("expected iterable input; got %r" % (input,))
 

	
 
    def parameterized_helper(f):
 
        attached_instance_method = [False]
 

	
 
        parent_classes = _terrible_magic_get_defining_classes()
 
        if any(issubclass(cls, TestCase) for cls in parent_classes):
 
            raise Exception("Warning: '@parameterized' tests won't work "
 
                            "inside subclasses of 'TestCase' - use "
 
                            "'@parameterized.expand' instead")
 

	
 
        @wraps(f)
 
        def parameterized_helper_method(self=None):
 
            if self is not None and not attached_instance_method[0]:
 
                # confusingly, we need to create a named instance method and
 
                # attach that to the class...
 
                cls = self.__class__
 
                im_f = new.instancemethod(f, None, cls)
 
                setattr(cls, f.__name__, im_f)
 
                attached_instance_method[0] = True
 
            for args in input:
 
                if isinstance(args, basestring):
 
                    args = [args]
 
                # ... then pull that named instance method off, turning it into
 
                # a bound method ...
 
                if self is not None:
 
                    args = [getattr(self, f.__name__)] + list(args)
 
                else:
 
                    args = [f] + list(args)
 
                # ... then yield that as a tuple. If those steps aren't
 
                # followed precicely, Nose gets upset and doesn't run the test
 
                # followed precisely, Nose gets upset and doesn't run the test
 
                # or doesn't run setup methods.
 
                yield tuple(args)
 

	
 
        f.__name__ = "_helper_for_%s" % (f.__name__,)
 
        parameterized_helper_method.parameterized_input = input
 
        parameterized_helper_method.parameterized_func = f
 
        return parameterized_helper_method
 

	
 
    return parameterized_helper
 

	
 

	
 
def to_safe_name(s):
 
    return re.sub("[^a-zA-Z0-9_]", "", s)
 

	
 

	
 
def parameterized_expand_helper(func_name, func, args):
 
    def parameterized_expand_helper_helper(self=()):
 
        if self != ():
 
            self = (self,)
 
        return func(*(self + args))
 
    parameterized_expand_helper_helper.__name__ = str(func_name)
 
    return parameterized_expand_helper_helper
 

	
 

	
 
def parameterized_expand(input):
 
    """ A "brute force" method of parameterizing test cases. Creates new test
 
        cases and injects them into the namespace that the wrapped function
 
        is being defined in. Useful for parameterizing tests in subclasses
 
        of 'UnitTest', where Nose test generators don't work.
 

	
 
        >>> @parameterized.expand([("foo", 1, 2)])
 
        ... def test_add1(name, input, expected):
 
        ...     actual = add1(input)
 
        ...     assert_equal(actual, expected)
 
        ...
 
        >>> locals()
 
        ... 'test_add1_foo_0': <function ...> ...
 
        >>>
 
        """
 

	
 
    def parameterized_expand_wrapper(f):
 
        stack = inspect.stack()
 
        frame = stack[1]
 
        frame_locals = frame[0].f_locals
 

	
 
        base_name = f.__name__
 
        for num, args in enumerate(input):
 
            name_suffix = "_%s" % (num,)
0 comments (0 inline, 0 general)