Current section

Files

Jump to
testament src testament@internal@util.erl
Raw

src/testament@internal@util.erl

-module(testament@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/testament/internal/util.gleam").
-export([get_test_file_name/1, is_doc/1, is_doctest_line/1, split_imports_and_code/2, clean_doc_tests/0, import_from_file_name/1, get_doc_tests_imports_and_code/2, create_tests_for_file/2]).
-export_type([doc_state/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-type doc_state() :: {doc_state, boolean(), list(glexer@token:token())}.
-file("src/testament/internal/util.gleam", 16).
?DOC(false).
-spec get_test_file_name(binary()) -> binary().
get_test_file_name(File) ->
_pipe = File,
_pipe@1 = gleam@string:replace(_pipe, <<"src"/utf8>>, <<""/utf8>>),
_pipe@2 = gleam@string:replace(
_pipe@1,
<<".gleam"/utf8>>,
<<"_doc_test.gleam"/utf8>>
),
_pipe@3 = filepath:join(<<"testament"/utf8>>, _pipe@2),
filepath:join(<<"test"/utf8>>, _pipe@3).
-file("src/testament/internal/util.gleam", 74).
?DOC(false).
-spec is_doc(glexer@token:token()) -> boolean().
is_doc(T) ->
case T of
{comment_doc, _} ->
true;
{comment_module, _} ->
true;
_ ->
false
end.
-file("src/testament/internal/util.gleam", 82).
?DOC(false).
-spec is_doctest_line(glexer@token:token()) -> boolean().
is_doctest_line(T) ->
case T of
{comment_doc, <<":"/utf8, _/binary>>} ->
true;
{comment_module, <<":"/utf8, _/binary>>} ->
true;
_ ->
false
end.
-file("src/testament/internal/util.gleam", 90).
?DOC(false).
-spec split_imports_and_code({list(binary()), list(binary())}, binary()) -> {list(binary()),
list(binary())}.
split_imports_and_code(Code, Line) ->
case gleam_stdlib:string_starts_with(Line, <<"import"/utf8>>) of
true ->
gleam@pair:map_first(
Code,
fun(_capture) -> lists:append(_capture, [Line]) end
);
false ->
gleam@pair:map_second(
Code,
fun(_capture@1) -> lists:append(_capture@1, [Line]) end
)
end.
-file("src/testament/internal/util.gleam", 100).
?DOC(false).
-spec clean_doc_tests() -> {ok, nil} | {error, simplifile:file_error()}.
clean_doc_tests() ->
gleam@result:'try'(
simplifile:get_files(<<"src"/utf8>>),
fun(Files) -> _pipe = Files,
_pipe@1 = gleam@list:map(_pipe, fun get_test_file_name/1),
simplifile:delete_all(_pipe@1) end
).
-file("src/testament/internal/util.gleam", 107).
?DOC(false).
-spec import_from_file_name(binary()) -> binary().
import_from_file_name(File) ->
Module@1 = case begin
_pipe = File,
_pipe@1 = filepath:strip_extension(_pipe),
_pipe@2 = filepath:split(_pipe@1),
gleam@list:rest(_pipe@2)
end of
{ok, Module} -> Module;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"import_from_file_name"/utf8>>,
line => 108,
value => _assert_fail,
start => 2700,
'end' => 2807,
pattern_start => 2711,
pattern_end => 2721})
end,
<<"import "/utf8,
(gleam@list:fold(Module@1, <<""/utf8>>, fun filepath:join/2))/binary>>.
-file("src/testament/internal/util.gleam", 57).
?DOC(false).
-spec fold_doc_state(doc_state(), glexer@token:token()) -> doc_state().
fold_doc_state(State, Line) ->
case {State, Line} of
{{doc_state, false, Lines}, {comment_doc, <<":"/utf8, _/binary>>}} ->
{doc_state,
true,
lists:append(
Lines,
[{comment_doc, <<":"/utf8, "{"/utf8>>}, Line]
)};
{{doc_state, false, Lines}, {comment_module, <<":"/utf8, _/binary>>}} ->
{doc_state,
true,
lists:append(
Lines,
[{comment_doc, <<":"/utf8, "{"/utf8>>}, Line]
)};
{{doc_state, true, Lines@1}, {comment_doc, <<":"/utf8, _/binary>>}} ->
_record = State,
{doc_state,
erlang:element(2, _record),
lists:append(Lines@1, [Line])};
{{doc_state, true, Lines@1}, {comment_module, <<":"/utf8, _/binary>>}} ->
_record = State,
{doc_state,
erlang:element(2, _record),
lists:append(Lines@1, [Line])};
{{doc_state, true, Lines@2}, _} ->
{doc_state,
false,
lists:append(
Lines@2,
[Line, {comment_doc, <<":"/utf8, "}"/utf8>>}]
)};
{_, _} ->
_record@1 = State,
{doc_state,
erlang:element(2, _record@1),
lists:append(erlang:element(3, State), [Line])}
end.
-file("src/testament/internal/util.gleam", 24).
?DOC(false).
-spec get_doc_tests_imports_and_code(binary(), list(binary())) -> {binary(),
binary()}.
get_doc_tests_imports_and_code(Code, Other_imports) ->
Cases = begin
_pipe = Code,
_pipe@1 = glexer:new(_pipe),
_pipe@2 = glexer:lex(_pipe@1),
_pipe@3 = gleam@list:map(_pipe@2, fun gleam@pair:first/1),
_pipe@4 = gleam@list:filter(_pipe@3, fun is_doc/1),
gleam@list:fold(_pipe@4, {doc_state, false, []}, fun fold_doc_state/2)
end,
Lines = case erlang:element(2, Cases) of
true ->
lists:append(
erlang:element(3, Cases),
[{comment_doc, <<":"/utf8, "}"/utf8>>}]
);
_ ->
erlang:element(3, Cases)
end,
_pipe@5 = Lines,
_pipe@6 = gleam@list:filter(_pipe@5, fun is_doctest_line/1),
_pipe@7 = gleam@list:map(_pipe@6, fun glexer@token:to_source/1),
_pipe@8 = gleam@list:map(
_pipe@7,
fun(_capture) -> gleam_stdlib:crop_string(_capture, <<":"/utf8>>) end
),
_pipe@9 = gleam@list:map(
_pipe@8,
fun(_capture@1) -> gleam@string:drop_start(_capture@1, 1) end
),
_pipe@10 = gleam@list:map(_pipe@9, fun gleam@string:trim/1),
_pipe@11 = gleam@list:fold(
_pipe@10,
{Other_imports, []},
fun split_imports_and_code/2
),
_pipe@12 = gleam@pair:map_first(_pipe@11, fun gleam@list:unique/1),
_pipe@13 = gleam@pair:map_first(
_pipe@12,
fun(_capture@2) -> gleam@string:join(_capture@2, <<"\n"/utf8>>) end
),
gleam@pair:map_second(
_pipe@13,
fun(_capture@3) -> gleam@string:join(_capture@3, <<"\n"/utf8>>) end
).
-file("src/testament/internal/util.gleam", 117).
?DOC(false).
-spec create_tests_for_file(binary(), list(binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
create_tests_for_file(File, Extra_imports) ->
File_content@1 = case simplifile:read(File) of
{ok, File_content} -> File_content;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"create_tests_for_file"/utf8>>,
line => 118,
value => _assert_fail,
start => 2940,
'end' => 2991,
pattern_start => 2951,
pattern_end => 2967})
end,
{Imports, Code} = get_doc_tests_imports_and_code(
File_content@1,
Extra_imports
),
case gleam@string:is_empty(Code) of
true ->
{ok, nil};
_ ->
Test_file_name = get_test_file_name(File),
_ = begin
_pipe = Test_file_name,
_pipe@1 = filepath:directory_name(_pipe),
simplifile:create_directory_all(_pipe@1)
end,
Test_content = gleam@string:join(
[Imports, <<"pub fn doc_test() {"/utf8>>, Code, <<"}"/utf8>>],
<<"\n"/utf8>>
),
_ = simplifile_erl:delete(Test_file_name),
_assert_subject = simplifile:append(Test_file_name, Test_content),
case _assert_subject of
{ok, nil} -> _assert_subject;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"create_tests_for_file"/utf8>>,
line => 138,
value => _assert_fail@1,
start => 3470,
'end' => 3538,
pattern_start => 3481,
pattern_end => 3488})
end
end.