Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

87 wiersze
3.2 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. -define(ENC_ERROR(Type, Obj, Case),
  6. ?_assertEqual({error, {Type, Obj}}, (catch jiffy:encode(Case)))).
  7. enc_invalid_ejson_test_() ->
  8. Type = invalid_ejson,
  9. Ref = make_ref(),
  10. {"invalid_ejson", [
  11. {"the atom 'undefined'", ?ENC_ERROR(Type, undefined, undefined)},
  12. {"Basic", ?ENC_ERROR(Type, Ref, Ref)},
  13. {"Nested", ?ENC_ERROR(Type, {Ref, Ref}, {Ref, Ref})}
  14. ]}.
  15. enc_invalid_string_test_() ->
  16. Type = invalid_string,
  17. {"invalid_string", [
  18. {"Bare strign", ?ENC_ERROR(Type, <<143>>, <<143>>)},
  19. {"List element", ?ENC_ERROR(Type, <<143>>, [<<143>>])},
  20. {"Bad obj value", ?ENC_ERROR(Type, <<143>>, {[{foo, <<143>>}]})}
  21. ]}.
  22. enc_invalid_object_test_() ->
  23. Type = invalid_object,
  24. Ref = make_ref(),
  25. {"invalid_object", [
  26. {"Number", ?ENC_ERROR(Type, {1}, {1})},
  27. {"Ref", ?ENC_ERROR(Type, {Ref}, {Ref})},
  28. {"Tuple", ?ENC_ERROR(Type, {{[]}}, {{[]}})},
  29. {"Atom", ?ENC_ERROR(Type, {foo}, {foo})}
  30. ]}.
  31. enc_invalid_object_member_test_() ->
  32. Type = invalid_object_member,
  33. {"invalid_object_member", [
  34. {"Basic", ?ENC_ERROR(Type, foo, {[foo]})},
  35. {"Basic", ?ENC_ERROR(Type, foo, {[{bar, baz}, foo]})},
  36. {"Nested", ?ENC_ERROR(Type, foo, {[{bar,{[foo]}}]})},
  37. {"Nested", ?ENC_ERROR(Type, foo, {[{bar,{[{baz, 1}, foo]}}]})},
  38. {"In List", ?ENC_ERROR(Type, foo, [{[foo]}])},
  39. {"In List", ?ENC_ERROR(Type, foo, [{[{bang, true}, foo]}])}
  40. ]}.
  41. enc_invalid_object_member_arity_test_() ->
  42. Type = invalid_object_member_arity,
  43. E1 = {foo},
  44. E2 = {x, y, z},
  45. {"invalid_object_member", [
  46. {"Basic", ?ENC_ERROR(Type, E1, {[E1]})},
  47. {"Basic", ?ENC_ERROR(Type, E2, {[E2]})},
  48. {"Basic", ?ENC_ERROR(Type, E1, {[{bar, baz}, E1]})},
  49. {"Basic", ?ENC_ERROR(Type, E2, {[{bar, baz}, E2]})},
  50. {"Nested", ?ENC_ERROR(Type, E1, {[{bar,{[E1]}}]})},
  51. {"Nested", ?ENC_ERROR(Type, E2, {[{bar,{[E2]}}]})},
  52. {"Nested", ?ENC_ERROR(Type, E1, {[{bar,{[{baz, 1}, E1]}}]})},
  53. {"Nested", ?ENC_ERROR(Type, E2, {[{bar,{[{baz, 1}, E2]}}]})},
  54. {"In List", ?ENC_ERROR(Type, E1, [{[E1]}])},
  55. {"In List", ?ENC_ERROR(Type, E2, [{[E2]}])},
  56. {"In List", ?ENC_ERROR(Type, E1, [{[{bang, true}, E1]}])},
  57. {"In List", ?ENC_ERROR(Type, E2, [{[{bang, true}, E2]}])}
  58. ]}.
  59. enc_invalid_object_member_key_test_() ->
  60. Type = invalid_object_member_key,
  61. E1 = {1, true},
  62. {"invalid_object_member_key", [
  63. {"Bad string", ?ENC_ERROR(Type, <<143>>, {[{<<143>>, true}]})},
  64. {"Basic", ?ENC_ERROR(Type, 1, {[{1, true}]})},
  65. {"Basic", ?ENC_ERROR(Type, [1], {[{[1], true}]})},
  66. {"Basic", ?ENC_ERROR(Type, {[{foo,bar}]}, {[{{[{foo,bar}]}, true}]})},
  67. {"Second", ?ENC_ERROR(Type, 1, {[{bar, baz}, E1]})},
  68. {"Nested", ?ENC_ERROR(Type, 1, {[{bar,{[E1]}}]})},
  69. {"Nested", ?ENC_ERROR(Type, 1, {[{bar,{[{baz, 1}, E1]}}]})},
  70. {"In List", ?ENC_ERROR(Type, 1, [{[E1]}])},
  71. {"In List", ?ENC_ERROR(Type, 1, [{[{bang, true}, E1]}])}
  72. ]}.