Skip to content

Commit

Permalink
Add tests for to_string and newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Apr 7, 2024
1 parent 9686856 commit c2871b1
Showing 1 changed file with 66 additions and 13 deletions.
79 changes: 66 additions & 13 deletions test/logger_formatter_json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,91 @@
-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").

all() -> [unstructured, structured, metadata, duplicate_keys].
all() -> [to_string, is_printable, unstructured, structured, metadata, duplicate_keys].

to_string(_) ->
Config = #{single_line => true},
?assertEqual("foo", logger_formatter_json:to_string("foo", Config)),
?assertEqual("foo", logger_formatter_json:to_string(foo, Config)),
?assertEqual(["[]"], logger_formatter_json:to_string([], Config)),
?assertEqual(<<>>, logger_formatter_json:to_string(<<>>, Config)),
?assertEqual(<<"foo">>, logger_formatter_json:to_string(<<"foo">>, Config)),
?assertEqual(
<<"foo\nbar">>,
iolist_to_binary(logger_formatter_json:to_string(<<"foo\nbar">>, Config))
),
?assertEqual(
<<"793µs"/utf8>>,
iolist_to_binary(logger_formatter_json:to_string(<<"793µs"/utf8>>, Config))
).


is_printable(_) ->
?assertEqual(true, logger_formatter_json:is_printable(<<"foo">>)),
% ?assertEqual(nomatch, re:run(<<"foo\nbar">>, <<"[[:^print:]]">>, [{capture, none}, unicode])),
?assertEqual(true, logger_formatter_json:is_printable(<<"foo\nbar">>)),
?assertEqual(true, logger_formatter_json:is_printable(<<"foo\nbar"/utf8>>)),
?assertEqual(false, logger_formatter_json:is_printable(<<0>>)).


unstructured() -> [{docs, "logs that aren't structured get passed through with a re-frame"}].

unstructured(_) ->
Config = #{single_line => true},
?assertEqual(
<<"{\"msg\":\"abc\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(#{level => info, msg => {string, "abc"}, meta => #{}}, #{})
logger_formatter_json:format(#{level => info, msg => {string, "abc"}, meta => #{}}, Config)
)
),
?assertEqual(
<<"{\"msg\":\"abc\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(#{level => info, msg => {string, [<<"abc">>]}, meta => #{}}, #{})
logger_formatter_json:format(
#{level => info, msg => {string, [<<"abc">>]}, meta => #{}},
Config
)
)
),
?assertEqual(
<<"{\"msg\":\"793\\u00B5s\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(
#{level => info, msg => {string, [<<"793µs"/utf8>>]}, meta => #{}},
#{}
Config
)
)
),
?assertEqual(
<<"{\"msg\":\"\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(#{level => info, msg => {string, <<>>}, meta => #{}}, Config)
)
),
?assertEqual(
<<"{\"msg\":\"foo\\nbar\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(
#{level => info, msg => {string, <<"foo\nbar">>}, meta => #{}},
Config
)
)
),
?assertEqual(
<<"{\"msg\":\"foo\\n\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(
#{level => info, msg => {string, <<"foo\n">>}, meta => #{}},
Config
)
)
),
?assertEqual(
<<"{\"msg\":\"793\\u00B5s\\n\",\"level\":\"info\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(
#{level => info, msg => {string, <<"793µs\n"/utf8>>}, meta => #{}},
Config
)
)
),
Expand Down Expand Up @@ -94,15 +156,6 @@ unstructured(_) ->
)
)
),
?assertEqual(
<<"{\"msg\":\"hello world\",\"level\":\"info\",\"foo\":\"<<\\\"control char\\\\n\\\">>\"}\n">>,
iolist_to_binary(
logger_formatter_json:format(
#{level => info, msg => {"hello ~s", ["world"]}, meta => #{foo => <<"control char\n">>}},
#{template => [msg, level, foo]}
)
)
),
% Binary data
?assertEqual(
<<"{\"msg\":\"hello world\",\"level\":\"info\",\"foo\":\"<<0,1,2,3>>\"}\n">>,
Expand Down

0 comments on commit c2871b1

Please sign in to comment.