Skip to content

Commit

Permalink
Add JSON EUnit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
badlop committed Jun 25, 2024
1 parent 75d767b commit c21cac5
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions test/json_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-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).

encode_map_test() ->
Map = #{name => <<"room">>,
service => <<"conference">>,
jid => jid:encode({<<"user">>, <<"server">>, <<"">>}),
affiliation => member},
Encoded26 =
<<"{\"affiliation\":\"member\",\"jid\":\"user@server\",\"service\":\"conference\",\"name\":\"room\"}">>,
?assertMatch(Encoded26, misc:json_encode(Map)).

-else.

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">> => atom_to_binary(member),
<<"jid">> => jid:encode({<<"user">>, <<"server">>, <<"">>}),
<<"name">> => <<"room">>,
<<"service">> => <<"conference">>},
?assertMatch(Map, misc:json_decode(Encoded)).

0 comments on commit c21cac5

Please sign in to comment.