2019-07-26 17:19:36 +00:00
|
|
|
from typing import Dict, Any
|
2020-01-24 23:43:43 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_typing import View
|
2019-07-29 22:40:01 +00:00
|
|
|
from eth2spec.utils.ssz.ssz_impl import serialize
|
2019-05-05 22:31:57 +00:00
|
|
|
|
|
|
|
|
2019-07-26 21:50:11 +00:00
|
|
|
def vector_test(description: str = None):
|
2019-07-26 17:19:36 +00:00
|
|
|
"""
|
2019-07-26 21:50:11 +00:00
|
|
|
vector_test decorator: Allow a caller to pass "generator_mode=True" to make the test yield data,
|
|
|
|
but behave like a normal test (ignoring the yield, but fully processing) a test when not in "generator_mode"
|
|
|
|
This should always be the most outer decorator around functions that yield data.
|
|
|
|
This is to deal with silent iteration through yielding function when in a pytest
|
|
|
|
context (i.e. not in generator mode).
|
2019-07-26 17:19:36 +00:00
|
|
|
:param description: Optional description for the test to add to the metadata.
|
|
|
|
:return: Decorator.
|
|
|
|
"""
|
2019-05-05 22:31:57 +00:00
|
|
|
def runner(fn):
|
2019-07-26 17:19:36 +00:00
|
|
|
# this wraps the function, to yield type-annotated entries of data.
|
|
|
|
# Valid types are:
|
|
|
|
# - "meta": all key-values with this type can be collected by the generator, to put somewhere together.
|
|
|
|
# - "ssz": raw SSZ bytes
|
|
|
|
# - "data": a python structure to be encoded by the user.
|
2019-05-05 22:31:57 +00:00
|
|
|
def entry(*args, **kw):
|
2019-07-26 17:19:36 +00:00
|
|
|
|
2019-07-26 21:50:11 +00:00
|
|
|
def generator_mode():
|
2019-07-26 17:19:36 +00:00
|
|
|
if description is not None:
|
2019-05-05 22:31:57 +00:00
|
|
|
# description can be explicit
|
2019-07-26 17:19:36 +00:00
|
|
|
yield 'description', 'meta', description
|
|
|
|
|
|
|
|
# transform the yielded data, and add type annotations
|
2019-05-05 22:31:57 +00:00
|
|
|
for data in fn(*args, **kw):
|
2019-07-26 22:26:05 +00:00
|
|
|
# if not 2 items, then it is assumed to be already formatted with a type:
|
|
|
|
# e.g. ("bls_setting", "meta", 1)
|
|
|
|
if len(data) != 2:
|
|
|
|
yield data
|
|
|
|
continue
|
|
|
|
# Try to infer the type, but keep it as-is if it's not a SSZ type or bytes.
|
|
|
|
(key, value) = data
|
2019-07-29 22:40:01 +00:00
|
|
|
if value is None:
|
|
|
|
continue
|
2020-01-24 23:43:43 +00:00
|
|
|
if isinstance(value, View):
|
2019-07-29 22:40:01 +00:00
|
|
|
yield key, 'ssz', serialize(value)
|
|
|
|
elif isinstance(value, bytes):
|
|
|
|
yield key, 'ssz', value
|
2020-01-24 23:43:43 +00:00
|
|
|
elif isinstance(value, list) and all([isinstance(el, (View, bytes)) for el in value]):
|
2019-07-26 22:26:05 +00:00
|
|
|
for i, el in enumerate(value):
|
2020-01-24 23:43:43 +00:00
|
|
|
if isinstance(el, View):
|
2019-07-29 22:40:01 +00:00
|
|
|
yield f'{key}_{i}', 'ssz', serialize(el)
|
2019-07-30 12:08:50 +00:00
|
|
|
elif isinstance(el, bytes):
|
2019-07-29 22:40:01 +00:00
|
|
|
yield f'{key}_{i}', 'ssz', el
|
2019-07-26 22:26:05 +00:00
|
|
|
yield f'{key}_count', 'meta', len(value)
|
|
|
|
else:
|
|
|
|
# Not a ssz value.
|
|
|
|
# The data will now just be yielded as any python data,
|
2020-10-08 18:34:59 +00:00
|
|
|
# something that should be encodable by the generator runner.
|
2019-07-26 22:26:05 +00:00
|
|
|
yield key, 'data', value
|
2019-07-26 21:50:11 +00:00
|
|
|
|
|
|
|
# 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:
|
|
|
|
# return the yielding function as a generator object.
|
|
|
|
# Don't yield in this function itself, that would make pytest skip over it.
|
|
|
|
return generator_mode()
|
2019-05-05 22:31:57 +00:00
|
|
|
else:
|
2019-07-26 17:19:36 +00:00
|
|
|
# Just complete the function, ignore all yielded data,
|
|
|
|
# we are not using it (or processing it, i.e. nearly zero efficiency loss)
|
|
|
|
# Pytest does not support yielded data in the outer function, so we need to wrap it like this.
|
2019-05-05 22:31:57 +00:00
|
|
|
for _ in fn(*args, **kw):
|
|
|
|
continue
|
2019-05-23 20:26:36 +00:00
|
|
|
return None
|
2019-07-26 17:19:36 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
return entry
|
2019-07-26 17:19:36 +00:00
|
|
|
|
2019-05-05 22:31:57 +00:00
|
|
|
return runner
|
|
|
|
|
|
|
|
|
2019-07-26 17:19:36 +00:00
|
|
|
def with_meta_tags(tags: Dict[str, Any]):
|
2019-05-21 20:28:47 +00:00
|
|
|
"""
|
2019-07-26 17:19:36 +00:00
|
|
|
Decorator factory, yields meta tags (key, value) pairs to the output of the function.
|
2019-05-21 20:28:47 +00:00
|
|
|
Useful to build test-vector annotations with.
|
|
|
|
:param tags: dict of tags
|
|
|
|
:return: Decorator.
|
|
|
|
"""
|
|
|
|
def runner(fn):
|
|
|
|
def entry(*args, **kw):
|
2019-07-26 17:19:36 +00:00
|
|
|
yielded_any = False
|
|
|
|
for part in fn(*args, **kw):
|
|
|
|
yield part
|
|
|
|
yielded_any = True
|
|
|
|
# Do not add tags if the function is not returning a dict at all (i.e. not in generator mode).
|
|
|
|
# As a pytest, we do not want to be yielding anything (unsupported by pytest)
|
|
|
|
if yielded_any:
|
2019-07-26 21:50:11 +00:00
|
|
|
for k, v in tags.items():
|
2019-07-26 17:19:36 +00:00
|
|
|
yield k, 'meta', v
|
2019-05-21 20:28:47 +00:00
|
|
|
return entry
|
|
|
|
return runner
|