-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, is_doc/1, is_doctest_line/1, split_imports_and_code/2, clean_doc_tests/0, import_from_file_name/1, fold_doc_state/2, 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", 77). ?DOC(false). -spec is_doc(glexer@token:token()) -> boolean(). is_doc(Line) -> case Line of {comment_doc, _} -> true; {comment_module, _} -> true; _ -> false end. -file("src/testament/internal/util.gleam", 85). ?DOC(false). -spec is_doctest_line(glexer@token:token()) -> boolean(). is_doctest_line(Line) -> case Line of {comment_doc, <<":"/utf8, _/binary>>} -> true; {comment_module, <<":"/utf8, _/binary>>} -> true; _ -> false end. -file("src/testament/internal/util.gleam", 93). ?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", 103). ?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", 110). ?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 => <>, module => <<"testament/internal/util"/utf8>>, function => <<"import_from_file_name"/utf8>>, line => 111, value => _assert_fail, start => 2741, 'end' => 2850, pattern_start => 2752, pattern_end => 2762}) end, <<"import "/utf8, (gleam@list:fold(Module@1, <<""/utf8>>, fun filepath:join/2))/binary>>. -file("src/testament/internal/util.gleam", 61). ?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, [Line, {comment_doc, <<":"/utf8, "{"/utf8>>} | Lines]}; {{doc_state, false, Lines}, {comment_module, <<":"/utf8, _/binary>>}} -> {doc_state, true, [Line, {comment_doc, <<":"/utf8, "{"/utf8>>} | Lines]}; {{doc_state, true, Lines@1}, {comment_doc, <<":"/utf8, _/binary>>}} -> {doc_state, erlang:element(2, State), [Line | Lines@1]}; {{doc_state, true, Lines@1}, {comment_module, <<":"/utf8, _/binary>>}} -> {doc_state, erlang:element(2, State), [Line | Lines@1]}; {{doc_state, true, Lines@2}, _} -> {doc_state, false, [{comment_doc, <<":"/utf8, "}"/utf8>>} | Lines@2]}; {_, _} -> State end. -file("src/testament/internal/util.gleam", 28). ?DOC(false). -spec get_doc_tests_imports_and_code(binary(), list(binary())) -> {binary(), binary()}. get_doc_tests_imports_and_code(Code, Other_imports) -> Prefix_len = string:length(<<":"/utf8>>), State = begin _pipe = Code, _pipe@1 = glexer:new(_pipe), _pipe@2 = glexer:discard_whitespace(_pipe@1), _pipe@3 = glexer:lex(_pipe@2), _pipe@4 = gleam@list:map(_pipe@3, fun gleam@pair:first/1), _pipe@5 = gleam@list:filter(_pipe@4, fun is_doc/1), gleam@list:fold(_pipe@5, {doc_state, false, []}, fun fold_doc_state/2) end, Lines = case erlang:element(2, State) of true -> [{comment_doc, <<":"/utf8, "}"/utf8>>} | erlang:element(3, State)]; _ -> erlang:element(3, State) end, _pipe@6 = Lines, _pipe@7 = lists:reverse(_pipe@6), _pipe@8 = gleam@list:filter(_pipe@7, fun is_doctest_line/1), _pipe@9 = gleam@list:map(_pipe@8, fun glexer@token:to_source/1), _pipe@10 = gleam@list:map( _pipe@9, fun(_capture) -> gleam_stdlib:crop_string(_capture, <<":"/utf8>>) end ), _pipe@11 = gleam@list:map( _pipe@10, fun(_capture@1) -> gleam@string:drop_start(_capture@1, Prefix_len) end ), _pipe@12 = gleam@list:map(_pipe@11, fun gleam@string:trim/1), _pipe@13 = gleam@list:fold( _pipe@12, {Other_imports, []}, fun split_imports_and_code/2 ), _pipe@14 = gleam@pair:map_first(_pipe@13, fun gleam@list:unique/1), _pipe@15 = gleam@pair:map_first( _pipe@14, fun(_capture@2) -> gleam@string:join(_capture@2, <<"\n"/utf8>>) end ), gleam@pair:map_second( _pipe@15, fun(_capture@3) -> gleam@string:join(_capture@3, <<"\n"/utf8>>) end ). -file("src/testament/internal/util.gleam", 121). ?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 => <>, module => <<"testament/internal/util"/utf8>>, function => <<"create_tests_for_file"/utf8>>, line => 125, value => _assert_fail, start => 3091, 'end' => 3142, pattern_start => 3102, pattern_end => 3118}) 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 => (<<<<"could not create doc tests for file '"/utf8, File/binary>>/binary, "'"/utf8>>), file => <>, module => <<"testament/internal/util"/utf8>>, function => <<"create_tests_for_file"/utf8>>, line => 146, value => _assert_fail@1, start => 3671, 'end' => 3739, pattern_start => 3682, pattern_end => 3689}) end end.