Example #1
0
 def test_decorator_lambda(self):
     """test that a decorator is called, and properly calls the underlying function"""
     l = lambda x: x + 21
     assert l(3) == 24
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_l = chatty_decorator(l)
     assert chatty_l(3) == 24
Example #2
0
def browser_is_notrunning(target, *args, **kwargs):
    """A checker that checks if the browser argument is a SeleniumNotRunning exception"""
    browser = Decorators.get_or_pop_arg("browser", args, kwargs,
                                        Decorators.inspect.getargspec(target))
    if isinstance(browser, SeleniumNotRunning):
        return True
    return False
Example #3
0
 def test_decorator_lambda(self):
     """test that a decorator is called, and properly calls the underlying function"""
     l = lambda x: x + 21
     assert l(3) == 24
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_l = chatty_decorator(l)
     assert chatty_l(3) == 24
Example #4
0
def _wrap_set_methods():
    method_descriptor = type(set.add)
    wrapper_descriptor = type(set.__or__)
    target_methods = {}
    for method in ("__init__", "__delattr__", "__getattribute__", "__setattr__"):
        target_methods[method] = None
    for method in ("difference_update", "intersection_update", "symmetric_difference_update", "update", "__iand__", "__ior__", "__isub__", "__ixor__"):
        target_methods[method] = SemiSortedSet._recalculate
    for method in ("clear", "discard", "pop", "remove"):
        target_methods[method] = SemiSortedSet._recalculate_after_remove
    for function_name in dir(set):
        set_function = getattr(set, function_name)
        if not isinstance(set_function, (method_descriptor, wrapper_descriptor)):
            continue
        override_function = getattr(SemiSortedSet, function_name)
        if override_function is not set_function:
            continue
        if function_name in target_methods:
            target_function = target_methods[function_name]
            if target_function is None:
                continue
            new_function = lock_wrapper(set_function, target_function)
        else:
            new_function = Decorators.SelfLocking.runwithlock(set_function)
        new_function = Decorators.wraptimer(new_function)
        setattr(SemiSortedSet, function_name, new_function)
Example #5
0
 def test_decorator(self):
     """test that a decorator is called, and properly calls the underlying function"""
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_g = chatty_decorator(self.g)
     result = chatty_g(3)
     assert result == 28
     assert self.g.calls[-1] == "Calling 'g'"
Example #6
0
 def test_decorator(self):
     """test that a decorator is called, and properly calls the underlying function"""
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_g = chatty_decorator(self.g)
     result = chatty_g(3)
     assert result == 28
     assert self.g.calls[-1] == "Calling 'g'"
    def test_override_arg(self):
        override_decorator = Decorators.decorator(self.override_x)
        override_g = override_decorator(self.g)
        assert override_g(0) == 75

        override_g3 = override_decorator(self.g3)
        assert override_g3(25, 0) == 100
Example #8
0
 def test_decorator_signature(self):
     """tests that a decorated function retains the signature of the underlying function"""
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_f = chatty_decorator(self.f)
     info = Decorators.decorator_helpers.getinfo(self.f)
     chatty_info = Decorators.decorator_helpers.getinfo(chatty_f)
     assert info == chatty_info
     assert info.__doc__ == chatty_info.__doc__
Example #9
0
 def test_decorator_signature(self):
     """tests that a decorated function retains the signature of the underlying function"""
     chatty_decorator = Decorators.decorator(self.chatty)
     chatty_f = chatty_decorator(self.f)
     info = Decorators.decorator_helpers.getinfo(self.f)
     chatty_info = Decorators.decorator_helpers.getinfo(chatty_f)
     assert info == chatty_info
     assert info.__doc__ == chatty_info.__doc__
Example #10
0
 def check_expected_kwargs(target, *args, **kwargs):
     args_present = {}
     for kw, expected_value in match_kwargs.items():
         actual_value = Decorators.get_or_pop_arg(kw, args, kwargs, Decorators.inspect.getargspec(target))
         if actual_value != expected_value:
             return False
         args_present[kw] = True
     return len(args_present) == len(match_kwargs)
