Skip to content

Commit

Permalink
add new test, and case
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Oct 24, 2024
1 parent 5d32c9c commit d1f6cfc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions Untitled
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 10 additions & 2 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@

-spec default(Rule :: atom()) -> DefaultRuleConfig :: term().
default(no_init_lists) ->
#{behaviours => [gen_server, gen_statem]};
#{behaviours =>
[gen_server, gen_statem, gen_fsm, supervisor, supervisor_bridge, gen_event]};
default(macro_names) ->
#{regex => "^[A-Z](_?[A-Z0-9]+)*$"};
default(operator_spaces) ->
Expand Down Expand Up @@ -1133,7 +1134,14 @@ no_init_lists(Config, Target, RuleConfig) ->
andalso ktn_code:attr(name, Node) == init
andalso ktn_code:attr(arity, Node) == 1
end,
[Init1Fun] = elvis_code:find(IsInit1Function, Root),

Init1Fun =
case elvis_code:find(IsInit1Function, Root) of
[] ->
[];
[Fun] ->
Fun
end,

FilterClauses =
fun(Clause) ->
Expand Down
12 changes: 12 additions & 0 deletions test/examples/no_init_lists_examples/fail_no_init_lists5.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-module(fail_no_init_lists5).

-behaviour(gen_fsm).

-export([init/1, handle_sync_event/4, handle_event/3]).

init([]) -> {error, "Don't use list for init/1"}.

handle_sync_event(_, _, _, _) -> ok.

handle_event(_, _, _) -> ok.

7 changes: 7 additions & 0 deletions test/examples/no_init_lists_examples/fail_no_init_lists6.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(fail_no_init_lists6).

-behaviour(supervisor).

-export([init/1]).

init([]) -> {error, "Don't use list for init/1"}.
9 changes: 9 additions & 0 deletions test/examples/no_init_lists_examples/fail_no_init_lists7.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-module(fail_no_init_lists7).

-behaviour(supervisor_bridge).

-export([init/1, terminate/2]).

init([]) -> {error, "Don't use list for init/1"}.

terminate(_, _) -> ok.
11 changes: 11 additions & 0 deletions test/examples/no_init_lists_examples/fail_no_init_lists8.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(fail_no_init_lists8).

-behaviour(gen_event).

-export([init/1, handle_event/2, handle_call/2]).

init([]) -> {error, "Don't use list for init/1"}.

handle_event(_, _) -> ok.

handle_call(_, _) -> ok.
8 changes: 8 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1530,11 +1530,19 @@ verify_no_init_lists(Config) ->
FailPath2 = ExamplesDir ++ "fail_no_init_lists2." ++ Ext,
FailPath3 = ExamplesDir ++ "fail_no_init_lists3." ++ Ext,
FailPath4 = ExamplesDir ++ "fail_no_init_lists4." ++ Ext,
FailPath5 = ExamplesDir ++ "fail_no_init_lists5." ++ Ext,
FailPath6 = ExamplesDir ++ "fail_no_init_lists6." ++ Ext,
FailPath7 = ExamplesDir ++ "fail_no_init_lists7." ++ Ext,
FailPath8 = ExamplesDir ++ "fail_no_init_lists8." ++ Ext,

[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath2),
[_, _, _] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath3),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath4),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath5),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath6),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath7),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath8),

PassPath = ExamplesDir ++ "pass_no_init_lists." ++ Ext,
PassPath2 = ExamplesDir ++ "pass_no_init_lists2." ++ Ext,
Expand Down

0 comments on commit d1f6cfc

Please sign in to comment.