-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bender_hooks.py
144 lines (106 loc) · 3.33 KB
/
test_bender_hooks.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from bender_hooks import HookError
import bender_hooks as hooks
import pytest
def test_simple_hook():
def my_spec(x, y):
"""spec docs"""
def my_spec_2():
"""spec docs"""
dec = hooks.make_decorator(my_spec)
dec_2 = hooks.make_decorator(my_spec_2)
class Impl(object):
def my_func(self):
return 'my_func'
@dec
def my_hook_1(self, x, y):
assert (x, y) == (1, 20)
return 'my_hook_1'
@dec
def my_hook_2(self, x):
assert x == 1
return 'my_hook_2'
@dec
def my_hook_3(self):
return 'my_hook_3'
@dec_2
def my_hook_4():
return 'my_hook_4'
assert Impl.my_hook_1.hook_name == 'my_spec'
assert Impl.my_hook_2.hook_name == 'my_spec'
assert Impl.my_hook_3.hook_name == 'my_spec'
impl = Impl()
assert hooks.call(impl.my_hook_1, x=1, y=20) == 'my_hook_1'
assert hooks.call(impl.my_hook_2, x=1, y=20) == 'my_hook_2'
assert hooks.call(impl.my_hook_3, x=1, y=20) == 'my_hook_3'
assert hooks.call(my_hook_4, x=1, y=20) == 'my_hook_4'
with pytest.raises(HookError):
hooks.call(impl.my_func)
def test_hook_with_input():
def my_spec(x, y):
"""spec docs"""
dec = hooks.make_decorator(my_spec, inputs='title')
@dec('Hook 1')
def my_hook_1(x, y):
assert x == 1
assert y == 20
return 'my_hook_1'
@dec('Hook 2')
def my_hook_2(x):
assert x == 1
return 'my_hook_2'
assert hooks.call(my_hook_1, x=1, y=20) == 'my_hook_1'
assert my_hook_1.inputs['title'] == 'Hook 1'
assert hooks.call(my_hook_2, x=1, y=20) == 'my_hook_2'
assert my_hook_2.inputs['title'] == 'Hook 2'
@pytest.mark.parametrize('spec', [
lambda *args: None,
lambda *args, **kwargs: None,
lambda **kwargs: None,
lambda x=10: None,
])
def test_invalid_specs(spec):
with pytest.raises(AssertionError):
hooks.make_decorator(spec)
def test_hooks_call():
def spec_a(x, y):
"""spec docs"""
def spec_b(z):
"""spec docs"""
a = hooks.make_decorator(spec_a)
b = hooks.make_decorator(spec_b)
calls = []
class Impl(object):
@a
def hook_1(self):
calls.append('hook_1')
@a
def hook_2(self):
calls.append('hook_2')
@b
def hook_3(self):
calls.append('hook_3')
return 'hook_3'
impl = Impl()
assert hooks.find_hooks(impl, 'spec_a') == [impl.hook_1, impl.hook_2]
assert hooks.find_hooks(impl, 'spec_b') == [impl.hook_3]
assert hooks.call_all_hooks(impl, 'spec_a', x=1, y=20) is None
assert hooks.call_all_hooks(impl, 'spec_b', z=40) is None
assert calls == ['hook_1', 'hook_2', 'hook_3']
calls[:] = []
assert hooks.call_unique_hook(impl, 'spec_b', z=40) == 'hook_3'
assert calls == ['hook_3']
with pytest.raises(hooks.HookError):
hooks.call_unique_hook(impl, 'spec_a', x=1, y=20)
def test_invalid_impl_signature():
def spec(x, y):
"""spec docs"""
dec = hooks.make_decorator(spec)
with pytest.raises(hooks.HookError):
@dec
def foo(xx, yy):
pass
with pytest.raises(hooks.HookError):
class Impl(object):
@dec
def foo(self, xx, yy):
pass