Example #11
0
 def check_expected_kwargs(target, *args, **kwargs):
     args_present = {}
     for kw, expected_value in match_kwargs.items():
         actual_value = Decorators.get_or_pop_arg(
             kw, args, kwargs, Decorators.inspect.getargspec(target))
         if actual_value != expected_value:
             return False
         args_present[kw] = True
     return len(args_present) == len(match_kwargs)
def test_get_or_pop_arg():
    def my_arg_function(foo, bar, jim=3):
        pass

    args = (1, 2)
    kw = {'jim': 3}

    assert Decorators.get_or_pop_arg('bar', args, kw,
                                     inspect.getargspec(my_arg_function)) == 2
    assert args == (1, 2)
    DictUtils.assert_dicts_equal(kw, {'jim': 3})
    assert Decorators.get_or_pop_arg('jim', args, kw,
                                     inspect.getargspec(my_arg_function)) == 3
    assert args == (1, 2)
    DictUtils.assert_dicts_equal(kw, {'jim': 3})

    kw['billybob'] = 4
    assert Decorators.get_or_pop_arg('billybob', args, kw,
                                     inspect.getargspec(my_arg_function)) == 4
    assert args == (1, 2)
    DictUtils.assert_dicts_equal(kw, {'jim': 3})
Example #13
0
 def test_decorator_lambda_calling_frame_extendedarg(self):
     """tests that a decorated function can get the frame of the calling function when a lambda is being decorated, and pass it as an extended argument"""
     l = lambda x, calling_frame: x + 21
     frames_so_far = len(getattr(l, "call_frames", []))
     callf_decorator = Decorators.decorator(self.callfdec2, [("calling_frame", None)], calling_frame_arg="calling_frame")
     callf_l = callf_decorator(l)
     assert callf_l(100, "nonsense") == 100 + 21
     assert len(l.call_frames) == frames_so_far + 1
     call_frame = l.call_frames[-1]
     assert call_frame
     # check that the argument given for calling frame is overridden
     assert call_frame != "nonsense"
     assert call_frame.f_code.co_name == "test_decorator_lambda_calling_frame_extendedarg"
def test_get_right_args():
    def my_arg_function(foo, bar, jim=3):
        pass

    rightargs = Decorators.getrightargs(my_arg_function, {
        'foo': 1,
        'bar': 2,
        'bob': 3,
        'mary': 4,
        'jim': 5
    })
    DictUtils.assert_dicts_equal(rightargs, {'foo': 1, 'bar': 2, 'jim': 5})
    rightargs = Decorators.getrightargs(my_arg_function, {
        'foo': 1,
        'bar': 2,
        'bob': 3,
        'mary': 4
    })
    DictUtils.assert_dicts_equal(rightargs, {'foo': 1, 'bar': 2})

    class my_arg_class(object):
        def __init__(self, foo, filip):
            pass

    rightargs = Decorators.getrightargs(my_arg_class, {
        'foo': 1,
        'bar': 2,
        'bob': 3,
        'mary': 4
    })
    DictUtils.assert_dicts_equal(rightargs, {'foo': 1, 'filip': None})

    rightargs, rightkw = Decorators.conform_to_argspec((1, 2), {
        'billybob': 5,
        'jim': 3
    }, inspect.getargspec(my_arg_function))
    assert rightargs == [1, 2, 3]
    assert not rightkw
Example #15
0
 def test_decorator_calling_frame_arg(self):
     """tests that a decorated function can get the frame of the calling function"""
     frames_so_far = len(getattr(self.g, "call_frames", []))
     callf_decorator = Decorators.decorator(self.callfdec, ['y', ('z', 3)], calling_frame_arg="calling_frame")
     callf_g = callf_decorator(self.g)
     assert callf_g(100, 4) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 1
     call_frame = self.g.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_calling_frame_arg"
     # check that calling_frame can't be overridden
     some_frame = Decorators.inspect.currentframe()
     raises(TypeError, callf_g, 100, 4, calling_frame=some_frame)
     raises(TypeError, callf_g, 100, 4, some_frame)
