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

Add [some] more tests #27

Merged
merged 11 commits into from
Jan 27, 2022
14 changes: 4 additions & 10 deletions src/rebar3_mini_typer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
plt => file:filename(),
trusted => [file:filename()],
files_r => [file:filename_all()],
macros => [{atom(), term()}],
io => io()}.

-export_type([opts/0]).
Expand Down Expand Up @@ -649,9 +650,8 @@ analyze_result({io, Val}, Args, Analysis) ->
{Args, Analysis#analysis{io = Val}};
analyze_result({mode, Mode}, Args, Analysis) ->
{Args, Analysis#analysis{mode = Mode}};
analyze_result({def, Val}, Args, Analysis) ->
NewVal = Analysis#analysis.macros ++ [Val],
{Args, Analysis#analysis{macros = NewVal}};
analyze_result({macros, Macros}, Args, Analysis) ->
{Args, Analysis#analysis{macros = Macros}};
analyze_result({inc, Val}, Args, Analysis) ->
NewVal = Analysis#analysis.includes ++ [Val],
{Args, Analysis#analysis{includes = NewVal}};
Expand All @@ -660,13 +660,7 @@ analyze_result({plt, Plt}, Args, Analysis) ->
analyze_result({show_succ, Value}, Args, Analysis) ->
{Args, Analysis#analysis{show_succ = Value}};
analyze_result({no_spec, Value}, Args, Analysis) ->
{Args, Analysis#analysis{no_spec = Value}};
analyze_result({pa, Dir}, Args, Analysis) ->
true = code:add_patha(Dir),
{Args, Analysis};
analyze_result({pz, Dir}, Args, Analysis) ->
true = code:add_pathz(Dir),
{Args, Analysis}.
{Args, Analysis#analysis{no_spec = Value}}.

%%--------------------------------------------------------------------
%% File processing.
Expand Down
6 changes: 6 additions & 0 deletions test/files/ann_erl/ignore.ann.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-module ignore.

-export([ignored/0]).

ignored() ->
ignored.
4 changes: 4 additions & 0 deletions test/files/def/def.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-module(def).

def() ->
?DEF.
7 changes: 7 additions & 0 deletions test/files/no_spec/specs.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(specs).

-export([specced/0]).

-spec specced() -> x:y().
specced() ->
true.
7 changes: 7 additions & 0 deletions test/files/show_succ/succ.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(succ).

-export([spec/0]).

-spec spec() -> boolean().
spec() ->
false.
3 changes: 2 additions & 1 deletion test/files/single_file/single.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
exported() ->
not_exported().

%% @doc Using some external types here, for completeness
not_exported() ->
ok.
lists:foreach(fun(_) -> ignore end, lists:seq(1, 10)).
1 change: 1 addition & 0 deletions test/files/trusted/empty.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module(empty).
8 changes: 8 additions & 0 deletions test/files/trusted/trusted.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(trusted).

-export([trusted/0]).

%% It's a lie!
-spec trusted() -> trusted.
trusted() ->
trusted.
6 changes: 6 additions & 0 deletions test/files/trusted/untrusted.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-module(untrusted).

-export([untrusted/0]).

untrusted() ->
trusted:trusted().
117 changes: 115 additions & 2 deletions test/rebar3_mini_typer_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
-behaviour(ct_suite).

-export([all/0]).
-export([empty/1, bad_plt/1, single_file/1, annotate/1, annotate_in_place/1]).
-export([empty/1, bad_plt/1, single_file/1, annotate/1, annotate_in_place/1, trusted/1,
show_succ/1, files/1, def/1, no_spec/1, ann_erl/1]).

all() ->
[empty, bad_plt, single_file, annotate, annotate_in_place].
[empty,
bad_plt,
single_file,
annotate,
annotate_in_place,
trusted,
show_succ,
files,
def,
no_spec,
ann_erl].

empty(_) ->
ct:comment("With no files... we get an error"),
Expand Down Expand Up @@ -42,6 +53,13 @@ single_file(_) ->
{info, <<"%% ----", _/binary>>},
{info, <<"-spec exported() -> 'ok'.">>}] =
run_typer(#{files_r => [abs_test_path("single_file")], mode => show_exported}),

ct:comment("With edoc... we get its types as edoc"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"%% @spec exported() -> 'ok'.">>},
{info, <<"%% @spec not_exported() -> 'ok'.">>}] =
run_typer(#{files_r => [abs_test_path("single_file")], edoc => true}),
{comment, ""}.

%% @todo Test annotate_inc_files when https://github.com/erlang/otp/issues/5653 is fixed.
Expand All @@ -66,6 +84,101 @@ annotate_in_place(_) ->
false = FileInfo0 == FileInfo1,
{comment, ""}.

%% @todo Improve the test when https://github.com/erlang/otp/issues/5657 is fixed.
trusted(_) ->
ct:comment("With an invalid path.. we get an error"),
[{abort, <<"typer: cannot access ", _/binary>>}] =
run_typer(#{files_r => [abs_test_path("trusted")],
trusted => [abs_test_path("trusted/non-existent.erl")]}),

ct:comment("No specs in the trusted file"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"\n%% File", _/binary>>},
{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec trusted() -> 'trusted'.">>},
{info, <<"-spec untrusted() -> 'trusted'.">>}] =
lists:sort(run_typer(#{files_r => [abs_test_path("trusted")],
trusted => [abs_test_path("trusted/empty.erl")]})),
{comment, ""}.

files(_) ->
ct:comment("3 files on files_r"),
[_, _, _] =
[F
|| {info, <<"\n%% File", F/binary>>}
<- run_typer(#{files_r => [abs_test_path("trusted")]})],

ct:comment("2 files on files"),
[_, _] =
[F
|| {info, <<"\n%% File", F/binary>>}
<- run_typer(#{files =>
[abs_test_path("trusted/empty.erl"),
abs_test_path("trusted/trusted.erl")]})],

ct:comment("3 files when combined"),
[_, _, _] =
[F
|| {info, <<"\n%% File", F/binary>>}
<- run_typer(#{files_r => [abs_test_path("trusted")],
files =>
[abs_test_path("trusted/empty.erl"),
abs_test_path("trusted/trusted.erl")]})],
{comment, ""}.

show_succ(_) ->
ct:comment("Show original contract if false"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec spec() -> boolean().">>}] =
run_typer(#{files_r => [abs_test_path("show_succ")], show_succ => false}),

ct:comment("Show success typing if true"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec spec() -> 'false'.">>}] =
run_typer(#{files_r => [abs_test_path("show_succ")], show_succ => true}),
{comment, ""}.

def(_) ->
ct:comment("Without a macro definition.. we get an error"),
[{abort, <<"typer: Analysis failed with error report:", _/binary>>}] =
run_typer(#{files_r => [abs_test_path("def")]}),

ct:comment("With a single module... we get its types"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec def() -> 'd1'.">>}] =
run_typer(#{files_r => [abs_test_path("def")], macros => [{'DEF', d1}]}),
{comment, ""}.

no_spec(_) ->
ct:comment("Show original spec if false"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec specced() -> x:y().">>}] =
run_typer(#{files_r => [abs_test_path("no_spec")], no_spec => false}),

ct:comment("Disregards existing spec if true"),
[{info, <<"\n%% File", _/binary>>},
{info, <<"%% ----", _/binary>>},
{info, <<"-spec specced() -> 'true'.">>}] =
run_typer(#{files_r => [abs_test_path("no_spec")], no_spec => true}),
{comment, ""}.

ann_erl(_) ->
ct:comment(".ann.erl files are ignored"),
[{abort, <<"typer: no file(s) to analyze">>}] =
run_typer(#{files_r => [abs_test_path("ann_erl")]}),

ct:comment(".ann.erl files are ignored even when explicitely required"),
[{abort, <<"typer: no file(s) to analyze">>}] =
run_typer(#{files => [abs_test_path("ann_erl/ignore.ann.erl")]}),
{comment, ""}.

%%% PRIVATE FUNCTIONS

test_annotate_mode(Mode) ->
Expand Down