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

Implement an EUnit reporter #19

Merged
merged 7 commits into from
May 23, 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ By defining `resolve` as the EUnit options, `doctest` will try to resolve the op
> {eunit_opts, [no_tty, {report, {my_reporter, Opts}}]}.
> ```

#### Doctest EUnit Reporter

`doctest` has a built-in EUnit reporter called `doctest_eunit_report` for better visualization of the tests. It can be used by defining the below option in the `rebar.config` of your project:
```erlang
{eunit_opts, [no_tty, {report, {doctest_eunit_report, []}}]}.
```

> Currently, no options are expected/provided by the reporter.

An example of the `doctest_eunit_report` output:
![doctest_eunit_report](/assets/reporter-go-to-definition.gif)

## TODO

- [ ] More tests
Expand Down
Binary file added assets/reporter-go-to-definition.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@
{test, [ct, eunit]},
{check, [ct, eunit, dialyzer]}
]}.

{eunit_opts, [no_tty, {report, {doctest_eunit_report, []}}]}.
70 changes: 47 additions & 23 deletions src/doctest_eunit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
-moduledoc false.

% API functions
-export([test/1, test/2, moduledoc_tests/3, doc_tests/3]).
-export([test/1, test/2]).

% Support functions
-export([moduledoc_tests/3, doc_tests/3, test_title/2]).

%%%=====================================================================
%%% API functions
Expand All @@ -27,27 +30,32 @@ test(Tests) ->
test(Tests, resolve).

test(Tests, resolve) ->
% eunit:test(Tests, [no_tty, {report, {doctest_eunit_report, []}}]);
eunit:test(Tests, options());
test(Tests, Options) when is_list(Options) ->
eunit:test(Tests, Options).

%%%=====================================================================
%%% Support functions
%%%=====================================================================

moduledoc_tests(Mod, AttrLn, CodeBlocks) ->
case catch tests(Mod, AttrLn, CodeBlocks,
fun(Ln, {Left, LeftValue}, {_Right, RightValue}) ->
Desc = iolist_to_binary(
io_lib:format("-moduledoc | Ln ~p", [Ln])
),
{Desc, Ln, fun() ->
fun({Left, LeftLn, LeftValue}, {Right, RightLn, RightValue}) ->
{desc("-moduledoc", Mod, LeftLn), {Mod, moduledoc, 0}, fun() ->
case LeftValue =:= RightValue of
true ->
ok;
false ->
erlang:error({assertEqual, [
{doctest, [
{attribute, moduledoc}
]},
{doctest, #{
attribute => moduledoc,
left => {Left, LeftLn, LeftValue},
right => {Right, RightLn, RightValue},
ln_range => {LeftLn, RightLn}
}},
{module, Mod},
{line, Ln},
{line, LeftLn},
{expression, Left},
{expected, RightValue},
{value, LeftValue}
Expand All @@ -66,23 +74,23 @@ moduledoc_tests(Mod, AttrLn, CodeBlocks) ->

doc_tests({M, F, A}, AttrLn, CodeBlocks) ->
case catch tests(M, AttrLn, CodeBlocks,
fun(Ln, {Left, LeftValue}, {_Right, RightValue}) ->
Desc = iolist_to_binary(
io_lib:format("-doc | Ln ~p", [Ln])
),
{Desc, {M, F, A}, fun() ->
fun({Left, LeftLn, LeftValue}, {Right, RightLn, RightValue}) ->
{desc("-doc", M, LeftLn), {M, F, A}, fun() ->
case LeftValue =:= RightValue of
true ->
ok;
false ->
erlang:error({assertEqual, [
{doctest, [
{attribute, doc}
]},
{doctest, #{
attribute => doc,
left => {Left, LeftLn, LeftValue},
right => {Right, RightLn, RightValue},
ln_range => {LeftLn, RightLn}
}},
{module, M},
{function, F},
{arity, A},
{line, Ln},
{line, LeftLn},
{expression, Left},
{expected, RightValue},
{value, LeftValue}
Expand All @@ -99,6 +107,16 @@ doc_tests({M, F, A}, AttrLn, CodeBlocks) ->
])
end.

test_title(Mod, Ln) ->
Filename = case proplists:lookup(source, Mod:module_info(compile)) of
{source, Src} ->
Src;
none ->
code:which(Mod)
end,
[[], Rel] = string:split(Filename, filename:absname("./")),
iolist_to_binary(io_lib:format(".~s:~p", [Rel, Ln])).

%%%=====================================================================
%%% Internal functions
%%%=====================================================================
Expand All @@ -113,21 +131,27 @@ options() ->

tests(Mod, AttrLn, CodeBlocks, Callback) when
is_atom(Mod), is_integer(AttrLn), AttrLn > 0,
is_list(CodeBlocks), is_function(Callback, 3) ->
is_list(CodeBlocks), is_function(Callback, 2) ->
{ok, lists:foldl(fun({CodeBlock, {CBLn, _CBCol}}, Acc) ->
case doctest_md:code_block_asserts(CodeBlock, AttrLn + CBLn) of
{ok, Asserts} ->
element(2, lists:foldl(fun({{Left, Right}, Ln}, {Bindings, Acc1}) ->
lists:reverse(element(2, lists:foldl(fun({{Left, LeftLn}, {Right, RightLn}}, {Bindings, Acc1}) ->
{LeftValue, NewBindings} = eval(Left, Bindings),
{RightValue, []} = eval(Right, []),
Test = Callback(Ln, {Left, LeftValue}, {Right, RightValue}),
Test = Callback(
{Left, LeftLn, LeftValue},
{Right, RightLn, RightValue}
),
{NewBindings, [Test | Acc1]}
end, {[], Acc}, lists:reverse(Asserts)));
end, {[], Acc}, lists:reverse(Asserts))));
{error, Reason} ->
throw({error, Reason})
end
end, [], CodeBlocks)}.

desc(Tag, Mod, Ln) ->
iolist_to_binary(io_lib:format("~s\s~s", [Tag, test_title(Mod, Ln)])).

eval(Bin, Bindings) ->
{ok, Tokens, _} = erl_scan:string(binary_to_list(<<Bin/binary, $.>>)),
{ok, Exprs} = erl_parse:parse_exprs(Tokens),
Expand Down
Loading
Loading