Skip to content

Commit

Permalink
add tests for syntax tools conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
richcarl committed Jan 25, 2021
1 parent ebd6ac1 commit 35c8687
Showing 1 changed file with 47 additions and 4 deletions.
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);
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
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

0 comments on commit 35c8687

Please sign in to comment.