Merge pull request #184 from ariel-anieli/test-cmd-log-cnv
Moved `ec_cmd_log` & `ec_cnv` tests into separate files
This commit is contained in:
commit
6d4e7d14ce
5 changed files with 76 additions and 82 deletions
|
@ -43,14 +43,8 @@
|
|||
colorize/4,
|
||||
format/1]).
|
||||
|
||||
-include("ec_cmd_log.hrl").
|
||||
|
||||
-define(RED, $r).
|
||||
-define(GREEN, $g).
|
||||
-define(YELLOW, $y).
|
||||
-define(BLUE, $b).
|
||||
-define(MAGENTA, $m).
|
||||
-define(CYAN, $c).
|
||||
-include("include/ec_cmd_log.hrl").
|
||||
-include("src/ec_cmd_log.hrl").
|
||||
|
||||
-define(PREFIX, "===> ").
|
||||
|
||||
|
@ -261,44 +255,3 @@ colorize(#state_t{caller=command_line, intensity = low},
|
|||
lists:flatten(cf:format("~!" ++ [Color] ++"~ts~!!~ts", [?PREFIX, Msg]));
|
||||
colorize(_LogState, _Color, _Bold, Msg) ->
|
||||
Msg.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Test Functions
|
||||
%%%===================================================================
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
should_test() ->
|
||||
ErrorLogState = new(error),
|
||||
?assertMatch(true, should(ErrorLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, not should(ErrorLogState, ?EC_INFO)),
|
||||
?assertMatch(true, not should(ErrorLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_ERROR, log_level(ErrorLogState)),
|
||||
?assertEqual(error, atom_log_level(ErrorLogState)),
|
||||
|
||||
InfoLogState = new(info),
|
||||
?assertMatch(true, should(InfoLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, should(InfoLogState, ?EC_INFO)),
|
||||
?assertMatch(true, not should(InfoLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_INFO, log_level(InfoLogState)),
|
||||
?assertEqual(info, atom_log_level(InfoLogState)),
|
||||
|
||||
DebugLogState = new(debug),
|
||||
?assertMatch(true, should(DebugLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, should(DebugLogState, ?EC_INFO)),
|
||||
?assertMatch(true, should(DebugLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_DEBUG, log_level(DebugLogState)),
|
||||
?assertEqual(debug, atom_log_level(DebugLogState)).
|
||||
|
||||
|
||||
no_color_test() ->
|
||||
LogState = new(debug, command_line, none),
|
||||
?assertEqual("test",
|
||||
colorize(LogState, ?RED, true, "test")).
|
||||
|
||||
color_test() ->
|
||||
LogState = new(debug, command_line, high),
|
||||
?assertEqual("\e[1;31m===> test\e[0m",
|
||||
colorize(LogState, ?RED, true, "test")).
|
||||
-endif.
|
||||
|
|
7
src/ec_cmd_log.hrl
Normal file
7
src/ec_cmd_log.hrl
Normal file
|
@ -0,0 +1,7 @@
|
|||
%%% @copyright 2024 Erlware, LLC.
|
||||
-define(RED, $r).
|
||||
-define(GREEN, $g).
|
||||
-define(YELLOW, $y).
|
||||
-define(BLUE, $b).
|
||||
-define(MAGENTA, $m).
|
||||
-define(CYAN, $c).
|
|
@ -212,36 +212,3 @@ to_atom(X)
|
|||
erlang:list_to_existing_atom(X);
|
||||
to_atom(X) ->
|
||||
to_atom(to_list(X)).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Tests
|
||||
%%%===================================================================
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
to_integer_test() ->
|
||||
?assertError(badarg, to_integer(1.5, strict)).
|
||||
|
||||
to_float_test() ->
|
||||
?assertError(badarg, to_float(10, strict)).
|
||||
|
||||
to_atom_test() ->
|
||||
?assertMatch(true, to_atom("true")),
|
||||
?assertMatch(true, to_atom(<<"true">>)),
|
||||
?assertMatch(false, to_atom(<<"false">>)),
|
||||
?assertMatch(false, to_atom(false)),
|
||||
?assertError(badarg, to_atom("hello_foo_bar_baz")),
|
||||
|
||||
S = erlang:list_to_atom("1"),
|
||||
?assertMatch(S, to_atom(1)).
|
||||
|
||||
to_boolean_test()->
|
||||
?assertMatch(true, to_boolean(<<"true">>)),
|
||||
?assertMatch(true, to_boolean("true")),
|
||||
?assertMatch(true, to_boolean(true)),
|
||||
?assertMatch(false, to_boolean(<<"false">>)),
|
||||
?assertMatch(false, to_boolean("false")),
|
||||
?assertMatch(false, to_boolean(false)).
|
||||
|
||||
-endif.
|
||||
|
|
39
test/ec_cmd_log_tests.erl
Normal file
39
test/ec_cmd_log_tests.erl
Normal file
|
@ -0,0 +1,39 @@
|
|||
%%% @copyright 2024 Erlware, LLC.
|
||||
-module(ec_cmd_log_tests).
|
||||
|
||||
-include("include/ec_cmd_log.hrl").
|
||||
-include("src/ec_cmd_log.hrl").
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
should_test() ->
|
||||
ErrorLogState = ec_cmd_log:new(error),
|
||||
?assertMatch(true, ec_cmd_log:should(ErrorLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, not ec_cmd_log:should(ErrorLogState, ?EC_INFO)),
|
||||
?assertMatch(true, not ec_cmd_log:should(ErrorLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_ERROR, ec_cmd_log:log_level(ErrorLogState)),
|
||||
?assertEqual(error, ec_cmd_log:atom_log_level(ErrorLogState)),
|
||||
|
||||
InfoLogState = ec_cmd_log:new(info),
|
||||
?assertMatch(true, ec_cmd_log:should(InfoLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, ec_cmd_log:should(InfoLogState, ?EC_INFO)),
|
||||
?assertMatch(true, not ec_cmd_log:should(InfoLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_INFO, ec_cmd_log:log_level(InfoLogState)),
|
||||
?assertEqual(info, ec_cmd_log:atom_log_level(InfoLogState)),
|
||||
|
||||
DebugLogState = ec_cmd_log:new(debug),
|
||||
?assertMatch(true, ec_cmd_log:should(DebugLogState, ?EC_ERROR)),
|
||||
?assertMatch(true, ec_cmd_log:should(DebugLogState, ?EC_INFO)),
|
||||
?assertMatch(true, ec_cmd_log:should(DebugLogState, ?EC_DEBUG)),
|
||||
?assertEqual(?EC_DEBUG, ec_cmd_log:log_level(DebugLogState)),
|
||||
?assertEqual(debug, ec_cmd_log:atom_log_level(DebugLogState)).
|
||||
|
||||
|
||||
no_color_test() ->
|
||||
LogState = ec_cmd_log:new(debug, command_line, none),
|
||||
?assertEqual("test",
|
||||
ec_cmd_log:colorize(LogState, ?RED, true, "test")).
|
||||
|
||||
color_test() ->
|
||||
LogState = ec_cmd_log:new(debug, command_line, high),
|
||||
?assertEqual("\e[1;31m===> test\e[0m",
|
||||
ec_cmd_log:colorize(LogState, ?RED, true, "test")).
|
28
test/ec_cnv_tests.erl
Normal file
28
test/ec_cnv_tests.erl
Normal file
|
@ -0,0 +1,28 @@
|
|||
%%% @copyright 2024 Erlware, LLC.
|
||||
-module(ec_cnv_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
to_integer_test() ->
|
||||
?assertError(badarg, ec_cnv:to_integer(1.5, strict)).
|
||||
|
||||
to_float_test() ->
|
||||
?assertError(badarg, ec_cnv:to_float(10, strict)).
|
||||
|
||||
to_atom_test() ->
|
||||
?assertMatch(true, ec_cnv:to_atom("true")),
|
||||
?assertMatch(true, ec_cnv:to_atom(<<"true">>)),
|
||||
?assertMatch(false, ec_cnv:to_atom(<<"false">>)),
|
||||
?assertMatch(false, ec_cnv:to_atom(false)),
|
||||
?assertError(badarg, ec_cnv:to_atom("hello_foo_bar_baz")),
|
||||
|
||||
S = erlang:list_to_atom("1"),
|
||||
?assertMatch(S, ec_cnv:to_atom(1)).
|
||||
|
||||
to_boolean_test()->
|
||||
?assertMatch(true, ec_cnv:to_boolean(<<"true">>)),
|
||||
?assertMatch(true, ec_cnv:to_boolean("true")),
|
||||
?assertMatch(true, ec_cnv:to_boolean(true)),
|
||||
?assertMatch(false, ec_cnv:to_boolean(<<"false">>)),
|
||||
?assertMatch(false, ec_cnv:to_boolean("false")),
|
||||
?assertMatch(false, ec_cnv:to_boolean(false)).
|
Loading…
Add table
Add a link
Reference in a new issue