Example #16
0
 def test_extend_decorator_signature(self):
     """tests that a decorated function can extend the signature of the underlying function"""
     ext_decorator = Decorators.decorator(self.extdec, ['y', ('z', 3)])
     ext_g = ext_decorator(self.g)
     info = Decorators.decorator_helpers.getinfo(self.g)
     ext_info = Decorators.decorator_helpers.getinfo(ext_g)
     assert ext_info.__doc__ == info.__doc__
     assert ext_info["name"] == info["name"]
     assert ext_info["argnames"] == ['x', 'y', 'z']
     assert ext_info["defarg"] == (3,)
     assert ext_info["shortsign"] == 'x, y, z'
     assert ext_info["fullsign"] == 'x, y, z=defarg[0]'
     assert ext_info["arg0"] == 'x'
     assert ext_info["arg1"] == 'y'
     assert ext_info["arg2"] == 'z'
Example #17
0
 def test_decorator_lambda_calling_frame_extendedarg(self):
     """tests that a decorated function can get the frame of the calling function when a lambda is being decorated, and pass it as an extended argument"""
     l = lambda x, calling_frame: x + 21
     frames_so_far = len(getattr(l, "call_frames", []))
     callf_decorator = Decorators.decorator(
         self.callfdec2, [("calling_frame", None)],
         calling_frame_arg="calling_frame")
     callf_l = callf_decorator(l)
     assert callf_l(100, "nonsense") == 100 + 21
     assert len(l.call_frames) == frames_so_far + 1
     call_frame = l.call_frames[-1]
     assert call_frame
     # check that the argument given for calling frame is overridden
     assert call_frame != "nonsense"
     assert call_frame.f_code.co_name == "test_decorator_lambda_calling_frame_extendedarg"
Example #18
0
 def test_extend_decorator_signature(self):
     """tests that a decorated function can extend the signature of the underlying function"""
     ext_decorator = Decorators.decorator(self.extdec, ['y', ('z', 3)])
     ext_g = ext_decorator(self.g)
     info = Decorators.decorator_helpers.getinfo(self.g)
     ext_info = Decorators.decorator_helpers.getinfo(ext_g)
     assert ext_info.__doc__ == info.__doc__
     assert ext_info["name"] == info["name"]
     assert ext_info["argnames"] == ['x', 'y', 'z']
     assert ext_info["defarg"] == (3, )
     assert ext_info["shortsign"] == 'x, y, z'
     assert ext_info["fullsign"] == 'x, y, z=defarg[0]'
     assert ext_info["arg0"] == 'x'
     assert ext_info["arg1"] == 'y'
     assert ext_info["arg2"] == 'z'
Example #19
0
 def test_decorator_lambda_calling_frame_arg(self):
     """tests that a decorated function can get the frame of the calling function when a lambda is being decorated"""
     l = lambda x: x + 21
     frames_so_far = len(getattr(l, "call_frames", []))
     callf_decorator = Decorators.decorator(self.callfdec, ['y', ('z', 3)], calling_frame_arg="calling_frame")
     callf_l = callf_decorator(l)
     assert callf_l(100, 4) == 100 + 12 + 21
     assert len(l.call_frames) == frames_so_far + 1
     call_frame = l.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_lambda_calling_frame_arg"
     # check that calling_frame can't be overridden
     some_frame = Decorators.inspect.currentframe()
     raises(TypeError, callf_l, 100, 4, calling_frame=some_frame)
     raises(TypeError, callf_l, 100, 4, some_frame)
Example #20
0
 def test_decorator_calling_frame_arg(self):
     """tests that a decorated function can get the frame of the calling function"""
     frames_so_far = len(getattr(self.g, "call_frames", []))
     callf_decorator = Decorators.decorator(
         self.callfdec, ['y', ('z', 3)], calling_frame_arg="calling_frame")
     callf_g = callf_decorator(self.g)
     assert callf_g(100, 4) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 1
     call_frame = self.g.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_calling_frame_arg"
     # check that calling_frame can't be overridden
     some_frame = Decorators.inspect.currentframe()
     raises(TypeError, callf_g, 100, 4, calling_frame=some_frame)
     raises(TypeError, callf_g, 100, 4, some_frame)
