Current section
Files
Jump to
Current section
Files
src/testament@internal@util.erl
-module(testament@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/testament/internal/util.gleam").
-export([get_test_file_name/1, clean_doc_tests/0, import_from_file_name/1, create_tests_for_file/2, create_tests_for_markdown_file/2, combine_conf_values/1, verbose_log/2, combine_unqualified/1]).
-export_type([config/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 config() :: {config,
list(binary()),
boolean(),
boolean(),
gleam@dict:dict(binary(), list(testament@conf:import())),
list(binary())}.
-file("src/testament/internal/util.gleam", 15).
?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,
<<".md"/utf8>>,
<<"_md.gleam"/utf8>>
),
_pipe@3 = gleam@string:replace(
_pipe@2,
<<".gleam"/utf8>>,
<<"_doc_test.gleam"/utf8>>
),
_pipe@4 = filepath:join(<<"testament"/utf8>>, _pipe@3),
filepath:join(<<"test"/utf8>>, _pipe@4).
-file("src/testament/internal/util.gleam", 24).
?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 = testament@internal@stream:new(_pipe),
_pipe@2 = testament@internal@stream:map(
_pipe@1,
fun get_test_file_name/1
),
_pipe@3 = testament@internal@stream:to_list(_pipe@2),
simplifile:delete_all(_pipe@3) end
).
-file("src/testament/internal/util.gleam", 33).
?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 => (<<<<"could not construct file name for '"/utf8,
File/binary>>/binary,
"'"/utf8>>),
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"import_from_file_name"/utf8>>,
line => 34,
value => _assert_fail,
start => 831,
'end' => 940,
pattern_start => 842,
pattern_end => 852})
end,
gleam@list:fold(Module@1, <<""/utf8>>, fun filepath:join/2).
-file("src/testament/internal/util.gleam", 68).
?DOC(false).
-spec do_create_tests(binary(), list(binary()), list(binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
do_create_tests(Filepath, Imports, Tests) ->
case Tests of
[] ->
{ok, nil};
_ ->
Imports@1 = begin
_pipe = Imports,
_pipe@1 = gleam@list:unique(_pipe),
gleam@string:join(_pipe@1, <<"\n"/utf8>>)
end,
Test_file_name = get_test_file_name(Filepath),
_ = simplifile_erl:delete(Test_file_name),
case begin
_pipe@2 = Test_file_name,
_pipe@3 = filepath:directory_name(_pipe@2),
simplifile:create_directory_all(_pipe@3)
end of
{ok, nil} -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"failed to create test doc directory"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"do_create_tests"/utf8>>,
line => 85,
value => _assert_fail,
start => 2233,
'end' => 2358,
pattern_start => 2244,
pattern_end => 2251})
end,
case simplifile:append(
Test_file_name,
<<Imports@1/binary, (<<"\n"/utf8>>)/binary>>
) of
{ok, nil} -> nil;
_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 => <<"do_create_tests"/utf8>>,
line => 91,
value => _assert_fail@1,
start => 2415,
'end' => 2507,
pattern_start => 2426,
pattern_end => 2433})
end,
_pipe@5 = gleam@list:index_map(
Tests,
fun(Code, Index) ->
_pipe@4 = gleam@string:join(
[<<<<"\npub fn doc"/utf8,
(erlang:integer_to_binary(Index))/binary>>/binary,
"_test() {"/utf8>>,
Code,
<<"}\n"/utf8>>],
<<"\n"/utf8>>
),
simplifile:append(Test_file_name, _pipe@4)
end
),
_pipe@6 = gleam@result:all(_pipe@5),
gleam@result:replace(_pipe@6, nil)
end.
-file("src/testament/internal/util.gleam", 44).
?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 => (<<<<"could not read file '"/utf8,
File/binary>>/binary,
"'"/utf8>>),
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"create_tests_for_file"/utf8>>,
line => 48,
value => _assert_fail,
start => 1174,
'end' => 1225,
pattern_start => 1185,
pattern_end => 1201})
end,
{Imports, Tests} = testament@internal@parse:get_doc_tests_imports_and_code(
File_content@1
),
Imports@1 = lists:append(Imports, Extra_imports),
do_create_tests(File, Imports@1, Tests).
-file("src/testament/internal/util.gleam", 57).
?DOC(false).
-spec create_tests_for_markdown_file(binary(), list(binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
create_tests_for_markdown_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 => (<<<<"could not read file '"/utf8,
File/binary>>/binary,
"'"/utf8>>),
file => <<?FILEPATH/utf8>>,
module => <<"testament/internal/util"/utf8>>,
function => <<"create_tests_for_markdown_file"/utf8>>,
line => 61,
value => _assert_fail,
start => 1585,
'end' => 1636,
pattern_start => 1596,
pattern_end => 1612})
end,
{Imports, Tests} = testament@internal@parse:parse_markdown_snippets(
File_content@1
),
do_create_tests(File, lists:append(Imports, Extra_imports), Tests).
-file("src/testament/internal/util.gleam", 117).
?DOC(false).
-spec combine_conf_values(list(testament@conf:conf())) -> config().
combine_conf_values(Opts) ->
gleam@list:fold(
Opts,
{config, [], false, false, maps:new(), []},
fun(Cfg, Opt) -> case Opt of
{extra_imports, File, Imports} ->
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
gleam@dict:insert(erlang:element(5, Cfg), File, Imports),
erlang:element(6, Cfg)};
{ignore_files, Files} ->
{config,
lists:append(erlang:element(2, Cfg), Files),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
erlang:element(5, Cfg),
erlang:element(6, Cfg)};
preserve_files ->
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
true,
erlang:element(5, Cfg),
erlang:element(6, Cfg)};
verbose ->
{config,
erlang:element(2, Cfg),
true,
erlang:element(4, Cfg),
erlang:element(5, Cfg),
erlang:element(6, Cfg)};
{markdown, Files@1} ->
{config,
erlang:element(2, Cfg),
erlang:element(3, Cfg),
erlang:element(4, Cfg),
erlang:element(5, Cfg),
lists:append(erlang:element(6, Cfg), Files@1)}
end end
).
-file("src/testament/internal/util.gleam", 148).
?DOC(false).
-spec verbose_log(boolean(), binary()) -> nil.
verbose_log(Log, Msg) ->
case Log of
true ->
gleam_stdlib:println(<<"testament: "/utf8, Msg/binary>>);
false ->
nil
end.
-file("src/testament/internal/util.gleam", 155).
?DOC(false).
-spec combine_unqualified(list(testament@conf:import())) -> list(binary()).
combine_unqualified(Imports) ->
_pipe = Imports,
_pipe@1 = gleam@list:group(_pipe, fun(I) -> erlang:element(2, I) end),
_pipe@2 = maps:to_list(_pipe@1),
_pipe@4 = gleam@list:fold(
_pipe@2,
[],
fun(Acc, I@1) ->
{Module, Imports@1} = I@1,
_pipe@3 = Acc,
gleam@list:prepend(
_pipe@3,
{import,
Module,
gleam@list:flat_map(
Imports@1,
fun(V) -> erlang:element(3, V) end
)}
)
end
),
gleam@list:map(
_pipe@4,
fun(I@2) ->
{import, Module@1, _} = I@2,
Tree = begin
_pipe@5 = gleam@string_tree:new(),
_pipe@6 = gleam@string_tree:append(_pipe@5, <<"import"/utf8>>),
_pipe@7 = gleam@string_tree:append(_pipe@6, <<" "/utf8>>),
gleam@string_tree:append(_pipe@7, Module@1)
end,
case I@2 of
{import, _, []} ->
unicode:characters_to_binary(Tree);
{import, _, Unqualified} ->
_pipe@8 = Tree,
_pipe@9 = gleam@string_tree:append(_pipe@8, <<".{"/utf8>>),
_pipe@10 = gleam@string_tree:append(
_pipe@9,
gleam@string:join(
gleam@list:unique(Unqualified),
<<", "/utf8>>
)
),
_pipe@11 = gleam@string_tree:append(_pipe@10, <<"}"/utf8>>),
unicode:characters_to_binary(_pipe@11)
end
end
).