diff --git a/src/jsn.app.src b/src/jsn.app.src index 0a80cf2..958cecc 100644 --- a/src/jsn.app.src +++ b/src/jsn.app.src @@ -1,6 +1,6 @@ {application, jsn, [ {description, "Utilities for interacting with decoded JSON in erlang"}, - {vsn, "1.2.0"}, %% <- need to set this appropriately when publishing to hex.pm + {vsn, "1.2.1"}, %% <- need to set this appropriately when publishing to hex.pm {applications, [kernel, stdlib, jsonx]}, diff --git a/src/jsn.erl b/src/jsn.erl index 738bd95..12386de 100644 --- a/src/jsn.erl +++ b/src/jsn.erl @@ -466,6 +466,10 @@ equal(Paths, OriginalObject, OtherObjectOrObjects) -> %% if mode is soft, a mismatch is allowed if the value is missing from any of %% the new objects %%------------------------------------------------------------------------------ +?IF_MAPS( +equal(Paths, OriginalObject, OtherObject, Mode) when is_map(OtherObject) -> + object_equal(Paths, OriginalObject, OtherObject, Mode); +) equal(Paths, OriginalObject, [], Mode) -> object_equal(Paths, OriginalObject, [], Mode); equal(Paths, OriginalObject, [{K,_}|_] = P, Mode) when K =/= struct -> diff --git a/test/jsn_tests.erl b/test/jsn_tests.erl index e855fc7..cc47c69 100644 --- a/test/jsn_tests.erl +++ b/test/jsn_tests.erl @@ -561,6 +561,87 @@ sort_keys_test_() -> ?_assertEqual(SortedEep18, jsn:sort_keys(Eep18)), ?_assertEqual(SortedStruct, jsn:sort_keys(Struct))]. +-ifdef(maps_support). + +equal_test_() -> + Field1 = <<"my field">>, + Field2 = <<"my great field">>, + Params = jsn:new([{Field1, false}, + {Field2, <<"42">>}]), + EqualParams = jsn:new([{Field2, <<"42">>}, + {Field1, false}]), + EqualStructParams = jsn:new([{Field2, <<"42">>}, + {Field1, false}], [{format, struct}]), + EqualEep18Params = jsn:new([{Field2, <<"42">>}, + {Field1, false}], [{format, eep18}]), + EqualMapParams = jsn:new([{Field2, <<"42">>}, + {Field1, false}], [{format, map}]), + MisMatchedParams = jsn:new([{Field1, false}, + {Field2, <<"451">>}]), + Nested = jsn:new([{Field1, false}, + {Field2, jsn:new([{Field1, true}, + {Field2, <<"42">>}])}]), + EqualNested = jsn:new([{Field1, false}, + {Field2, jsn:new([{Field2, <<"42">>}, + {Field1, true}])}]), + List = jsn:new([{Field1, [jsn:new([{Field1, true}, + {Field2, <<"42">>}]), + jsn:new([{Field2, <<"42">>}, + {Field1, false}])]}]), + EqualList = jsn:new({Field1, [jsn:new([{Field2, <<"42">>}, + {Field1, false}]), + jsn:new([{Field2, <<"42">>}, + {Field1, true}])]}), + Struct = jsn:new([{Field1, false}, + {[Field2,Field1], true}, + {[Field2, Field2], <<"42">>}], [{format, struct}]), + EqualStruct = jsn:new([{[Field2, Field2], <<"42">>}, + {[Field2, Field1], true}, + {Field1, false}], [{format, struct}]), + MismatchedStruct = jsn:new([{Field1, true}, + {[Field2, Field2], <<"42">>}, + {[Field2, Field1], false}], [{format, struct}]), + [?_assertEqual(ok, jsn:equal([Field1, Field2], Params, Params, hard)), + ?_assertEqual(ok, jsn:equal([Field1, Field2], Params, [Params, Params], hard)), + ?_assertEqual(ok, jsn:equal([Field1, Field2], Params, Params, soft)), + ?_assertEqual(ok, jsn:equal([Field1, Field2], Params, [Params, jsn:new()], soft)), + ?_assertEqual(ok, jsn:equal([Field2], Params, Params)), + ?_assertEqual(ok, jsn:equal([Field1], Params, MisMatchedParams)), + ?_assertEqual(ok, jsn:equal([Field2], Params, [Params, jsn:new()], soft)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field2], Params, jsn:new(), hard)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field2], Params, [EqualParams, jsn:new()], hard)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field1, Field2], Params, MisMatchedParams)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field2], Params, [Params, jsn:new(), MisMatchedParams], soft)), + ?_assertEqual(ok, jsn:equal([Field1, Field2], Params, EqualParams)), + ?_assertEqual(ok, jsn:equal([Field2, Field1], Params, EqualParams)), + ?_assertEqual(ok, jsn:equal([Field1], Params, EqualParams)), + ?_assertEqual(ok, jsn:equal([Field2], Params, EqualParams)), + ?_assertEqual(ok, jsn:equal([Field1], Params, EqualStructParams)), + ?_assertEqual(ok, jsn:equal([Field2], Params, EqualStructParams)), + ?_assertEqual(ok, jsn:equal([Field1], Params, EqualEep18Params)), + ?_assertEqual(ok, jsn:equal([Field2], Params, EqualEep18Params)), + ?_assertEqual(ok, jsn:equal([Field1], Params, EqualMapParams)), + ?_assertEqual(ok, jsn:equal([Field2], Params, EqualMapParams)), + ?_assertEqual(true, jsn:sort_equal(Params, EqualParams)), + ?_assertEqual(false, jsn:sort_equal(MisMatchedParams, Params)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field2], Nested, EqualNested)), + ?_assertEqual(true, jsn:sort_equal(Nested, EqualNested)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field1], List, EqualList)), + ?_assertEqual(true, jsn:sort_equal(List, EqualList)), + ?_assertEqual(ok, jsn:equal([Field1], Struct, EqualStruct)), + ?_assertEqual(true, jsn:sort_equal(Struct, EqualStruct)), + ?_assertMatch({error, {not_equal, _}}, + jsn:equal([Field1], Struct, MismatchedStruct)), + ?_assertEqual(false, jsn:sort_equal(Struct, MismatchedStruct))]. + +-else. + equal_test_() -> Field1 = <<"my field">>, Field2 = <<"my great field">>, @@ -634,6 +715,7 @@ equal_test_() -> jsn:equal([Field1], Struct, MismatchedStruct)), ?_assertEqual(false, jsn:sort_equal(Struct, MismatchedStruct))]. +-endif. path_equal_test_() -> Field1 = <<"cat meow">>,