Support printing test vectors of Option type (needed for diversifiers).

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira Hopwood 2018-07-21 14:10:28 +01:00
parent c8036959ad
commit 3dfe532bac
1 changed files with 25 additions and 0 deletions

View File

@ -10,6 +10,12 @@ def chunk(h):
hstr = str(h, 'utf-8')
return '0x' + ', 0x'.join([hstr[i:i+2] for i in range(0, len(hstr), 2)])
class Some(object):
def __init__(self, thing):
self.thing = thing
def option(x):
return Some(x) if x else None
#
# JSON (with string comments)
@ -17,6 +23,9 @@ def chunk(h):
#
def tv_value_json(value, bitcoin_flavoured):
if isinstance(value, Some):
value = value.thing
if type(value) == bytes:
if bitcoin_flavoured and len(value) == 32:
value = value[::-1]
@ -54,6 +63,20 @@ def tv_bytes_rust(name, value, pad):
pad,
))
def tv_option_bytes_rust(name, value, pad):
if value:
print('''%s%s: Some([
%s%s
%s]),''' % (
pad,
name,
pad,
chunk(hexlify(value.thing)),
pad,
))
else:
print('%s%s: None,' % (pad, name))
def tv_int_rust(name, value, pad):
print('%s%s: %d,' % (pad, name, value))
@ -61,6 +84,8 @@ def tv_part_rust(name, value, indent=3):
pad = ' ' * indent
if type(value) == bytes:
tv_bytes_rust(name, value, pad)
elif isinstance(value, Some) or value is None:
tv_option_bytes_rust(name, value, pad)
elif type(value) == int:
tv_int_rust(name, value, pad)
else: