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.

71 lines
1.6 KiB

8 years ago
8 years ago
8 years ago
  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_07_compound_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. -include("jiffy_util.hrl").
  6. compound_success_test_() ->
  7. [gen(ok, Case) || Case <- cases(ok)] ++
  8. [gen(special_encoding, Case) || Case <- cases(special_encoding)].
  9. compound_failure_test_() ->
  10. [gen(error, Case) || Case <- cases(error)].
  11. gen(ok, {J, E}) ->
  12. gen(ok, {J, E, J});
  13. gen(ok, {J1, E, J2}) ->
  14. {msg("~s", [J1]), [
  15. {"Decode", ?_assertEqual(E, dec(J1))},
  16. {"Encode", ?_assertEqual(J2, enc(E))}
  17. ]};
  18. gen(special_encoding, {J, E}) ->
  19. {msg("~s", [J]), [
  20. {"Encode", ?_assertEqual(J, enc(E))}
  21. ]};
  22. gen(error, J) ->
  23. {msg("Error: ~s", [J]), [
  24. ?_assertThrow({error, _}, dec(J))
  25. ]}.
  26. cases(ok) ->
  27. [
  28. {<<"[{}]">>, [{[]}]},
  29. {<<"{\"foo\":[123]}">>, {[{<<"foo">>, [123]}]}},
  30. {<<"{\"foo\":{\"bar\":true}}">>,
  31. {[{<<"foo">>, {[{<<"bar">>, true}]} }]} },
  32. {<<"{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}">>,
  33. {[
  34. {<<"foo">>, []},
  35. {<<"bar">>, {[{<<"baz">>, true}]}},
  36. {<<"alice">>, <<"bob">>}
  37. ]}
  38. },
  39. {<<"[-123,\"foo\",{\"bar\":[]},null]">>,
  40. [
  41. -123,
  42. <<"foo">>,
  43. {[{<<"bar">>, []}]},
  44. null
  45. ]
  46. }
  47. ];
  48. cases(special_encoding) ->
  49. [
  50. {<<"{\"123\":\"foo\"}">>, {[{123, <<"foo">>}]}}
  51. ];
  52. cases(error) ->
  53. [
  54. <<"[{}">>,
  55. <<"}]">>
  56. ].