Example #21
0
 def test_decorator_lambda_calling_frame_arg(self):
     """tests that a decorated function can get the frame of the calling function when a lambda is being decorated"""
     l = lambda x: x + 21
     frames_so_far = len(getattr(l, "call_frames", []))
     callf_decorator = Decorators.decorator(
         self.callfdec, ['y', ('z', 3)], calling_frame_arg="calling_frame")
     callf_l = callf_decorator(l)
     assert callf_l(100, 4) == 100 + 12 + 21
     assert len(l.call_frames) == frames_so_far + 1
     call_frame = l.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_lambda_calling_frame_arg"
     # check that calling_frame can't be overridden
     some_frame = Decorators.inspect.currentframe()
     raises(TypeError, callf_l, 100, 4, calling_frame=some_frame)
     raises(TypeError, callf_l, 100, 4, some_frame)
Example #22
0
 def test_decorator_calling_frame_extendedarg(self):
     """tests that a decorated function can get the frame of the calling function and pass it as an extended argument"""
     frames_so_far = len(getattr(self.g, "call_frames", []))
     callf_decorator = Decorators.decorator(self.callfdec, ['y', ('z', 3), ("calling_frame", None)], calling_frame_arg="calling_frame")
     callf_g = callf_decorator(self.g)
     assert callf_g(100, 4) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 1
     call_frame = self.g.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_calling_frame_extendedarg"
     # check that calling_frame can be overridden, but the result is still correct
     some_frame = Decorators.inspect.currentframe().f_back
     assert callf_g(100, 4, calling_frame=some_frame) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 2
     assert self.g.call_frames[-1] != some_frame
     assert callf_g(100, 4, 5, some_frame) == 100 + 20 + 25
     assert len(self.g.call_frames) == frames_so_far + 3
     assert self.g.call_frames[-1] != some_frame
Example #23
0
 def test_extend_decorator_existing(self):
     """tests that a decorated function can extend the underlying function, handling existing arguments right"""
     ext_decorator = Decorators.decorator(self.extdec2, [('y', 5), ('z', 3), ('x', 0)])
     ext_g = ext_decorator(self.g2)
     info = Decorators.decorator_helpers.getinfo(self.g2)
     ext_info = Decorators.decorator_helpers.getinfo(ext_g)
     assert ext_info.__doc__ == info.__doc__
     assert ext_info["name"] == info["name"]
     assert ext_info["argnames"] == ['x', 'z', 'y']
     assert ext_info["defarg"] == (0, 3, 5)
     assert ext_info["shortsign"] == 'x, z, y'
     assert ext_info["fullsign"] == 'x=defarg[0], z=defarg[1], y=defarg[2]'
     assert ext_info["arg0"] == 'x'
     assert ext_info["arg1"] == 'z'
     assert ext_info["arg2"] == 'y'
     result = ext_g()
     assert result == (0 + 3*5) + 3 + 25
     assert self.g2.calls[-1] == "Called with x=0, y=5, z=3"
Example #24
0
 def test_extend_decorator_existing(self):
     """tests that a decorated function can extend the underlying function, handling existing arguments right"""
     ext_decorator = Decorators.decorator(self.extdec2, [('y', 5), ('z', 3),
                                                         ('x', 0)])
     ext_g = ext_decorator(self.g2)
     info = Decorators.decorator_helpers.getinfo(self.g2)
     ext_info = Decorators.decorator_helpers.getinfo(ext_g)
     assert ext_info.__doc__ == info.__doc__
     assert ext_info["name"] == info["name"]
     assert ext_info["argnames"] == ['x', 'z', 'y']
     assert ext_info["defarg"] == (0, 3, 5)
     assert ext_info["shortsign"] == 'x, z, y'
     assert ext_info["fullsign"] == 'x=defarg[0], z=defarg[1], y=defarg[2]'
     assert ext_info["arg0"] == 'x'
     assert ext_info["arg1"] == 'z'
     assert ext_info["arg2"] == 'y'
     result = ext_g()
     assert result == (0 + 3 * 5) + 3 + 25
     assert self.g2.calls[-1] == "Called with x=0, y=5, z=3"
