mirror of
https://github.com/zap-proto/erlang.git
synced 2026-10-30 15:05:14 +00:00
87 lines
2.4 KiB
Erlang
Executable File
87 lines
2.4 KiB
Erlang
Executable File
#!/usr/bin/env escript
|
|
%% -*- mode: erlang -*-
|
|
%%! -pa /usr/local/include
|
|
|
|
-include("test.capnp.hrl").
|
|
-define(SKIP, 127).
|
|
|
|
main([File, Case, Test]) ->
|
|
{ok, Message} = ecapnp_message:read_file(File),
|
|
run_test(Case, Test, Message);
|
|
main([Case, Test]) ->
|
|
run_test(Case, Test, undefined);
|
|
main(Args) ->
|
|
io:format(standard_error, "ecapnp_test unknown args: ~p~n", [Args]),
|
|
halt(?SKIP).
|
|
|
|
run_test(Case, Test, Message) when is_list(Case) ->
|
|
try
|
|
run_test(list_to_atom(Case), list_to_atom(Test), Message)
|
|
catch
|
|
Class:Error ->
|
|
io:format(standard_error, "ecapnp_test error: ~p:~p ~p~n", [Class, Error, erlang:get_stacktrace()])
|
|
end;
|
|
|
|
run_test(decode, Test, Message) ->
|
|
decode(Test, Message);
|
|
run_test(encode, Test, undefined) ->
|
|
io:setopts([{encoding, unicode}]),
|
|
io:put_chars( ecapnp_message:write(encode(Test)) );
|
|
run_test(Action, Test, _) ->
|
|
not_implemented(Action, Test).
|
|
|
|
not_implemented(Action, Test) ->
|
|
io:format(standard_error, "Unknown test: ~p ~p.~n", [Action, Test]),
|
|
halt(?SKIP).
|
|
|
|
|
|
decode(simpleTest, Msg) ->
|
|
{ok, Root} = test(root, 'SimpleTest', Msg),
|
|
io:format("(int = ~b, msg = \"~s\")~n",
|
|
[test(get, int, Root), test(get, msg, Root)]);
|
|
|
|
decode(textListTypeTest, Msg) ->
|
|
{ok, Root} = test(root, 'ListTest', Msg),
|
|
io:format("(textList = [~s])~n",
|
|
[string:join(
|
|
[io_lib:format("\"~s\"", [S])
|
|
|| S <- test(get, textList, Root)],
|
|
", ")]
|
|
);
|
|
|
|
decode(uInt8DefaultValueTest, Msg) ->
|
|
{ok, Root} = test(root, 'TestDefaults', Msg),
|
|
io:format("(uInt8Field = ~b)~n",
|
|
[test(get, uInt8Field, Root)]);
|
|
|
|
decode(constTest, _Msg) ->
|
|
io:format("(msg = \"~s\")~n", [test(const, constTestValue)]);
|
|
|
|
decode(Test, _) ->
|
|
not_implemented(decode, Test).
|
|
|
|
|
|
encode(simpleTest) ->
|
|
{ok, Root} = test(root, 'SimpleTest'),
|
|
ok = test(set, int, 1234567890, Root),
|
|
ok = test(set, msg, <<"a short message...">>, Root),
|
|
Root;
|
|
|
|
encode(textListTypeTest) ->
|
|
{ok, Root} = test(root, 'ListTest'),
|
|
ok = test(set, textList, [<<"foo">>, <<"bar">>, <<"baz">>], Root),
|
|
Root;
|
|
|
|
encode(uInt8DefaultValueTest) ->
|
|
{ok, Root} = test(root, 'TestDefaults'),
|
|
ok = test(set, uInt8Field, 0, Root),
|
|
Root;
|
|
|
|
encode(constTest) ->
|
|
{ok, Root} = test(root, 'SimpleTest'),
|
|
ok = test(set, msg, test(const, constTestValue), Root),
|
|
Root;
|
|
|
|
encode(Test) ->
|
|
not_implemented(encode, Test).
|