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.

87 line
3.1 KiB

  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_12_error_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. enc_invalid_ejson_test_() ->
  6. Type = invalid_ejson,
  7. Ref = make_ref(),
  8. {"invalid_ejson", [
  9. {"Basic", enc_error(Type, Ref, Ref)},
  10. {"Nested", enc_error(Type, {Ref, Ref}, {Ref, Ref})}
  11. ]}.
  12. enc_invalid_string_test_() ->
  13. Type = invalid_string,
  14. {"invalid_string", [
  15. {"Bare strign", enc_error(Type, <<143>>, <<143>>)},
  16. {"List element", enc_error(Type, <<143>>, [<<143>>])},
  17. {"Bad obj value", enc_error(Type, <<143>>, {[{foo, <<143>>}]})}
  18. ]}.
  19. enc_invalid_object_test_() ->
  20. Type = invalid_object,
  21. Ref = make_ref(),
  22. {"invalid_object", [
  23. {"Number", enc_error(Type, {1}, {1})},
  24. {"Ref", enc_error(Type, {Ref}, {Ref})},
  25. {"Tuple", enc_error(Type, {{[]}}, {{[]}})},
  26. {"Atom", enc_error(Type, {foo}, {foo})}
  27. ]}.
  28. enc_invalid_object_member_test_() ->
  29. Type = invalid_object_member,
  30. {"invalid_object_member", [
  31. {"Basic", enc_error(Type, foo, {[foo]})},
  32. {"Basic", enc_error(Type, foo, {[{bar, baz}, foo]})},
  33. {"Nested", enc_error(Type, foo, {[{bar,{[foo]}}]})},
  34. {"Nested", enc_error(Type, foo, {[{bar,{[{baz, 1}, foo]}}]})},
  35. {"In List", enc_error(Type, foo, [{[foo]}])},
  36. {"In List", enc_error(Type, foo, [{[{bang, true}, foo]}])}
  37. ]}.
  38. enc_invalid_object_member_arity_test_() ->
  39. Type = invalid_object_member_arity,
  40. E1 = {foo},
  41. E2 = {x, y, z},
  42. {"invalid_object_member", [
  43. {"Basic", enc_error(Type, E1, {[E1]})},
  44. {"Basic", enc_error(Type, E2, {[E2]})},
  45. {"Basic", enc_error(Type, E1, {[{bar, baz}, E1]})},
  46. {"Basic", enc_error(Type, E2, {[{bar, baz}, E2]})},
  47. {"Nested", enc_error(Type, E1, {[{bar,{[E1]}}]})},
  48. {"Nested", enc_error(Type, E2, {[{bar,{[E2]}}]})},
  49. {"Nested", enc_error(Type, E1, {[{bar,{[{baz, 1}, E1]}}]})},
  50. {"Nested", enc_error(Type, E2, {[{bar,{[{baz, 1}, E2]}}]})},
  51. {"In List", enc_error(Type, E1, [{[E1]}])},
  52. {"In List", enc_error(Type, E2, [{[E2]}])},
  53. {"In List", enc_error(Type, E1, [{[{bang, true}, E1]}])},
  54. {"In List", enc_error(Type, E2, [{[{bang, true}, E2]}])}
  55. ]}.
  56. enc_invalid_object_member_key_test_() ->
  57. Type = invalid_object_member_key,
  58. E1 = {1, true},
  59. {"invalid_object_member_key", [
  60. {"Bad string", enc_error(Type, <<143>>, {[{<<143>>, true}]})},
  61. {"Basic", enc_error(Type, 1, {[{1, true}]})},
  62. {"Basic", enc_error(Type, [1], {[{[1], true}]})},
  63. {"Basic", enc_error(Type, {[{foo,bar}]}, {[{{[{foo,bar}]}, true}]})},
  64. {"Second", enc_error(Type, 1, {[{bar, baz}, E1]})},
  65. {"Nested", enc_error(Type, 1, {[{bar,{[E1]}}]})},
  66. {"Nested", enc_error(Type, 1, {[{bar,{[{baz, 1}, E1]}}]})},
  67. {"In List", enc_error(Type, 1, [{[E1]}])},
  68. {"In List", enc_error(Type, 1, [{[{bang, true}, E1]}])}
  69. ]}.
  70. enc_error(Type, Obj, Case) ->
  71. ?_assertEqual({error, {Type, Obj}}, (catch jiffy:encode(Case))).