Example #25
0
 def test_decorator_calling_frame_extendedarg(self):
     """tests that a decorated function can get the frame of the calling function and pass it as an extended argument"""
     frames_so_far = len(getattr(self.g, "call_frames", []))
     callf_decorator = Decorators.decorator(
         self.callfdec, ['y', ('z', 3), ("calling_frame", None)],
         calling_frame_arg="calling_frame")
     callf_g = callf_decorator(self.g)
     assert callf_g(100, 4) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 1
     call_frame = self.g.call_frames[-1]
     assert call_frame
     assert call_frame.f_code.co_name == "test_decorator_calling_frame_extendedarg"
     # check that calling_frame can be overridden, but the result is still correct
     some_frame = Decorators.inspect.currentframe().f_back
     assert callf_g(100, 4, calling_frame=some_frame) == 100 + 12 + 25
     assert len(self.g.call_frames) == frames_so_far + 2
     assert self.g.call_frames[-1] != some_frame
     assert callf_g(100, 4, 5, some_frame) == 100 + 20 + 25
     assert len(self.g.call_frames) == frames_so_far + 3
     assert self.g.call_frames[-1] != some_frame
Example #26
0
    pass

class SeleniumNotInstalled(Utils.Skipped):
    pass

def browser_is_notrunning(target, *args, **kwargs):
    """A checker that checks if the browser argument is a SeleniumNotRunning exception"""
    browser = Decorators.get_or_pop_arg("browser", args, kwargs, Decorators.inspect.getargspec(target))
    if isinstance(browser, SeleniumNotRunning):
        return True
    return False

if_selenium_browser = Utils.skip_test_for("Selenium Server not found; cannot run web browser tests", browser_is_notrunning)
if_selenium_module = Utils.if_module(selenium, "selenium")

if_selenium = Decorators.chain_decorators(if_selenium_browser, if_selenium_module)

class BrowserDim(IterativeTester.Dimension):
    seleniumHost = "localhost"
    seleniumPort = 4444
    def __init__(self):
        """iterates over supported browsers and runs tests on each of them"""
        browsernames = []
        self._resources = {}
        self._browsernames = {}
        self._skipped_conditions = {}
        self._selenium_runners = {}
        havebrowsers = False
        if selenium:
            for browser in all_browsers:
                if selenium_can_find(browser):
Example #27
0
 def test_extend_decorator(self):
     """tests that a decorated function can extend the underlying function"""
     ext_decorator = Decorators.decorator(self.extdec, ['y', ('z', 3)])
     ext_g = ext_decorator(self.g)
     assert ext_g(100, 4) == 100 + 12 + 25
     assert self.g.calls[-1] == "Called with x=100, y=4, z=3"
Example #28
0
def browser_is_notrunning(target, *args, **kwargs):
    """A checker that checks if the browser argument is a SeleniumNotRunning exception"""
    browser = Decorators.get_or_pop_arg("browser", args, kwargs, Decorators.inspect.getargspec(target))
    if isinstance(browser, SeleniumNotRunning):
        return True
    return False
 def override_x(f, *args, **kw):
     args, kw = Decorators.override_arg("x", 50, args, kw,
                                        inspect.getargspec(f))
     return f(*args, **kw)
Example #30
0
 def test_extend_decorator(self):
     """tests that a decorated function can extend the underlying function"""
     ext_decorator = Decorators.decorator(self.extdec, ['y', ('z', 3)])
     ext_g = ext_decorator(self.g)
     assert ext_g(100, 4) == 100 + 12 + 25
     assert self.g.calls[-1] == "Called with x=100, y=4, z=3"