def test_wrap_class_constructor(): class A(object): def __init__(self, a, b=None): self.a = a self.b = b cons = kwargify(A) a = cons(a=1) assert a.a == 1 assert a.b is None
def skip_plugin(item, skip, reason="Skipped"): if isinstance(skip, bool): if skip: pytest.skip(reason) elif callable(skip): skip_kwargified = kwargify(skip) if skip_kwargified(**extract_fixtures_values(item)): pytest.skip(reason) else: if bool(skip): pytest.skip(reason)
def test_wrapped(): # double check that the function wrapper does its job def f(): """doctring!""" pass f.custom_attr = True wrapped_f = kwargify(f) # __wrapped__ should be set assert wrapped_f.__wrapped__ is f # dunder attrs should be copied over assert wrapped_f.__doc__ == f.__doc__ # any public attrs on the wrapped func should be available assert wrapped_f.custom_attr
def test_wrap_method(): """Tst whether wrapping already existing method works.""" class A(object): def a(self): return True def b(self, a, b): return locals() def c(self, a, b=None): return locals() a = A() k_a = kwargify(a.a) k_b = kwargify(a.b) k_c = kwargify(a.c) # Plain function assert k_a() # Without nonrequired parameters with pytest.raises(TypeError): k_b() result = k_b(1, 2) assert result["a"] == 1 assert result["b"] == 2 # With nonrequired params with pytest.raises(TypeError): k_c() result_1 = k_c(1, 2) result_2 = k_c(1) assert result_1["a"] == result_2["a"] == 1 assert result_1["b"] == 2 assert result_2["b"] is None
def test_only_kwargs_passed_not_enough(self, o): kwargify(o.onlyargs)(a="bar")
def test_only_kwargs_passed_wrong(self, o): kwargify(o.onlyargs)(foo="bar")
def test_args_given_enough(self, o): kwargify(o.onlyargs)(1, 2)
def test_no_args_given_fails(self, o): kwargify(o.onlyargs)()
def test_both_passed(self, o): kwargify(o.onlyargs)(1, b=2)
def test_pass_only_required(self, o): assert kwargify(o.withdefault)(1)["b"] is None
def test_no_args_given(self, o): kwargify(o.noargs)()
def test_override_default_with_kwarg(self, o): assert kwargify(o.withdefault)(1, b=2)["b"] == 2
def test_only_kwargs_passed(self, o): kwargify(o.onlyargs)(a=1, b=2)
def test_kwargs_passed(self, o): kwargify(o.noargs)(foo="bar")
def test_args_given(self, o, n): kwargify(o.noargs)(*range(n + 1))
def f(g): self._plugins.append(Plugin(name, keys, kwargify(g), kwargs)) return g # So the markers can be chained