Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/atomvm_spectrometer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ parse_ecosystem_args([Unknown | _], _Opts) ->
parse_supported_args([], Opts) ->
Opts;
parse_supported_args(["--module", Mod | Rest], Opts) ->
Bin = spectrometer_utils:normalize_module_name(Mod),
Bin = spectrometer_utils:normalize_module_name(Mod, true),
parse_supported_args(Rest, Opts#{module => Bin});
parse_supported_args(["-m", Mod | Rest], Opts) ->
Bin = spectrometer_utils:normalize_module_name(Mod),
Bin = spectrometer_utils:normalize_module_name(Mod, true),
parse_supported_args(Rest, Opts#{module => Bin});
parse_supported_args(["--cache", Dir | Rest], Opts) ->
parse_supported_args(Rest, Opts#{cache_dir => Dir});
Expand Down
3 changes: 3 additions & 0 deletions src/spectrometer_help.erl
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ usage_supported() ->
"\n"
"Options:\n"
" --module <mod> Show functions for a specific OTP module\n"
" (Elixir modules can omit the Elixir. prefix)\n"
" -m <mod> Same as --module\n"
" --erl Show only Erlang functions (exclude Elixir)\n"
" --ex Filter output to Elixir modules (does not rename or strip module prefixes)\n"
Expand All @@ -194,6 +195,8 @@ usage_supported() ->
" spectrometer supported\n"
" spectrometer supported --module gen_server\n"
" spectrometer supported -m lists\n"
" spectrometer supported -m Elixir.List\n"
" spectrometer supported -m List\n"
" spectrometer supported --ex\n"
" spectrometer supported --erl\n"
" spectrometer supported -c /tmp/custom_cache\n"
Expand Down
23 changes: 4 additions & 19 deletions src/spectrometer_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ directory removal, and GitHub URL normalization for deduplication.
is_elixir_module_name/1,
make_temp_dir/1,
normalize_github_url/1,
normalize_module_name/1,
normalize_module_name/2,
normalize_platform_name/1,
purge_dir/1,
Expand Down Expand Up @@ -405,35 +404,21 @@ os_temp_dir() ->
"/tmp"
end.

-doc "Detect if a module name is Elixir-style (starts with literal Elixir. prefix).".
-doc "Detect if a module name is capitalized (Elixir style).".
-spec is_elixir_module_name(atom() | string() | binary()) -> boolean().
is_elixir_module_name(Atom) when is_atom(Atom) ->
is_elixir_module_name(atom_to_list(Atom));
is_elixir_module_name(Str) when is_list(Str) ->
case Str of
"Elixir." ++ _ -> true;
case normalize_module_name(Str, true) of
<<"Elixir.", _/binary>> -> true;
_ -> false
end;
is_elixir_module_name(Bin) when is_binary(Bin) ->
case Bin of
case normalize_module_name(Bin, true) of
<<"Elixir.", _/binary>> -> true;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
_ -> false
end.

-doc """
Normalize module name without Elixir prefixing. 'Elixir.GPIO' -> <<"Elixir.GPIO">>,
'GPIO' -> <<"GPIO">>, 'lists' -> <<"lists">>. Delegates to normalize_module_name/2 with
ElixirFlag = false. For Elixir-prefixed output, use normalize_module_name/2 with
ElixirFlag = true. Returns a binary().
""".
-spec normalize_module_name(string() | atom() | binary()) -> binary().
normalize_module_name(Atom) when is_atom(Atom) ->
normalize_module_name(atom_to_list(Atom));
normalize_module_name(Str) when is_list(Str) ->
normalize_module_name(Str, false);
normalize_module_name(Str) when is_binary(Str) ->
normalize_module_name(Str, false).

-doc """
Normalize module name.

Expand Down
34 changes: 34 additions & 0 deletions test/atomvm_spectrometer_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,40 @@ parse_supported_args_short_module_test_() ->
?assertEqual(<<"maps">>, maps:get(module, Opts))
end}.

parse_supported_args_elxir_module_test_() ->
{"normalizes Elixir module names with prefix", fun() ->
% Capitalized module name should get Elixir. prefix
{command, supported, Opts1} = atomvm_spectrometer:parse_args([
"supported", "-m", "List"
]),
?assertEqual(<<"Elixir.List">>, maps:get(module, Opts1)),
% Already-prefixed module name stays as-is
{command, supported, Opts2} = atomvm_spectrometer:parse_args([
"supported", "-m", "Elixir.List"
]),
?assertEqual(<<"Elixir.List">>, maps:get(module, Opts2)),
% Erlang module name (lowercase) stays as-is
{command, supported, Opts3} = atomvm_spectrometer:parse_args([
"supported", "-m", "lists"
]),
?assertEqual(<<"lists">>, maps:get(module, Opts3)),
% Long form --module: Capitalized module name should get Elixir. prefix
{command, supported, Opts4} = atomvm_spectrometer:parse_args([
"supported", "--module", "List"
]),
?assertEqual(<<"Elixir.List">>, maps:get(module, Opts4)),
% Long form --module: Already-prefixed module name stays as-is
{command, supported, Opts5} = atomvm_spectrometer:parse_args([
"supported", "--module", "Elixir.List"
]),
?assertEqual(<<"Elixir.List">>, maps:get(module, Opts5)),
% Long form --module: Erlang module name (lowercase) stays as-is
{command, supported, Opts6} = atomvm_spectrometer:parse_args([
"supported", "--module", "lists"
]),
?assertEqual(<<"lists">>, maps:get(module, Opts6))
end}.

parse_supported_args_erl_test_() ->
{"parses --erl flag", fun() ->
{command, supported, Opts} = atomvm_spectrometer:parse_args(
Expand Down
30 changes: 9 additions & 21 deletions test/spectrometer_elixir_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,11 @@ parse_query_8_formats_test() ->
%% normalize_module tests
%% =============================================================================

normalize_gpio_test() ->
?assertEqual(
<<"GPIO">>,
spectrometer_utils:normalize_module_name("GPIO")
).

normalize_elixir_gpio_test() ->
?assertEqual(
<<"Elixir.GPIO">>,
spectrometer_utils:normalize_module_name("Elixir.GPIO")
).

normalize_lists_test() ->
normalize_with_flag_test() ->
?assertEqual(
<<"lists">>,
spectrometer_utils:normalize_module_name("lists")
).

normalize_with_flag_test() ->
spectrometer_utils:normalize_module_name("lists", false)
),
?assertEqual(
<<"Elixir.GPIO">>,
spectrometer_utils:normalize_module_name("GPIO", true)
Expand All @@ -80,9 +66,11 @@ is_elixir_module_name_elxir_prefix_test() ->
true, spectrometer_utils:is_elixir_module_name("Elixir.MyModule")
).

is_elixir_module_name_lowercase_test() ->
is_elixir_module_name_case_test() ->
?assertEqual(false, spectrometer_utils:is_elixir_module_name("lists")),
?assertEqual(false, spectrometer_utils:is_elixir_module_name("maps")),
% Uppercase without Elixir prefix is no longer considered Elixir
?assertEqual(false, spectrometer_utils:is_elixir_module_name("GPIO")),
?assertEqual(false, spectrometer_utils:is_elixir_module_name("MyModule")).
?assertEqual(true, spectrometer_utils:is_elixir_module_name("GPIO")),
?assertEqual(true, spectrometer_utils:is_elixir_module_name("MyModule")),
% Binary inputs
?assertEqual(true, spectrometer_utils:is_elixir_module_name(<<"GPIO">>)),
?assertEqual(false, spectrometer_utils:is_elixir_module_name(<<"lists">>)).
22 changes: 1 addition & 21 deletions test/spectrometer_utils_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -361,29 +361,9 @@ start_applications_success_test_() ->
?_assertEqual(ok, spectrometer_utils:start_applications())}.

%% =============================================================================
%% normalize_module_name/1 and /2 tests
%% normalize_module_name/2 tests
%% =============================================================================

normalize_module_name_string_test_() ->
{"normalizes string module name",
?_assertEqual(
<<"Elixir.GPIO">>,
spectrometer_utils:normalize_module_name("Elixir.GPIO")
)}.

normalize_module_name_atom_test_() ->
{"normalizes atom module name",
?_assertEqual(
<<"Elixir.GPIO">>,
spectrometer_utils:normalize_module_name('Elixir.GPIO')
)}.

normalize_module_name_non_elixir_atom_test_() ->
{"normalizes atom without Elixir prefix",
?_assertEqual(
<<"lists">>, spectrometer_utils:normalize_module_name(lists)
)}.

normalize_module_name_2_atom_test_() ->
{"normalize_module_name/2 with atom and ElixirFlag=true",
?_assertEqual(
Expand Down
Loading