2019-05-21 20:28:47 +00:00
|
|
|
from typing import Dict, Any, Callable, Iterable
|
2019-05-05 22:31:57 +00:00
|
|
|
from eth2spec.debug.encode import encode
|
2019-06-11 14:42:50 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import Container
|
2019-05-05 22:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def spectest(description: str = None):
|
|
|
|
def runner(fn):
|
2019-05-06 13:40:18 +00:00
|
|
|
# this wraps the function, to hide that the function actually is yielding data, instead of returning once.
|
2019-05-05 22:31:57 +00:00
|
|
|
def entry(*args, **kw):
|
|
|
|
# check generator mode, may be None/else.
|
|
|
|
# "pop" removes it, so it is not passed to the inner function.
|
|
|
|
if kw.pop('generator_mode', False) is True:
|
|
|
|
out = {}
|
|
|
|
if description is None:
|
|
|
|
# fall back on function name for test description
|
|
|
|
name = fn.__name__
|
|
|
|
if name.startswith('test_'):
|
|
|
|
name = name[5:]
|
|
|
|
out['description'] = name
|
|
|
|
else:
|
|
|
|
# description can be explicit
|
|
|
|
out['description'] = description
|
2019-05-23 20:26:36 +00:00
|
|
|
has_contents = False
|
2019-05-05 22:31:57 +00:00
|
|
|
# put all generated data into a dict.
|
|
|
|
for data in fn(*args, **kw):
|
2019-05-23 20:26:36 +00:00
|
|
|
has_contents = True
|
2019-05-05 22:31:57 +00:00
|
|
|
# If there is a type argument, encode it as that type.
|
|
|
|
if len(data) == 3:
|
|
|
|
(key, value, typ) = data
|
|
|
|
out[key] = encode(value, typ)
|
|
|
|
else:
|
2019-05-06 13:40:18 +00:00
|
|
|
# Otherwise, try to infer the type, but keep it as-is if it's not a SSZ container.
|
2019-05-05 22:31:57 +00:00
|
|
|
(key, value) = data
|
2019-06-11 14:42:50 +00:00
|
|
|
if isinstance(value, Container):
|
2019-05-06 13:40:18 +00:00
|
|
|
out[key] = encode(value, value.__class__)
|
|
|
|
else:
|
2019-06-11 14:42:50 +00:00
|
|
|
# not a ssz value.
|
|
|
|
# It could be vector or bytes still, but it is a rare case,
|
|
|
|
# and lists can't be inferred fully (generics lose element type).
|
|
|
|
# In such cases, explicitly state the type of the yielded value as a third yielded object.
|
2019-05-06 13:40:18 +00:00
|
|
|
out[key] = value
|
2019-05-23 20:26:36 +00:00
|
|
|
if has_contents:
|
|
|
|
return out
|
|
|
|
else:
|
|
|
|
return None
|
2019-05-05 22:31:57 +00:00
|
|
|
else:
|
|
|
|
# just complete the function, ignore all yielded data, we are not using it
|
|
|
|
for _ in fn(*args, **kw):
|
|
|
|
continue
|
2019-05-23 20:26:36 +00:00
|
|
|
return None
|
2019-05-05 22:31:57 +00:00
|
|
|
return entry
|
|
|
|
return runner
|
|
|
|
|
|
|
|
|
2019-05-21 20:28:47 +00:00
|
|
|
def with_tags(tags: Dict[str, Any]):
|
|
|
|
"""
|
|
|
|
Decorator factory, adds tags (key, value) pairs to the output of the function.
|
|
|
|
Useful to build test-vector annotations with.
|
|
|
|
This decorator is applied after the ``spectest`` decorator is applied.
|
|
|
|
:param tags: dict of tags
|
|
|
|
:return: Decorator.
|
|
|
|
"""
|
|
|
|
def runner(fn):
|
|
|
|
def entry(*args, **kw):
|
|
|
|
fn_out = fn(*args, **kw)
|
|
|
|
# do not add tags if the function is not returning a dict at all (i.e. not in generator mode)
|
|
|
|
if fn_out is None:
|
2019-05-23 20:28:11 +00:00
|
|
|
return None
|
2019-05-21 20:28:47 +00:00
|
|
|
return {**tags, **fn_out}
|
|
|
|
return entry
|
|
|
|
return runner
|
|
|
|
|
|
|
|
|
|
|
|
def with_args(create_args: Callable[[], Iterable[Any]]):
|
|
|
|
"""
|
|
|
|
Decorator factory, adds given extra arguments to the decorated function.
|
|
|
|
:param create_args: function to create arguments with.
|
|
|
|
:return: Decorator.
|
|
|
|
"""
|
2019-05-05 22:31:57 +00:00
|
|
|
def runner(fn):
|
|
|
|
# this wraps the function, to hide that the function actually yielding data.
|
|
|
|
def entry(*args, **kw):
|
2019-05-21 20:28:47 +00:00
|
|
|
return fn(*(list(create_args()) + list(args)), **kw)
|
2019-05-05 22:31:57 +00:00
|
|
|
return entry
|
|
|
|
return runner
|