forked from youknowone/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_callable.py
83 lines (66 loc) · 2.48 KB
/
test_callable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
import pytest
from ring.callable import Callable
if sys.version_info[0] >= 3:
from ._test_callable_py3 import * # noqa
@pytest.mark.parametrize('f,args,kwargs,merged', [
(lambda: None, (), {}, {}),
(lambda x, y: None, (1, 2), {}, dict(x=1, y=2)),
(lambda x, y, z=30: None, (1, 2), {}, dict(x=1, y=2, z=30)),
(lambda x, y, z=30: None, (1,), {'y': 2}, dict(x=1, y=2, z=30)),
(lambda x, y, z=30: None, (1, 2, 3), {}, dict(x=1, y=2, z=3)),
(lambda x, y, z=30: None, (1,), {'y': 20}, dict(x=1, y=20, z=30)),
(lambda x, y, *args: None, (1,), {'y': 20}, dict(x=1, y=20, args=())),
(lambda x, y, *args: None, (1, 2, 3, 4, 5), {}, dict(x=1, y=2, args=(3, 4, 5))),
(lambda x, **kw: None, (), {'x': 10, 'y': 20, 'z': 30}, dict(x=10, kw={'y': 20, 'z': 30})),
(lambda x, *args, **kw: None, (1, 2, 3, 4), {'y': 20, 'z': 30}, dict(x=1, args=(2, 3, 4), kw={'y': 20, 'z': 30})),
])
def test_kwargify(f, args, kwargs, merged):
kwargified = Callable(f).kwargify(args, kwargs)
print(kwargified)
print(merged)
assert kwargified == merged
@pytest.mark.parametrize('f,args,kwargs,exc', [
(lambda: None, (1, 2), {}, TypeError),
(lambda x, y: None, (2, 3), {'x': 1}, TypeError),
(lambda x, y, z=30: None, (1,), {}, TypeError),
(lambda x, y, z=30: None, (1,), {'x': 2}, TypeError),
])
def test_kwargify_exc(f, args, kwargs, exc):
with pytest.raises(exc):
Callable(f).kwargify(args, kwargs)
def test_empty_annotations():
c = Callable(lambda a, *b, **c: None)
assert c.annotations == {}
def test_callable_attributes():
def f():
pass
w = Callable(f)
assert w.is_barefunction is True
assert w.is_descriptor is False
assert w.is_membermethod is False
assert w.is_classmethod is False
class A():
def m(self):
pass
w = Callable(m)
assert w.is_barefunction is False
assert w.is_descriptor is False
assert w.is_membermethod is True
assert w.is_classmethod is False
@classmethod
def c(cls):
pass
w = Callable(c)
assert w.is_barefunction is False
assert w.is_descriptor is True
assert w.is_membermethod is False
assert w.is_classmethod is True
@staticmethod
def s():
pass
w = Callable(s)
assert w.is_barefunction is False
assert w.is_descriptor is True
assert w.is_membermethod is False
assert w.is_classmethod is False