forked from fgmacedo/python-statemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_signature.py
More file actions
165 lines (136 loc) · 5.28 KB
/
Copy pathtest_signature.py
File metadata and controls
165 lines (136 loc) · 5.28 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import inspect
from functools import partial
import pytest
from statemachine.signature import SignatureAdapter
def single_positional_param(a):
return a
def single_default_keyword_param(a=42):
return a
def args_param(*args):
return args
def kwargs_param(**kwargs):
return kwargs
def args_and_kwargs_param(*args, **kwargs):
return args, kwargs
def positional_optional_catchall(a, b="ham", *args):
return a, b, args
def ignored_param(a, b, *, c, d=10):
return a, b, c, d
def positional_and_kw_arguments(source, target, event):
return source, target, event
def default_kw_arguments(source: str = "A", target: str = "B", event: str = "go"):
return source, target, event
class MyObject:
def __init__(self, value=42):
self.value = value
def method_no_argument(self):
return self.value
class TestSignatureAdapter:
@pytest.mark.parametrize(
("func", "args", "kwargs", "expected"),
[
(single_positional_param, [10], {}, 10),
(single_positional_param, [], {"a": 10}, 10),
pytest.param(single_positional_param, [], {}, TypeError),
(single_default_keyword_param, [10], {}, 10),
(single_default_keyword_param, [], {"a": 10}, 10),
pytest.param(single_default_keyword_param, [], {}, 42),
(MyObject().method_no_argument, [], {}, 42),
(MyObject().method_no_argument, ["ignored"], {"x": True}, 42),
(MyObject.method_no_argument, [MyObject()], {"x": True}, 42),
pytest.param(MyObject.method_no_argument, [], {}, TypeError),
(args_param, [], {}, ()),
(args_param, [42], {}, (42,)),
(args_param, [1, 1, 2, 3, 5, 8, 13], {}, (1, 1, 2, 3, 5, 8, 13)),
(
args_param,
[1, 1, 2, 3, 5, 8, 13],
{"x": True, "other": 42},
(1, 1, 2, 3, 5, 8, 13),
),
(kwargs_param, [], {}, {}),
(kwargs_param, [1], {}, {}),
(kwargs_param, [1, 3, 5, 8, "x", True], {}, {}),
(kwargs_param, [], {"x": True}, {"x": True}),
(kwargs_param, [], {"x": True, "n": 42}, {"x": True, "n": 42}),
(
kwargs_param,
[10, "x", False],
{"x": True, "n": 42},
{"x": True, "n": 42},
),
(args_and_kwargs_param, [], {}, ((), {})),
(args_and_kwargs_param, [1], {}, ((1,), {})),
(
args_and_kwargs_param,
[1, 3, 5, False, "n"],
{"x": True, "n": 42},
((1, 3, 5, False, "n"), {"x": True, "n": 42}),
),
(positional_optional_catchall, [], {}, TypeError),
(positional_optional_catchall, [42], {}, (42, "ham", ())),
pytest.param(
positional_optional_catchall,
[True],
{"b": "spam"},
(True, "spam", ()),
),
pytest.param(
positional_optional_catchall,
["a", "b"],
{"b": "spam"},
("a", "spam", ()),
),
(
positional_optional_catchall,
["a", "b", "c"],
{"b": "spam"},
("a", "spam", ("c",)),
),
(
positional_optional_catchall,
["a", "b", "c", False, 10],
{"other": 42},
("a", "b", ("c", False, 10)),
),
(ignored_param, [], {}, TypeError),
(ignored_param, [1, 2, 3], {}, TypeError),
pytest.param(
ignored_param,
[1, 2],
{"c": 42},
(1, 2, 42, 10),
),
pytest.param(
ignored_param,
[1, 2],
{"c": 42, "d": 21},
(1, 2, 42, 21),
),
pytest.param(positional_and_kw_arguments, [], {}, TypeError),
(positional_and_kw_arguments, [1, 2, 3], {}, (1, 2, 3)),
(positional_and_kw_arguments, [1, 2], {"event": "foo"}, (1, 2, "foo")),
(
positional_and_kw_arguments,
[],
{"source": "A", "target": "B", "event": "foo"},
("A", "B", "foo"),
),
pytest.param(default_kw_arguments, [], {}, ("A", "B", "go")),
(default_kw_arguments, [1, 2, 3], {}, (1, 2, 3)),
(default_kw_arguments, [1, 2], {"event": "wait"}, (1, 2, "wait")),
],
)
def test_wrap_fn_single_positional_parameter(self, func, args, kwargs, expected):
wrapped_func = SignatureAdapter.wrap(func)
assert wrapped_func.__name__ == func.__name__
if inspect.isclass(expected) and issubclass(expected, Exception):
with pytest.raises(expected):
wrapped_func(*args, **kwargs)
else:
assert wrapped_func(*args, **kwargs) == expected
def test_support_for_partial(self):
part = partial(positional_and_kw_arguments, event="activated")
wrapped_func = SignatureAdapter.wrap(part)
assert wrapped_func("A", "B") == ("A", "B", "activated")
assert wrapped_func.__name__ == positional_and_kw_arguments.__name__