-
Notifications
You must be signed in to change notification settings - Fork 51
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
richcarl
wants to merge
4
commits into
main
Choose a base branch
from
syntaxtools-compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add tests for syntax tools conversion
- Loading branch information
commit 35c86873c83ea2342feb4e1b3724aac9d0a6b33c
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) -> | ||
|
@@ -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, | ||
|
@@ -176,6 +201,7 @@ all() -> | |
[ | ||
{group, smoke_tests}, | ||
{group, parser}, | ||
{group, parser_st}, | ||
{group, range_tests}, | ||
{group, pragma_tests}, | ||
{group, verbose_warning_tests} | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean in erlfmt or during the rountrip through |
||
parse_expr("try ok of _ -> ok catch _ -> ok; _:_ -> ok; _:_:T -> ok end") | ||
). | ||
|
||
types(Config) when is_list(Config) -> | ||
|
@@ -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", | ||
|
@@ -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", | ||
|
@@ -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")). | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
But I think this was a great way to get test coverage and it is really impressive that everything is passing.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.