forked from facebook/TestSlide
/
dsl.py
288 lines (239 loc) · 10.2 KB
/
dsl.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import functools
import inspect
from functools import partial
from re import sub as _sub
from typing import Any, Callable, NoReturn, Optional, Union
from testslide.core import TestCase
from .lib import Context, Skip # noqa: F401
ExampleFunction = Callable[..., Any]
HaltingFunction = Callable[..., NoReturn]
def _validate_parameter(
code: ExampleFunction, name: str, index: int, allow_async: bool = True
) -> None:
parameters = list(inspect.signature(code).parameters.keys())
if not parameters or parameters[index] != name:
raise ValueError(
f"Function must receive parameter #{index+1} named "
f"'{name}', but given function has parameters: {parameters}."
)
if not allow_async and inspect.iscoroutinefunction(code):
raise RuntimeError(
f"TestSlide DSL context function `{code.__name__}` can not be async!"
)
def _require_context(action: str) -> Callable:
def wrapper(func: Callable) -> Callable:
@functools.wraps(func)
def func_with_context_validation(
self: "_DSLContext", *args: Any, **kwargs: Any
) -> None:
if not self.current_context:
raise TypeError("Can not {} without a parent context".format(action))
return func(self, *args, **kwargs)
return func_with_context_validation
return wrapper
class _DSLContext:
"""
This class implement TestSlide DSL. This is not intended to be used
directly.
"""
def __init__(
self,
current_context: Optional[Context] = None,
skip: bool = False,
focus: bool = False,
) -> None:
self.current_context = current_context
self.skip = skip
self.focus = focus
@staticmethod
def _not_callable(*args: Any, **kwargs: Any) -> NoReturn:
raise BaseException("This function should not be called outside test code.")
@staticmethod
def _name_from_function(func: ExampleFunction) -> str:
return _sub("_", " ", func.__name__)
def _create_context(
self, name: str, context_code: ExampleFunction, *args: Any, **kwargs: Any
) -> HaltingFunction:
if not self.current_context:
new_context = Context(name, skip=self.skip, focus=self.focus)
else:
new_context = self.current_context.add_child_context(
name, skip=self.skip, focus=self.focus
)
_validate_parameter(context_code, "context", 0, allow_async=False)
context_code(
type(self)(current_context=new_context, skip=self.skip, focus=self.focus),
*args,
**kwargs,
)
return self._not_callable
def __call__(
self, arg: Union[str, ExampleFunction]
) -> Union[partial, HaltingFunction]:
if callable(arg):
context_code = arg
name = self._name_from_function(context_code)
return self._create_context(name, context_code)
else:
name = arg
return functools.partial(self._create_context, name)
def _reset(self) -> None:
self.skip = False
self.focus = False
# nested contexts
def sub_context(
self, arg: Union[str, ExampleFunction]
) -> Union[partial, HaltingFunction]:
self._reset()
return self(arg)
def xsub_context(self, arg: ExampleFunction) -> HaltingFunction:
self._reset()
self.skip = True
return self(arg)
def fsub_context(self, arg: ExampleFunction) -> HaltingFunction:
self._reset()
self.focus = True
return self(arg)
# Examples
@_require_context("create example")
def _create_example(
self,
name: Optional[str],
example_code: ExampleFunction,
skip: bool,
focus: bool,
) -> HaltingFunction:
if name is None:
name = self._name_from_function(example_code)
_validate_parameter(example_code, "self", 0)
self.current_context.add_example(name, example_code, skip=skip, focus=focus) # type: ignore
return self._not_callable
def example(
self,
arg: Optional[Union[str, ExampleFunction]] = None,
skip: bool = False,
focus: bool = False,
skip_unless: bool = True,
) -> Union[partial, HaltingFunction]:
skip = skip or not skip_unless
if callable(arg):
example_code = arg
name = self._name_from_function(example_code)
return self._create_example(name, example_code, skip=skip, focus=focus)
else:
name = arg # type: ignore
return functools.partial(self._create_example, name, skip=skip, focus=focus)
def xexample(self, arg: Union[str, ExampleFunction]) -> HaltingFunction:
return self.example(arg, skip=True)
def fexample(self, arg: Union[str, ExampleFunction]) -> HaltingFunction:
return self.example(arg, focus=True)
# Shared contexts
@_require_context("create a shared context")
def _create_shared_context(
self, name: str, shared_context_code: ExampleFunction
) -> HaltingFunction:
_validate_parameter(shared_context_code, "context", 0)
self.current_context.add_shared_context(name, shared_context_code) # type: ignore
return self._not_callable
def shared_context(
self, arg: Union[str, ExampleFunction]
) -> Union[partial, HaltingFunction]:
if callable(arg):
shared_context_code = arg
name = self._name_from_function(shared_context_code)
return self._create_shared_context(name, shared_context_code)
else:
name = arg
return functools.partial(self._create_shared_context, name)
@_require_context("merge a shared context")
def merge_context(self, name: str, *args: Any, **kwargs: Any) -> None:
if name not in self.current_context.all_shared_contexts: # type: ignore
raise TypeError('Shared context "{}" does not exist'.format(name))
self.current_context.all_shared_contexts[name](self, *args, **kwargs) # type: ignore
@_require_context("merge a TestCase")
def merge_test_case(self, test_case: "TestCase", attr_name: str) -> HaltingFunction:
self.current_context.add_test_case(test_case, attr_name) # type:ignore
return self._not_callable
@_require_context("nest a shared context")
def nest_context(self, name: str, *args: Any, **kwargs: Any) -> None:
if name not in self.current_context.all_shared_contexts: # type:ignore
raise TypeError('Shared context "{}" does not exist'.format(name))
self._create_context(
name, self.current_context.all_shared_contexts[name], *args, **kwargs # type: ignore
)
# Helper function
@_require_context("create functions")
def function(self, function_code: ExampleFunction) -> HaltingFunction:
_validate_parameter(function_code, "self", 0)
self.current_context.add_function(function_code.__name__, function_code) # type: ignore
return self._not_callable
# Memoizable attributes
@_require_context("create memoizable attributes")
def memoize(
self,
name_or_code: Optional[Union[str, ExampleFunction]] = None,
memoizable_code: Optional[ExampleFunction] = None,
**kwargs: Any,
) -> HaltingFunction:
_memoizable_code: ExampleFunction
if name_or_code:
if kwargs:
raise ValueError("Invalid arguments!")
if memoizable_code: # name + code
name = name_or_code
_memoizable_code = memoizable_code
else: # used as decorator
name = name_or_code.__name__ # type: ignore
_memoizable_code = name_or_code # type: ignore
_validate_parameter(_memoizable_code, "self", 0)
self.current_context.add_memoized_attribute(name, _memoizable_code) # type: ignore
else: # kwargs
if name_or_code or memoizable_code:
raise ValueError("Invalid arguments!")
for name, code in kwargs.items():
self.memoize(name, code)
return self._not_callable
@_require_context("create a memoize before attribute")
def memoize_before(
self,
name_or_code: Union[str, ExampleFunction],
memoizable_code: Optional[ExampleFunction] = None,
) -> HaltingFunction:
_memoizable_code: ExampleFunction
if memoizable_code: # Got a lambda
name = name_or_code
_memoizable_code = memoizable_code
else: # Got a function
name = name_or_code.__name__ # type: ignore
_memoizable_code = name_or_code # type: ignore
_validate_parameter(_memoizable_code, "self", 0)
self.current_context.add_memoized_attribute(name, _memoizable_code, before=True) # type: ignore
return self._not_callable
# Hooks
@_require_context("register before hook")
def before(self, before_code: ExampleFunction) -> HaltingFunction:
_validate_parameter(before_code, "self", 0)
if not self.current_context:
raise TypeError("Can not register before hook at top level context")
self.current_context.before_functions.append(before_code)
return self._not_callable
@_require_context("register after hook")
def after(self, after_code: ExampleFunction) -> HaltingFunction:
_validate_parameter(after_code, "self", 0)
self.current_context.after_functions.append(after_code) # type: ignore
return self._not_callable
@_require_context("register around hook")
def around(self, around_code: ExampleFunction) -> HaltingFunction:
_validate_parameter(around_code, "self", 0)
_validate_parameter(around_code, "wrapped", 1)
if not self.current_context:
raise TypeError("Can not register around hook at top level context")
self.current_context.around_functions.append(around_code)
return self._not_callable
context = _DSLContext()
xcontext = _DSLContext(skip=True)
fcontext = _DSLContext(focus=True)