Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix miscellaneous issues #38

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/doctest_eunit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
test(Tests) ->
test(Tests, rebar3_config).

test({_Desc, []}, _Opts) ->
ok;
test(Tests, rebar3_config) ->
eunit:test(Tests, rebar3_config_opts());
eunit:test({inparallel, Tests}, rebar3_config_opts());
test(Tests, Options) when is_list(Options) ->
eunit:test(Tests, Options).
eunit:test({inparallel, Tests}, Options).

%%%=====================================================================
%%% Support functions
Expand Down
114 changes: 61 additions & 53 deletions src/doctest_eunit_report.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ handle_begin(test, Data, #state{groups = Groups} = State) ->
{id, Id} = proplists:lookup(id, Data),
GroupId = lists:droplast(Id),
State#state{groups = orddict:update(GroupId, fun([Group, Tests]) ->
Test = test_info(maps:from_list(Data)),
[Group, orddict:append(Id, Test, Tests)]
case test_info(maps:from_list(Data)) of
#{source := {_Mod, doctest_test, 0}} ->
[Group, Tests];
Test ->
[Group, orddict:append(Id, Test, Tests)]
end
end, Groups)}.

handle_end(group, Data, #state{groups = Groups} = State) ->
Expand All @@ -72,9 +76,14 @@ handle_end(test, Data, #state{groups = Groups} = State) ->
{id, Id} = proplists:lookup(id, Data),
GroupId = lists:droplast(Id),
State#state{groups = orddict:update(GroupId, fun([Group, Tests]) ->
[Group, orddict:update(Id, fun([Test]) ->
[maps:merge(Test, maps:from_list(Data))]
end, Tests)]
case orddict:is_key(Id, Tests) of
true ->
[Group, orddict:update(Id, fun([Test]) ->
[maps:merge(Test, maps:from_list(Data))]
end, Tests)];
false ->
[Group, Tests]
end
end, Groups)}.

handle_cancel(group, Data, #state{groups = Groups} = State) ->
Expand All @@ -86,9 +95,14 @@ handle_cancel(test, Data, #state{groups = Groups} = State) ->
{id, Id} = proplists:lookup(id, Data),
GroupId = lists:droplast(Id),
State#state{groups = orddict:update(GroupId, fun([Group, Tests]) ->
[Group, orddict:update(Id, fun([Test]) ->
[maps:merge(Test, maps:from_list(Data))]
end, Tests)]
case orddict:is_key(Id, Tests) of
true ->
[Group, orddict:update(Id, fun([Test]) ->
[maps:merge(Test, maps:from_list(Data))]
end, Tests)];
false ->
[Group, Tests]
end
end, Groups)}.

