%% @author Anatolii Kosorukov [rustkas.github.io/] %% @copyright 2021 by Anatolii Kosorukov %% @doc Helper functions for eunit testing. -module(eunit_helper). -export([check_all_by_regex/3,check_all_by_regex/4]). -include_lib("eunit/include/eunit.hrl"). %% @doc Checks whether string's elements match regular expression. %%
%% See also: %% [http://erlang.org/doc/man/re.html#run-2 re:run/2]. %% %% @param MP regular expression %% @param CharacterList input string %% @param CheckFirst logic switch. It determines whether the desired solution is a match or a nomatch when the regex is executed %% @returns Returns true if all elements of CharacterList meet the criteria for regular expression and logical switch CheckFirst. -spec check_all_by_regex(MP, CharacterList, CheckFirst) -> Result when MP :: {re_pattern, term(), term(), term(), term()}, CharacterList :: string(), CheckFirst :: boolean(), Result :: boolean(). check_all_by_regex(MP, CharacterList, CheckFirst) -> Result = lists:all(fun(Elem) -> case re:run([Elem], MP) of {match, _} -> CheckFirst; nomatch -> not CheckFirst end end, CharacterList), Result. %% @doc Checks whether string's elements match regular expression. %%
%% See also: %% [http://erlang.org/doc/man/re.html#run-2 re:run/2]. %% %% @param MP regular expression %% @param CharacterList input string %% @param CheckFirst logic switch. It determines whether the desired solution is a match or a nomatch when the regex is executed %% @param DebugInfo information for additional logging printing %% @returns Returns true if all elements of CharacterList meet the criteria for regular expression and logical switch CheckFirst. -spec check_all_by_regex(MP, CharacterList, CheckFirst, DebugInfo) -> Result when MP :: {re_pattern, term(), term(), term(), term()}, CharacterList :: string(), CheckFirst :: boolean(), DebugInfo :: nomatch | match, Result :: boolean(). check_all_by_regex(MP, CharacterList, CheckFirst, nomatch) -> Result = lists:all(fun(Elem) -> case re:run([Elem], MP) of {match, _} -> CheckFirst; nomatch -> ?debugFmt("~p~n", [Elem]), not CheckFirst end end, CharacterList), Result; check_all_by_regex(MP, CharacterList, CheckFirst, match) -> Result = lists:all(fun(Elem) -> case re:run([Elem], MP) of {match, _} -> ?debugFmt("~p~n", [Elem]), CheckFirst; nomatch -> not CheckFirst end end, CharacterList), Result.