Skip to content

Commit c7b942b

Browse files
author
Sylvain MARIE
committed
Added test for the new needs_binding method
1 parent 16f857a commit c7b942b

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import sys
2+
from functools import partial
3+
4+
import pytest
5+
6+
from pytest_cases.common_others import needs_binding
7+
8+
9+
def a(foo, bar=None):
10+
return foo * 2
11+
12+
13+
b = partial(a, bar=1)
14+
15+
16+
def c():
17+
def _c(foo):
18+
return foo * 2
19+
return _c
20+
21+
22+
def d():
23+
def _d(foo, bar=None):
24+
return foo * 2
25+
return partial(_d, bar=1)
26+
27+
28+
class Foo(object):
29+
30+
def a(self, foo):
31+
assert isinstance(self, Foo)
32+
return foo * 2
33+
34+
@staticmethod
35+
def b(foo):
36+
return foo * 2
37+
38+
@classmethod
39+
def c(cls, foo):
40+
assert issubclass(cls, Foo)
41+
return foo * 2
42+
43+
class MyDesc(object):
44+
def __get__(self, instance, owner):
45+
def descriptor_method(foo):
46+
return foo * 2
47+
return descriptor_method
48+
49+
d = MyDesc()
50+
51+
def e(self):
52+
def nested(foo):
53+
return foo * 2
54+
55+
return nested
56+
57+
class Nested(object):
58+
def a(self, foo):
59+
assert isinstance(self, Foo.Nested)
60+
return foo * 2
61+
62+
@staticmethod
63+
def b(foo):
64+
return foo * 2
65+
66+
@classmethod
67+
def c(cls, foo):
68+
assert issubclass(cls, Foo.Nested)
69+
return foo * 2
70+
71+
class MyDesc(object):
72+
def __get__(self, instance, owner=None):
73+
def descriptor_method(foo):
74+
return foo * 2
75+
76+
return descriptor_method
77+
78+
d = MyDesc()
79+
80+
def e(self):
81+
def nested(foo):
82+
return foo * 2
83+
84+
return nested
85+
86+
87+
ALL_PY = True
88+
PY3_ONLY = sys.version_info >= (3,)
89+
90+
91+
@pytest.mark.parametrize("name, m, expected, supported", [
92+
("plain_py_func", a, False, ALL_PY),
93+
("partial_func", b, False, ALL_PY),
94+
("nested_func", c(), False, ALL_PY),
95+
("nested_partial_func", d(), False, ALL_PY),
96+
# --- class
97+
("method_on_instance", Foo().a, False, ALL_PY),
98+
("method_unbound", Foo.a, True, ALL_PY),
99+
("method_on_class_dict", Foo.__dict__['a'], True, PY3_ONLY),
100+
("static_method_on_instance", Foo().b, False, ALL_PY),
101+
("static_method_on_class", Foo.b, False, ALL_PY),
102+
("static_method_on_class_dict", Foo.__dict__['b'], True, ALL_PY),
103+
("class_method_on_instance", Foo().c, False, ALL_PY),
104+
("class_method_on_class", Foo.c, False, ALL_PY),
105+
("class_method_on_class_dict", Foo.__dict__['c'], True, PY3_ONLY),
106+
("descriptor_method_on_instance", Foo().d, False, ALL_PY),
107+
("descriptor_method_on_class", Foo.d, False, ALL_PY),
108+
# --- nested class
109+
("cls_nested_py_func", Foo.Nested().e(), False, ALL_PY),
110+
("cls_nested_method_on_instance", Foo.Nested().a, False, ALL_PY),
111+
("cls_nested_method_unbound", Foo.Nested.a, True, ALL_PY),
112+
("cls_nested_method_on_class_dict", Foo.Nested.__dict__['a'], True, PY3_ONLY),
113+
("cls_nested_static_method_on_instance", Foo.Nested().b, False, ALL_PY),
114+
("cls_nested_static_method_on_class", Foo.Nested.b, False, ALL_PY),
115+
("cls_nested_static_method_on_class_dict", Foo.Nested.__dict__['b'], True, ALL_PY),
116+
("cls_nested_class_method_on_instance", Foo.Nested().c, False, ALL_PY),
117+
("cls_nested_class_method_on_class", Foo.Nested.c, False, ALL_PY),
118+
("cls_nested_class_method_on_class_dict", Foo.Nested.__dict__['c'], True, PY3_ONLY),
119+
("cls_nested_descriptor_method_on_instance", Foo.Nested().d, False, ALL_PY),
120+
("cls_nested_descriptor_method_on_class", Foo.Nested.d, False, ALL_PY),
121+
], ids=lambda x: str(x) if isinstance(x, (str, bytes, bool)) else "")
122+
def test_needs_binding(name, m, expected, supported):
123+
124+
if not supported:
125+
pytest.skip("Not supported on this version of python")
126+
127+
should_bind = needs_binding(m)
128+
assert should_bind is expected
129+
130+
should_bind2, m = needs_binding(m, return_bound=True)
131+
assert should_bind == should_bind2
132+
133+
# test the method
134+
assert m(2) == 4

0 commit comments

Comments
 (0)