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]).
-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(binary())),
list(binary())}.
-file("src/testament/internal/util.gleam", 12).
?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", 21).
?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", 28).
?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 => 29,
value => _assert_fail,
start => 695,
'end' => 804,
pattern_start => 706,
pattern_end => 716})
end,
<<"import "/utf8,
(gleam@list:fold(Module@1, <<""/utf8>>, fun filepath:join/2))/binary>>.
-file("src/testament/internal/util.gleam", 66).
?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 => 83,
value => _assert_fail,
start => 2156,
'end' => 2281,
pattern_start => 2167,
pattern_end => 2174})
end,
case simplifile:append(
Test_file_name,
<<Imports@1/binary, "\n"/utf8>>
) 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 => 89,
value => _assert_fail@1,
start => 2338,
'end' => 2409,
pattern_start => 2349,
pattern_end => 2356})
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", 39).
?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 => 43,
value => _assert_fail,
start => 1051,
'end' => 1102,
pattern_start => 1062,
pattern_end => 1078})
end,
{Imports, Tests} = testament@internal@parse:get_doc_tests_imports_and_code(
File_content@1
),
Imports@1 = begin
_pipe = Imports,
_pipe@1 = lists:append(_pipe, Extra_imports),
gleam@list:prepend(_pipe@1, import_from_file_name(File))
end,
do_create_tests(File, Imports@1, Tests).
-file("src/testament/internal/util.gleam", 55).
?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 => 59,
value => _assert_fail,
start => 1521,
'end' => 1572,
pattern_start => 1532,
pattern_end => 1548})
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", 114).
?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,
begin
_pipe = Imports,
_pipe@1 = gleam@list:map(
_pipe,
fun gleam@string:trim/1
),
gleam@list:map(
_pipe@1,
fun(_capture) ->
gleam@string:append(
<<"import "/utf8>>,
_capture
)
end
)
end
),
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", 150).
?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.