You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 line
1.8 KiB

  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_16_dedupe_keys_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. dedupe_keys_test_() ->
  6. Opts = [dedupe_keys],
  7. Cases = [
  8. % Simple sanity check
  9. {
  10. {[{<<"foo">>, 1}]},
  11. {[{<<"foo">>, 1}]}
  12. },
  13. % Basic test
  14. {
  15. {[{<<"foo">>, 1}, {<<"foo">>, 2}]},
  16. {[{<<"foo">>, 2}]}
  17. },
  18. % Non-repeated keys are fine
  19. {
  20. {[{<<"foo">>, 1}, {<<"bar">>, 2}]},
  21. {[{<<"foo">>, 1}, {<<"bar">>, 2}]}
  22. },
  23. % Key order stays the same other than deduped keys
  24. {
  25. {[{<<"bar">>, 1}, {<<"foo">>, 2}, {<<"baz">>, 3}, {<<"foo">>, 4}]},
  26. {[{<<"bar">>, 1}, {<<"baz">>, 3}, {<<"foo">>, 4}]}
  27. },
  28. % Multiple repeats are handled
  29. {
  30. {[{<<"foo">>, 1}, {<<"foo">>, 2}, {<<"foo">>, 3}]},
  31. {[{<<"foo">>, 3}]}
  32. },
  33. % Sub-objects are covered
  34. {
  35. {[{<<"foo">>, {[{<<"bar">>, 1}, {<<"bar">>, 2}]}}]},
  36. {[{<<"foo">>, {[{<<"bar">>, 2}]}}]}
  37. },
  38. % Objects in arrays are handled
  39. {
  40. [{[{<<"foo">>, 1}, {<<"foo">>, 2}]}],
  41. [{[{<<"foo">>, 2}]}]
  42. },
  43. % Embedded NULL bytes are handled
  44. {
  45. {[{<<"foo\\u0000bar">>, 1}, {<<"foo\\u0000baz">>, 2}]},
  46. {[{<<"foo\\u0000bar">>, 1}, {<<"foo\\u0000baz">>, 2}]}
  47. },
  48. % Can dedupe with embedded NULL bytes
  49. {
  50. {[{<<"foo\\u0000bar">>, 1}, {<<"foo\\u0000bar">>, 2}]},
  51. {[{<<"foo\\u0000bar">>, 2}]}
  52. }
  53. ],
  54. {"Test dedupe_keys", lists:map(fun({Data, Result}) ->
  55. Json = jiffy:encode(Data),
  56. ?_assertEqual(Result, jiffy:decode(Json, Opts))
  57. end, Cases)}.