25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-20 17:27:00 +01:00

Add JSON EUnit testing

This commit is contained in:
Badlop 2024-06-25 12:04:43 +02:00
parent 9b675fe775
commit 992d84c002

72
test/json_test.erl Normal file
View File

@ -0,0 +1,72 @@
-module(json_test).
-compile(export_all).
-include_lib("eunit/include/eunit.hrl").
%% @format-begin
encode_binary_test() ->
Binary = <<"This is an error text.">>,
Encoded = <<"\"This is an error text.\"">>,
?assertMatch(Encoded, misc:json_encode(Binary)).
-ifdef(OTP_BELOW_27).
-ifdef(OTP_BELOW_26).
%% OTP 25 or lower
encode_map_test() ->
Map = #{name => <<"room">>,
service => <<"conference">>,
jid => jid:encode({<<"user">>, <<"server">>, <<"">>}),
affiliation => member},
Encoded =
<<"{\"service\":\"conference\",\"name\":\"room\",\"jid\":\"user@server\",\"affiliation\":\"member\"}">>,
?assertMatch(Encoded, misc:json_encode(Map)).
-else.
%% OTP 26
encode_map_test() ->
Map = #{name => <<"room">>,
service => <<"conference">>,
jid => jid:encode({<<"user">>, <<"server">>, <<"">>}),
affiliation => member},
Encoded =
<<"{\"affiliation\":\"member\",\"jid\":\"user@server\",\"service\":\"conference\",\"name\":\"room\"}">>,
?assertMatch(Encoded, misc:json_encode(Map)).
-endif.
-else.
%% OTP 27 or higher or higher
encode_map_test() ->
Map = #{name => <<"room">>,
service => <<"conference">>,
jid => jid:encode({<<"user">>, <<"server">>, <<"">>}),
affiliation => member},
Encoded27 =
<<"{\"name\":\"room\",\"service\":\"conference\",\"jid\":\"user@server\",\"affiliation\":\"member\"}">>,
?assertMatch(Encoded27, misc:json_encode(Map)).
-endif.
decode_test() ->
Encoded =
<<"{\"affiliation\":\"member\",\"jid\":\"user@server\",\"service\":\"conference\",\"name\":\"room\"}">>,
TupleList =
#{<<"affiliation">> => <<"member">>,
<<"jid">> => <<"user@server">>,
<<"name">> => <<"room">>,
<<"service">> => <<"conference">>},
?assertMatch(TupleList, misc:json_decode(Encoded)).
decode_maps_test() ->
Encoded =
<<"{\"affiliation\":\"member\",\"jid\":\"user@server\",\"service\":\"conference\",\"name\":\"room\"}">>,
Map = #{<<"affiliation">> => misc:atom_to_binary(member),
<<"jid">> => jid:encode({<<"user">>, <<"server">>, <<"">>}),
<<"name">> => <<"room">>,
<<"service">> => <<"conference">>},
?assertMatch(Map, misc:json_decode(Encoded)).