tests for aeso:compiler:to_sophia_value/2
This commit is contained in:
parent
f866e24624
commit
dc4a2ca2f9
@ -54,27 +54,49 @@ encode_decode_test() ->
|
|||||||
ok.
|
ok.
|
||||||
|
|
||||||
encode_decode_sophia_test() ->
|
encode_decode_sophia_test() ->
|
||||||
{42} = encode_decode_sophia_string("int", "42"),
|
Check = fun(Type, Str) -> case {encode_decode_sophia_string(Type, Str), Str} of
|
||||||
{1} = encode_decode_sophia_string("bool", "true"),
|
{X, X} -> ok;
|
||||||
{0} = encode_decode_sophia_string("bool", "false"),
|
Other -> Other
|
||||||
{<<"Hello">>} = encode_decode_sophia_string("string", "\"Hello\""),
|
end end,
|
||||||
{<<"Hello">>, [1,2,3], {variant, 1, [1]}} =
|
ok = Check("int", "42"),
|
||||||
encode_decode_sophia_string(
|
ok = Check("bool", "true"),
|
||||||
"(string, list(int), option(bool))",
|
ok = Check("bool", "false"),
|
||||||
"\"Hello\", [1,2,3], Some(true)"),
|
ok = Check("string", "\"Hello\""),
|
||||||
|
ok = Check("(string, list(int), option(bool))",
|
||||||
|
"(\"Hello\", [1, 2, 3], Some(true))"),
|
||||||
|
ok = Check("variant", "Blue({[\"x\"] = 1})"),
|
||||||
|
ok = Check("r", "{x = (\"foo\", 0), y = Red}"),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
encode_decode_sophia_string(SophiaType, String) ->
|
encode_decode_sophia_string(SophiaType, String) ->
|
||||||
io:format("String ~p~n", [String]),
|
io:format("String ~p~n", [String]),
|
||||||
|
TypeDefs = [" type an_alias('a) = (string, 'a)\n",
|
||||||
|
" record r = {x : an_alias(int), y : variant}\n"
|
||||||
|
" datatype variant = Red | Blue(map(string, int))\n"],
|
||||||
Code = [ "contract MakeCall =\n"
|
Code = [ "contract MakeCall =\n"
|
||||||
, " function foo : ", SophiaType, " => _\n"
|
, " type arg_type = ", SophiaType, "\n"
|
||||||
|
, TypeDefs
|
||||||
|
, " function foo : arg_type => _\n"
|
||||||
, " function __call() = foo(", String, ")\n" ],
|
, " function __call() = foo(", String, ")\n" ],
|
||||||
{ok, _, {Types, _}, Args} = aeso_compiler:check_call(lists:flatten(Code), []),
|
case aeso_compiler:check_call(lists:flatten(Code), []) of
|
||||||
Arg = list_to_tuple(Args),
|
{ok, _, {[Type], _}, [Arg]} ->
|
||||||
Type = {tuple, Types},
|
io:format("Type ~p~n", [Type]),
|
||||||
io:format("Type ~p~n", [Type]),
|
Data = encode(Arg),
|
||||||
Data = encode(Arg),
|
Decoded = decode(Type, Data),
|
||||||
decode(Type, Data).
|
DecodeCode = [ "contract Decode =\n",
|
||||||
|
TypeDefs,
|
||||||
|
" function __decode(_ : ", SophiaType, ") = ()\n" ],
|
||||||
|
case aeso_compiler:to_sophia_value(DecodeCode, Decoded) of
|
||||||
|
{ok, Sophia} ->
|
||||||
|
lists:flatten(io_lib:format("~s", [prettypr:format(aeso_pretty:expr(Sophia))]));
|
||||||
|
{error, Err} ->
|
||||||
|
io:format("~s\n", [Err]),
|
||||||
|
{error, Err}
|
||||||
|
end;
|
||||||
|
{error, Err} ->
|
||||||
|
io:format("~s\n", [Err]),
|
||||||
|
{error, Err}
|
||||||
|
end.
|
||||||
|
|
||||||
encode_decode(T, D) ->
|
encode_decode(T, D) ->
|
||||||
?assertEqual(D, decode(T, encode(D))),
|
?assertEqual(D, decode(T, encode(D))),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user