terminate({ok, Data}, #state{groups = Groups} = _State) ->
Expand All @@ -97,8 +111,7 @@ terminate({ok, Data}, #state{groups = Groups} = _State) ->
print_footer(Data, Time),
{ok, Data};
terminate({error, Reason}, State) ->
% TODO: Print error
io:format(user, "[error] ~tp~n", [{Reason, State}]),
print_error(error, Reason, State),
{error, Reason}.

test_info(#{desc := <<"doctest ", Rest/binary>>} = D) ->
Expand Down Expand Up @@ -273,7 +286,15 @@ format_error({error, {assertEqual, Info}, Stacktrace}, Test) ->
print_test({assertEqual, Info}, Test, Stacktrace)
end;
format_error({error, {Reason, Info}, Stacktrace}, Test) ->
print_test({Reason, Info}, Test, Stacktrace);
case is_list(Info) andalso (
proplists:is_defined(expected, Info) orelse
proplists:is_defined(pattern, Info)
) of
true ->
print_test({Reason, Info}, Test, Stacktrace);
false ->
print_error(error, {Reason, Info}, Stacktrace)
end;
format_error({Class, Reason, Stacktrace}, _Test) ->
print_error(Class, Reason, Stacktrace).

Expand All @@ -300,50 +321,37 @@ print_doctest(#{ln_range := {FromLn, ToLn}} = _DocTest, {ErrReason, Info}, Test,
<<"\n">>
].

print_test({ErrReason, Info}, Test, _Stacktrace) ->
case is_list(Info) andalso (
proplists:is_defined(expected, Info) orelse
proplists:is_defined(pattern, Info)
) of
print_test({Reason, Info}, Test, _Stacktrace) ->
Left = proplists:get_value(expected, Info,
proplists:get_value(pattern, Info)),
Right = proplists:get_value(value, Info,
proplists:get_value(unexpected_success, Info,
proplists:get_value(unexpected_exception, Info))),
LeftFmt = case proplists:is_defined(pattern, Info) of
true ->
Left = proplists:get_value(expected, Info,
proplists:get_value(pattern, Info)),
Right = proplists:get_value(value, Info,
proplists:get_value(unexpected_success, Info,
proplists:get_value(unexpected_exception, Info))),
LeftFmt = case proplists:is_defined(pattern, Info) of
true ->
"~ts";
false ->
"~tp"
end,
RightFmt = "~tP",
Filename = maps:get(file, Test),
{line, Ln} = proplists:lookup(line, Info),
Lns = readlines(Filename),
LnExpr = lists:nth(Ln, Lns),
Pd = iolist_to_binary(lists:duplicate(byte_size(integer_to_binary(Ln)), <<"\s">>)),
[
<<"\s">>, Pd, <<"\s❌\s"/utf8>>, {{to_bin, ErrReason}, {fg, bright_black}}, <<"\n">>,
<<"\n">>,
<<"\s">>, Pd, <<"\sExpected: ">>, {{fmt, LeftFmt, [Left]}, {fg, green}}, <<"\n">>,
<<"\s">>, Pd, <<"\sReceived: ">>, {{fmt, RightFmt, [Right, ?EUNIT_DEBUG_VAL_DEPTH]}, {fg, red}}, <<"\n">>,
<<"\n">>,
format_pre_code(Test, Pd),
<<"\s">>, Pd, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\n">>,
<<"\s">>, {{to_bin, Ln}, {fg, red}}, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\s">>, LnExpr, <<"\n">>,
<<"\s">>, Pd, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\n">>,
<<"\s">>, Pd, <<"\s">>, {<<"└── at "/utf8>>, {fg, bright_black}}, {Filename, {fg, blue}}, {{fmt, ":~p", [Ln]}, {fg, blue}},
<<"\n">>
];
"~ts";
false ->
[
<<"\s\s\s❌\s"/utf8>>, {{to_bin, ErrReason}, {fg, bright_black}}, <<"\n">>,
<<"\n">>,
<<"\s\s\s">>, {{fmt, "~tp", [Info]}, {fg, red}},
<<"\n">>
]
end.
"~tp"
end,
RightFmt = "~tP",
Filename = maps:get(file, Test),
{line, Ln} = proplists:lookup(line, Info),
Lns = readlines(Filename),
LnExpr = lists:nth(Ln, Lns),
Pd = iolist_to_binary(lists:duplicate(byte_size(integer_to_binary(Ln)), <<"\s">>)),
[
<<"\s">>, Pd, <<"\s❌\s"/utf8>>, {{to_bin, Reason}, {fg, bright_black}}, <<"\n">>,
<<"\n">>,
<<"\s">>, Pd, <<"\sExpected: ">>, {{fmt, LeftFmt, [Left]}, {fg, green}}, <<"\n">>,
<<"\s">>, Pd, <<"\sReceived: ">>, {{fmt, RightFmt, [Right, ?EUNIT_DEBUG_VAL_DEPTH]}, {fg, red}}, <<"\n">>,
<<"\n">>,
format_pre_code(Test, Pd),
<<"\s">>, Pd, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\n">>,
<<"\s">>, {{to_bin, Ln}, {fg, red}}, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\s">>, LnExpr, <<"\n">>,
<<"\s">>, Pd, <<"\s">>, {<<"│"/utf8>>, {fg, bright_black}}, <<"\n">>,
<<"\s">>, Pd, <<"\s">>, {<<"└── at "/utf8>>, {fg, bright_black}}, {Filename, {fg, blue}}, {{fmt, ":~p", [Ln]}, {fg, blue}},
<<"\n">>
].

print_error(Class, Reason, Stacktrace) ->
[
Expand Down
29 changes: 18 additions & 11 deletions src/doctest_extract_tag.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ chunks({Mod, Forms}) ->
Comments = erl_comment_scan:file(Filename),
{Mod, _EDoc, Entries} =
edoc_extract:source(Filename, edoc_lib:get_doc_env([]), [return_entries]),
lists:map(fun({Ln, Data}) ->
EntryComments = search_entry_comments(Ln, Comments),
Doc = comments_to_binary(EntryComments),
case Data of
moduledoc ->
{token(Mod), Ln-1, Doc};
{doc, {F, A}} ->
{token({Mod, F, A}), Ln-1, Doc}
lists:filtermap(fun({Ln, Data}) ->
case search_entry_comments(Ln, Comments) of
{value, EntryComments} ->
Doc = comments_to_binary(EntryComments),
case Data of
moduledoc ->
{true, {token(Mod), Ln-1, Doc}};
{doc, {F, A}} ->
{true, {token({Mod, F, A}), Ln-1, Doc}}
end;
false ->
false
end
end, filtermap_entries(Entries)).

Expand Down Expand Up @@ -77,9 +81,12 @@ token(Mod) ->
{moduledoc, Mod, <<"@moduledoc">>}.

search_entry_comments(Ln, Comments) ->
{value, {_, _, _, EntryComments}} =
lists:search(fun({CLn, _, _, _}) -> CLn =:= Ln end, Comments),
EntryComments.
case lists:search(fun({CLn, _, _, _}) -> CLn =:= Ln end, Comments) of
{value, {_, _, _, EntryComments}} ->
{value, EntryComments};
false ->
false
end.

comments_to_binary(Comments) ->
iolist_to_binary(lists:join($\n, [rm_percentage(C) || C <- Comments])).
Expand Down
Loading