From c21cac5c761fe868317b96d66d037b73998e7f0c Mon Sep 17 00:00:00 2001 From: Badlop Date: Tue, 25 Jun 2024 12:04:43 +0200 Subject: [PATCH] Add JSON EUnit testing --- test/json_test.erl | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/json_test.erl diff --git a/test/json_test.erl b/test/json_test.erl new file mode 100644 index 00000000000..8a67a627010 --- /dev/null +++ b/test/json_test.erl @@ -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)).