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

add library module for converting between erlfmt and syntax tools #237

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests for syntax tools conversion
  • Loading branch information
richcarl committed Jan 25, 2021
commit 35c86873c83ea2342feb4e1b3724aac9d0a6b33c
51 changes: 47 additions & 4 deletions test/erlfmt_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ end_per_group(_GroupName, _Config) ->
ok.

init_per_testcase(_TestCase, Config) ->
GroupProps = ?config(tc_group_properties, Config),
case proplists:get_bool(syntax_tools, GroupProps) of
true ->
put('$syntax_tools$', true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow I didn't know about this magic.
I know this is a minimal diff, but thinking of the next person reading this, maybe it would be better to use a parameter, since each test already has access to Config. Something like:

is_syntax_tools(Config) ->
    proplists:get_bool(syntax_tools, ?config(tc_group_properties, Config)).
    
 some_test(Config) ->
   RoundTripWithSyntaxTools = is_syntax_tools(Config)
   ...
   parse_form(Form, RoundTripWithSyntaxTools)

But I think this was a great way to get test coverage and it is really impressive that everything is passing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to pass things as parameters whenever I can, but all the test code would have to get the extra Options parameter passed all the way down to the parse_form/parse_forms functions, and that seemed too intrusive. I also thought about using meck to modify the behaviour of the test code, but that's also a bit yucky and causes a dep on meck. What do you prefer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer a parameter, since I don't think it needs to pass too far down.
But let's also hear what @michalmuskala has to say.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. It's not pretty, but it's practical. I actually wonder if we should try doing something in erlfmt_format_SUITE as well to leverage all the examples we have in there, though I'm not 100% sure how that would look like.

false ->
erase('$syntax_tools$')
end,
Config.

end_per_testcase(_TestCase, _Config) ->
Expand All @@ -124,6 +131,24 @@ groups() ->
annos,
shebang
]},
{parser_st, [parallel, syntax_tools], [
records,
attributes,
specs,
macro_call_exprs,
macro_call_pats,
macro_call_types,
macro_definitions,
functions_and_funs,
operators,
lists,
binaries,
maps,
clauses,
types,
annos,
shebang
]},
{smoke_tests, [parallel], [
{group, snapshot_tests},
smoke_test_cli,
Expand Down Expand Up @@ -176,6 +201,7 @@ all() ->
[
{group, smoke_tests},
{group, parser},
{group, parser_st},
{group, range_tests},
{group, pragma_tests},
{group, verbose_warning_tests}
Expand Down Expand Up @@ -736,12 +762,13 @@ clauses(Config) when is_list(Config) ->
{clause, _, {'catch', _, [{var, _, '_'}, {var, _, '_'}]}, empty, [
{atom, _, ok}
]},
{clause, _, {'catch', _, [{var, _, '_'}, {var, _, '_'}, {var, _, '_'}]}, empty, [
{clause, _, {'catch', _, [{var, _, '_'}, {var, _, '_'}, {var, _, 'T'}]}, empty, [
{atom, _, ok}
]}
]},
[]},
parse_expr("try ok of _ -> ok catch _ -> ok; _:_ -> ok; _:_:_ -> ok end")
%% Note: formatting may drop the Trace part if it's just an underscore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean in erlfmt or during the rountrip through erl_syntax?

parse_expr("try ok of _ -> ok catch _ -> ok; _:_ -> ok; _:_:T -> ok end")
).

types(Config) when is_list(Config) ->
Expand Down Expand Up @@ -952,7 +979,7 @@ parse_type(String) ->
parse_form(String) ->
case erlfmt:read_nodes_string("nofile", String) of
{ok, [Form], []} ->
Form;
maybe_roundtrip(Form);
{ok, _, [Warning | _]} ->
ct:fail(
"Expected successful parse: \n~ts\n for warning: ~ts",
Expand All @@ -968,7 +995,7 @@ parse_form(String) ->
parse_forms(String) ->
case erlfmt:read_nodes_string("nofile", String) of
{ok, Forms, []} ->
Forms;
maybe_roundtrip_list(Forms);
{ok, _, [Warning | _]} ->
ct:fail(
"Expected successful parse: \n~ts\n for warning: ~ts",
Expand All @@ -981,6 +1008,22 @@ parse_forms(String) ->
)
end.

maybe_roundtrip_list(Nodes) ->
case get('$syntax_tools$') of
true ->
[erlfmt_ast:st_to_erlfmt(erlfmt_ast:erlfmt_to_st(N)) || N <- Nodes];
_ ->
Nodes
end.

maybe_roundtrip(Node) ->
case get('$syntax_tools$') of
true ->
erlfmt_ast:st_to_erlfmt(erlfmt_ast:erlfmt_to_st(Node));
_ ->
Node
end.

smoke_test_cli(Config) when is_list(Config) ->
?assertMatch("Usage: erlfmt " ++ _, os:cmd(escript() ++ " -h")).

Expand Down