Skip to content

Commit

Permalink
[#22] Add basic tests for code navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi committed Jul 31, 2019
1 parent 8eec625 commit 191198d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/erlang_ls_server.erl
Expand Up @@ -26,6 +26,11 @@
-export([ connected/3
]).

%% TODO: Move to separate module
-export([ definition/1
, search/3
]).

%%==============================================================================
%% Includes
%%==============================================================================
Expand Down Expand Up @@ -202,6 +207,7 @@ send_notification(Socket, Method, Params) ->
lager:debug("[SERVER] Sending notification [notification=~p]", [Notification]),
gen_tcp:send(Socket, Notification).

%% TODO: definition/2 should probably not take the Uri, but a path.
-spec definition(uri(), erlang_ls_parser:poi()) -> null | map().
definition(_Uri, #{ info := {application, {M, _F, _A}} = Info }) ->
case annotated_tree(erlang_ls_uri:filename(M)) of
Expand Down
48 changes: 41 additions & 7 deletions test/erlang_ls_code_navigation_SUITE.erl
Expand Up @@ -13,7 +13,8 @@
]).

%% Test cases
-export([ main/1
-export([ macro/1
, record/1
]).

%%==============================================================================
Expand All @@ -36,7 +37,13 @@ suite() ->

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
Config.
DataDir = ?config(data_dir, Config),
DataDirBin = list_to_binary(DataDir),
[ {data_dir_bin, DataDirBin}
, {include_path, [ filename:join([DataDirBin, "src"])
, filename:join([DataDirBin, "include"])
]}
|Config].

-spec end_per_suite(config()) -> ok.
end_per_suite(_Config) ->
Expand All @@ -52,18 +59,45 @@ end_per_testcase(_TestCase, _Config) ->

-spec all() -> [atom()].
all() ->
[main].
[macro, record].

%%==============================================================================
%% Testcases
%%==============================================================================
-spec macro(config()) -> ok.
macro(Config) ->
FileName = <<"code_navigation.erl">>,
Thing = {macro, 'MACRO_A'},
Definition = definition(?config(data_dir_bin, Config), Thing),
?assertEqual( Definition
, erlang_ls_server:search( FileName
, ?config(include_path, Config)
, erlang_ls_server:definition(Thing))),
ok.

-spec main(config()) -> ok.
main(_Config) ->
%% First stream allowed
?assert(true),
-spec record(config()) -> ok.
record(Config) ->
FileName = <<"code_navigation.erl">>,
Thing = {record_expr, "record_a"},
Definition = definition(?config(data_dir_bin, Config), Thing),
?assertEqual( Definition
, erlang_ls_server:search( FileName
, ?config(include_path, Config)
, erlang_ls_server:definition(Thing))),
ok.

%%==============================================================================
%% Internal Functions
%%==============================================================================
definition(DataDir, {record_expr, "record_a"}) ->
FilePath = filename:join([DataDir, <<"src">>, <<"code_navigation.erl">>]),
#{ range => erlang_ls_protocol:range(#{from => {4, 0}, to => {4, 0}})
, uri => erlang_ls_uri:uri(FilePath)
};
definition(DataDir, {macro, 'MACRO_A'}) ->
FilePath = filename:join([DataDir, <<"src">>, <<"code_navigation.erl">>]),
#{ range => erlang_ls_protocol:range(#{from => {6, 0}, to => {6, 0}})
, uri => erlang_ls_uri:uri(FilePath)
}.

%% TODO: Armonize strings vs atoms in definitions
14 changes: 14 additions & 0 deletions test/erlang_ls_code_navigation_SUITE_data/src/code_navigation.erl
@@ -0,0 +1,14 @@
-module(code_navigation).

-export([ function_a/0 ]).

-record(record_a, {field_a, field_b}).

-define(MACRO_A, macro_a).

function_a() ->
function_b(),
#record_a{}.

function_b() ->
?MACRO_A.

0 comments on commit 191198d

Please sign in to comment.