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

Formatting fails with assertMatch containing with. #98

Closed
7run opened this issue Jul 16, 2024 · 3 comments · Fixed by #100
Closed

Formatting fails with assertMatch containing with. #98

7run opened this issue Jul 16, 2024 · 3 comments · Fixed by #100

Comments

@7run
Copy link

7run commented Jul 16, 2024

When trying to format by efmt in a module that describes a unit test,
The formatting fails if the evaluation expression for ?assertMatch contains a guard.

Successful patterns

-module(test).

-include_lib("eunit/include/eunit.hrl").


match_test() ->
    ?assertMatch(<<_/binary>>, <<"hello">>).
rebar3 efmt -w test/test.erl
[2024-07-16T07:37:38Z INFO  efmt] Overwrote "test/test.erl"
[2024-07-16T07:37:38Z INFO  efmt] All files were formatted correctly!

Failure patterns

-module(test).

-include_lib("eunit/include/eunit.hrl").


match_test() ->
    ?assertMatch(Bin when is_binary(Bin), <<"hello">>).
rebar3 efmt -w test/test.erl
[2024-07-16T07:38:37Z ERROR efmt] Failed to format "test/test.erl"
    Parse failed:
    --> test/test.erl:8:18
    8 | ?assertMatch(Bin when is_binary(Bin), <<"hello">>).
      |                  ^ unexpected token
@sile
Copy link
Owner

sile commented Jul 16, 2024

Thank you for reporting this issue. However this is an expected behavior.
Because #73 has dropped the -include handling (mainly, as it's a bit error prune and to reduce maintenance costs), the current version of efmt assumes that each argument of unknown macros (that are not defined in the same file) is a well-formed expression.

There is a workaround by adding dummy macro definition in the same file as follows:

-module(test).

-include_lib("eunit/include/eunit.hrl").

-ifdef(DUMMY_MACRO_FOR_EFMT).
-define(assertMatch(Pattern, Value), ok).
-endif.


match_test() ->
    ?assertMatch(Bin when is_binary(Bin), <<"hello">>).

However, I think there is room for improvement in handling unknown macros.
I will consider and try possible approaches in the near future.

@7run
Copy link
Author

7run commented Jul 18, 2024

Thank you for your answer 😄

The workaround you gave us solved the problem.

@sile
Copy link
Owner

sile commented Jul 19, 2024

[FYI] Special handling for ?assertMatch() and ?assertNotMatch() has been added in #100, making the above workaround unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants