Skip to content

Commit

Permalink
Merge pull request #17 from nalundgaard/fix_equal_map
Browse files Browse the repository at this point in the history
fix equal/3,4 functions
  • Loading branch information
nalundgaard authored Oct 4, 2017
2 parents 0f3f84e + 1c2988e commit d2cffda
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jsn.app.src
Original file line number Diff line number Diff line change
@@ -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]},
Expand Down
4 changes: 4 additions & 0 deletions src/jsn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
82 changes: 82 additions & 0 deletions test/jsn_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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">>,
Expand Down Expand Up @@ -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">>,
Expand Down

0 comments on commit d2cffda

Please sign in to comment.